Freitag, 2. März 2012

VI Commands to remember

Sometimes you need to edit a file with VI. Because the file is too big for other editors. Or you just have no other editor. But if you know these basic commands, using VI is really a pleasure:

Open, save, quit

CommandDescription
vi filenameopen filename in VI
ZZ or :wq or :xwrite changes to file and quit VI

Switching between edit mode and command mode

CommandDescription
ichange to edit mode at cursor position
ESCchange to command mode

Moving the cursor

CommandDescription
1234Gmove cursor to line 1234
Gmove cursor to end of file
12|move cursor to column 12
/stringmove cursor to first occurence of string
nmove cursor to next occurence of string

Copy & paste, delete

CommandDescription
yycopy current line into buffer
3yycopy next 3 lines into buffer
ppaste the line(s) in the buffer into the text after the current line
Ddelete the remainder of the current line
dGdelete to end of file
dddelete the current line
3dddelete the next three lines

Search & replace

CommandDescription
s/string1/string2/ replace all occurencies of string1 with string2