Skip to main content

Linux tip - Find and replace in vi

To find and replace one or more occurences of a given text pattern with a
new text string, use the s[ubstitute] command.

There are a variety of options, but these are what you most probably want:

:%s/foo/bar/g find each occurance of 'foo' and replace it with 'bar' without asking for confirmation

:%s/foo/bar/gc find each occurance of 'foo' and replace it with 'bar' asking for confirmation first

:%s//bar/gc find (match exact word only) and replace each occurance of 'foo' with 'bar'

:%s/foo/bar/gci find (case insensitive) and replace each occurance of 'foo' with 'bar'

:%s/foo/bar/gcI find (case sensitive) and replace each occurance of 'foo' with 'bar'


NB: Without the 'g' flag, replacement occurs only for the first occurrence in each line.

For a full description and some more interesting examples of the substitute command refer to

:help substitute

See also:

:help cmdline-ranges
:help pattern
:help gdefault


To find out more check out:
http://www.vim.org/tips/tip.php?tip_id=31

Comments

Popular posts from this blog

Linux - Bash - .Xdefaults - To have customized xterm window

Open a file in your root folder named .Xdefaults Add the following code in the file. XTerm*Background:   black XTerm*Foreground:   linen XTerm*font:  9x15 XTerm*saveLines:    1000 XTerm*HiForeColor: white XTerm*HiBackColor: #c06077 XTerm*geometry: 150x40+10+10 XTerm*font:             -*-courier 10 pitch-medium-r-*-*-14-*-*-*-*-*-iso8859-* To select the code for any required font, run the command  xfontsel -print This opens a GUI where you can select the required font, whose code would be displayed while it is selected and at the time when you exit the application.  Add this code in the .Xdefaults file.

Save Widows remote desktop credentials

First, START > All Programs > Accessories > Remote Desktop Connection Type in the computer name and clock OPTIONS Check off the box that says save credentials Now connect, and you will be prompted for your username/password. Enter them. Now you should be connected to the remote computer. Close out of the remote session. Method 1 Create a .bat file that has the following command: START mstsc.exe /v: & exit Run the .bat file and the remote desktop session should open and submit your saved credetials automatically. Method 2 Type mstsc -v it will login without asking credentials. Method 3 Save the session as a .rdp file (e.g: myRemoteMachine.rdp) and Double click the rdp file to open the remote desktop session without asking for credentials.

Vim Cheat Sheet

This is a small collection of tips and tricks I have collected for the vim text editor. This is by no means complete or a tutorial on how to use vim. Just a set of commands I don’t want to forget, but don’t use often enough to burn into memory, yet. See the resources section below for a more complete introduction and a set of in-depth tutorials. Navigation Marks ma" --> mark spot label it a 'a     --> jump to spot marked by label a Copy and Paste Registers Vim has a clipboard history stored in registers, you can also use these registers to cut and paste items to. "ad " cut something to register a "ap " paste something from register a :reg " list registers Deleting Lines :g/regexp/d " delete all lines that match regexp :v/regexp/d " delete all lines that do NOT regexp :15,20d " delete lines 10-20 Buffer Management :ls " matching list buffers :b [nu...