Friday, 28 March 2014

vi Cheat Sheet

Navigating File
asdf
w forward one wordb back to start of word
0 start of line$ end of line
nG go to line nG go to last line
Inserting Text (§) ESC to finish
i insert before cursor (§)A append at the end of line (§)
o open new line below (§)C change to end of line (§)
O open new line above (§)D delete to end of line
Editing:1 char1 wordn words1 linen lines
deletexdwndwddndd
changercw(§)ncw(§)cc(§)ncc(§)
copy (yank) into bufferywnywyynyy
p paste buffer after/below cursorP paste buffer before/above cursor
delete -> move cursor -> paste = cut & paste
copy -> move cursor -> paste = duplicate
Searching for Text:
/pattern search for patternn repeat last search
:n,ms/old/new/ between lines n and m substitute old with new
Miscellaneous
. repeat last edit commandu undo last edit command
>> indent lineU undo all changes to current line
J join lines^L redraw screen
Writing the file/Quitting vi
:r file read contents of file and place after current line
:n,mw file write portion of file between lines n and m to file
:w save:w file save to file:w! force save (often as root)
:q quit:wq save & quit:q! force quit (no save)
Escaping into shell
:!ksh run interative shell; press ^D to return to vi
:!cmd run cmd; press <CR> to return to vi
!!cmd run cmd; put its output into file being edited