VIM Cheat Sheet
Getting started
Moving cursor
| Action | Commands |
|---|---|
| cursor left | left arrow or h |
| cursor right | right arrow or l |
| cursor up | up arrow or k |
| cursor down | down arrow or j |
| jump to last line | G |
| jump to first line | gg or :1 |
| jump to line 99 | :99 |
| jump to end of line | $ |
| jump to start of line | 0 (zero) |
TEXT EDITING
| Action | Commands |
|---|---|
| enter insert mode | i |
| back to normal mode | ESC |
| delete a character | x |
| delete a word | dw |
| delete to the end of the line | d$ |
| delete a line | dd |
| delete multiple lines | 10dd (delete 10 lines) |
| replace a character | r |
| replace a word | cw or ce |
| append a new line below | o |
| append a new line above | O |
| undo | u |
CUT and COPY and PASTE
| Action | Command |
|---|---|
| yank(copy) current line | yy |
| copy 3 lines | 3yy |
| paste copied lines after cursor | p |
| paste copied lines before cursor | P |
| cut(delete) current line | dd |
| cut(delete) to the end of the line | d$ |
| show the contents of all registers | :reg |
SAVE and EXITING
| Action | Command |
|---|---|
| Go back to normal mode | ESC |
| Save but don’t exit | :w |
| save then exit | :wq or :x or ZZ |
| force exit without saving | :q! |
| force saving then exit | :wq! |
More functions
Search and Replace
| Actions | Command |
|---|---|
| search a word | /word type n or N to repeat |
| substitute ‘new’ for ‘old’ | :s/old/new/ |
| substitute globally on the line | :s/old/new/g |
| substitute string between lines | :2,11 s/old/new/g |
| substitute string in the whole file | :%s/old/new/g |
Execute external commands
| Actions | Command |
|---|---|
| execute an external command | :!command |
| show file location | :!pwd |
| write current file with name FILENAME | :w FILENAME |
| Save line 10 through 20 to file FILENAME | :10,20w FILENAME |
| insert contents of FILENAME to current file | :r FILENAME |
SET OPTION
| Actions | Command |
|---|---|
| Show line number | :set number |
| disable search history | :noh |
Editing multiple files
| Actions | Command |
|---|---|
| Edit another file in buffer | :e[dit] foo.txt |
| go to next buffer | :bn[ext] |
| go to previous buffer | :bp[revious] |
| delete a buffer(close file) | :bd[elete] |
| go to a buffer by index # | :b# |
| go to buffer by file name | :b file |
| list all buffers | :ls |
| open a file in a new buffer and split window | :sp[lit] file |
| open a file in a new buffer and vertically split window | :vs[plit] file |
| edit all buffers as vertical windows | :vert[ical] ba[ll] |
| edit all buffers as tabs | :tab ba[ll] |
| split window | Ctrl + ws |
| split window vertically | Ctrl + wv |
| switch windows | Ctrl + ww |
| quit a window | Ctrl + wq |
| exchange current window with next one | Ctrl + wx |
| make all windows equal height & width | Ctrl + w= |