| Vi Cheat Sheet | ||||
| Command Line Options: | Change | |||
| vi file | Invoke vi on file | r | replace character | |
| vi file1 file2 | Invoke vi on files sequentially | cw | change word | |
| view file or vi -R file | Invoke vi on file in read-only mode | cc | change current line | |
| vi -r file | Recover file and recent edits after a crash | cmotion | change text between the cursor and the target of motion | |
| vi -t tag | Look up tag and start editing at its definition | C | change to end of line | |
| vi + file | open file at last line | R | overwrite characters | |
| vi +n file | open file at line no. n | S | substitute: delete current line and insert new text | |
| vi -c command file | open file , execute command (which is usually search command or line no. (POSIX) | s | substitute: delete character and insert new text | |
| vi +/pattern file | open file directly at pattern | |||
| Marking Position | Other Commands | |||
| mx | mark current position as x | . | repeat last edit command | |
| `x | move cursor to mark x | u, U | undo last edit, restore current line | |
| `` | return to previous mark or context | J | delete lines | |
| 'x | move to beginning of line containing mark x | |||
| '' | return to beginning of line containing previous mark | Delete, Move | ||
| x | delete character under cursor | |||
| Vi Commands | X | delete character before cursor | ||
| h, j, k, l | left, down, up, right | dw | delete word | |
| w, W, b, B | forward and backward by a word | dd | delete current line | |
| e, E | end of word | dmotion | delete text between the cursor and the target of motion | |
| ), ( | beginning of next, previous sentence | D | delete to end of line | |
| }, { | beginning of next, previous paragraph | p, P | put deleted text After, Before cursor | |
| ]], [[ | beginning of next, previous section | "np | put text from delete buffer number n after cursor (for last 9 deletions) | |
| 0, $ | first and last positions of current line | |||
| ^ | first nonblank character of the current line | ex edit commands | ||
| +, - | first nonblank character of the next and previous lines | :m | move lines | |
| n | | column n of current line | :co or :t | copy lines | |
| H, M, L | Top, Middle, Last line of screen | :.,$d | delete from current line to end of file | |
| nH, nL | n number of lines after Tp line and before Last line | :.,/pattern/co$ | copy from current line through line containing pattern to end of file. | |
| :30,60m0 | move lines 30 through 60 to top of file | |||
| Line Number in Vi | ||||
| ctrl G | Display current line number | Exit Commands | ||
| nG | move to line number n | zz, :x, :wq | write (save) and quit file [3 types] | |
| G | move to last line in file | :w | write (save) file | |
| :n | move to line n in file | :w! | write (save) file, overriding protection | |
| :30,60w newfile | write from line 30 through 60 as new file | |||
| Scrolling in Vi | :30,60w>> file | write from line 30 through 60 and append to file | ||
| ctrl F, ctrl B | scroll Forward, Backward one screen | :w %.new | write current buffer named file as file.new | |
| ctrl D, ctrl U | scroll Down, Up one half screen | :q | quit file | |
| ctrl E, ctrl Y | show one more line at Bottom, Top of window | :q! | quit file overriding protection | |
| ctrl L | redraw screen without scrolling | Q | quit vi and invoke ex | |
| z return, z., z- | reposition line with cursor: to Top, Middle, Bottom of screen | :n | edit next file | |
| :e file2 | edit file2 without leaving vi | |||
| Searching in Vi | :e! | return to version of current file at the time of last write | ||
| /pattern, ?pattern | search Forward, Backward for pattern | :e # | edit alternate file | |
| n, N | repeat last search in Same , Opposite direction | :vi | invoke vi editor from ex | |
| /, ? | repeat previous search Forward, Backward | : | invoke one ex command from vi editor | |
| fx, Fx | search Forward, Backward for character x in current line | # | alternate filename (substitutes into ex command) | |
| tx, Tx | search Forward, Backward to character Before, After x in current line | % | current filename (substitutes into ex command) | |
| ; | repeat previous current line search | |||
| , | repeat previous current line search in opposite direction | |||
| Editing Commands | ||||
| Insert | ||||
| i, a | insert text Before, After cursor | |||
| I, A | insert text Before beginning, After end of line | |||
| o, O | open new line for text Below, Above cursor | |||
| Yank (copy) | ||||
| yw | copy word | |||
| yy | copy current line | |||
| "ayy | copy current line into named buffer a (a-z). uppercase names append text | |||
| ymotion | copy text between the cursor and the target of motion | |||
| p, P | put copied text After, Before cursor | |||
| "aP | put text from buffer a Before cursor (a-z) | |||