Thursday, July 31, 2008

vi commands

Switch to command mode <ESC>

ESC start command mode ( it starts in command mode )
i change to insert mode
:q quit
:wq write and quit
:w write the file
:# goto line #
/str search down for "str"
?str search up for "str"
dd delete line
x delete character
dw delete word ( cut )
yy yank line ( copy )
yw yank word
p paste
u undo ( only limit is the last write )
. repeat last change
cw change word

Go to line number ## :##

Undo previous command: u

Undo all changes to line: U

Search backward for string: ?string

Search forward for string: /string

% (entire file) s (search and replace) /old text with new/ c (confirm) g (global - all): %s/oldstring/newstring/cg

Delete 5 lines (to buffer): 5dd

Delete lines 5-10:5,10d

string replacement

Sometimes you want to replace one string with another. This is done with the ex command "s". The form of the replacement is :
: line_range s/old/new/g
ex ( replace foo with bar from line 10 to 20 ) :
:10,20s/foo/bar/g
You can use visual mode to select the lines, when you type the colon to start the command it will use symbols for the visual line range.