Useful vi command - quick guide
Useful vi Commands
Cut/Paste Commands:
| x |
delete one character (destructive backspace) |
| dw |
delete the current word (Note: ndw deletes n numbered words) |
| dd |
delete the current line (Note: ndd deletes n numbered lines) |
| D |
delete all content to the right of the cursor |
| d$ |
same as above |
| :u |
undo last command |
| p,P |
paste line starting one line below/above current cursor location |
| J |
combine the contents of two lines |
| "[a-z]nyy |
yank next n lines into named buffer [a-z] |
| "[a-z]p/P |
place the contents of selected buffer below/above the current line |
Extensions to the Above Commands:
| :3,18d |
delete lines 3 through 18 |
| 16,25m30 |
move lines 16 through 25 to after line 30 |
| 23,29co62 |
copy specified lines and place after line 62 |
Cursor Relocation commands:
| :[n] |
goto line [n] |
| shift g |
place cursor on last line of text |
| h/l/j/k |
move cursor left, right, down and up |
| ^f/^b |
move forward, backward in text, one page |
| ^u/^d |
move up, down one half page |
| $ |
move to end of line |
| 0 |
move to beginning of line |
Extensions to the Above:
| b |
move backwards one word (Note: nb moves back n number of words) |
| e |
move to end of current word |
| ( |
move to beginning of curent block |
| ) |
move to the end of current block |
Searching and Substitution commands:
| / [string] |
search forward for string |
| ? [string] |
search backwards for string |
| n |
repeat last search |
| N |
repeat search in opposite direction |
| cw |
change the contents of the current word, (use ESC to stop replacement mode) |
| c$ |
Replace all content to the right of cursor (exit replacement mode with ESC) |
| c0 |
Replace all content to the left of cursor (exit with ESC) |
| :1,$s/s1/s2/g |
global replacement of string1 with string2 |
| r |
replace current character with next character typed |
Entering the Insert Mode:
| i |
Begin inserting text at current cursor location |
| I |
Begin inserting text at the beginning of the current line |
| a |
Begin appending text, one character to the right of current cursor location |
| A |
Begin appending text at the end of the current line |
| o/O |
Begin entering text one line below\above current line |
| ESC |
Exit insertion mode and return to command mode |
Exiting and Entering VI
| ZZ |
save file and exit VI |
| :wq |
same as above |
| :e! |
return to last saved version of current file |
| :q |
quit without save, (Note :q! is required if changes have been made) |
| :w |
write without exit (:w! to force write) |
Fancy Stuff:
| :1,10w file |
write lines 1 through 10 to file newfile |
| :340,$w >> file |
write lines 340 through the end of the file and append to file newfile |
| :sh |
escape temporarily to a shell |
| ^d |
return from shell to VI |
| :![command] |
execute UNIX command without leaving VI |
| :r![command] |
read output of command into VI |
| :r[filename] |
read filename into VI |
| :$r newfile |
read in newfile and attach at the end of current document |
| :r !sort file |
read in contents of file after it has been passed through the UNIX sort |
| :n |
open next file (works with wildcard filenames, ex: vi file*) |
| :^g |
list current line number |
| :set |
number show line numbers |
| :set showinsert |
show flag ("I") at bottom of screen when in insert mode |
| :set all |
display current values of VI variables |
| :set ai |
set autoindent; after this enter the insert mode and tab, from this point on VI will indent each line to this location. Use ESC to stop the indentations |
| ^T |
set the autoindent tab one tab stop to the right |
| ^D |
set the autoindent tab one stop to the left |
| :set tabstop=n |
sets default tab space to number n |
| >> |
shift contents of line one tab stop to the right |
| << |
shift contents of line one tab stop to the left |
This entry was posted
on Monday, April 21st, 2008 at 1:12 am and is filed under Unix.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
April 21st, 2008 at 1:31 am
I finally decided to write a comment on your blog. I just wanted to say good job. I really enjoy reading your posts.
Tina Russell
[Reply]