CHEAT SHEET
Vim cheat sheet
Vim keys for editing config files fast. Normal mode is home base: press Esc to get back to it from anywhere.
Modes
| Key | Mode |
|---|---|
| Esc | Normal mode: commands, movement, deleting |
| i a o | Insert mode: type text |
| v | Visual mode: select characters |
| V | Visual line mode: select whole lines |
| Ctrl+v | Visual block mode: select a rectangle |
| : | Command-line mode: :w, :q, :%s/a/b/g and more |
Entering insert mode
| Key | What it does |
|---|---|
| i / a | Insert before / after the cursor |
| I / A | Insert at the start / end of the line |
| o / O | Open a new line below / above |
| s / S | Replace the character / the whole line |
| gi | Insert where you last left insert mode |
Moving around
h j k l # left, down, up, rightw b e # next word, previous word, end of word0 ^ $ # start of line, first non-blank, end of linegg G # top of the file, bottom of the file42G :42 # go to line 42% # jump to the matching bracket{ } # previous and next paragraphCtrl+f Ctrl+b # page down, page upCtrl+d Ctrl+u # half a page down, half a page upzz # center the cursor line on screen
Editing
x # delete the character under the cursordd # delete (cut) the linedw d$ # delete a word, delete to the end of the lineyy yw # copy the line, copy a wordp P # paste after, paste beforecw ciw # change a word, change the whole word under the cursorcc C # change the line, change to the end of the liner R # replace one character, enter replace modeJ # join the next line onto this one~ # toggle the case of a character. # repeat the last changeu Ctrl+r # undo, redo>> << # indent, unindent the line3dd 5x # a number repeats a command: 3 lines, 5 characters
Text objects
Pattern: an action (d, c, y) + i (inside) or a (around) + an object. Cursor anywhere inside the object works.
| Keys | What it does |
|---|---|
| ciw | Change the word under the cursor |
| ci" | Change the text inside double quotes |
| ci( / ci{ | Change inside parentheses / braces |
| da( | Delete the parentheses and what is inside |
| dap | Delete the whole paragraph |
| yi( | Copy the text inside parentheses |
| cit | Change inside an HTML tag |
Visual mode
v V Ctrl+v # select characters, lines, a block> < # indent, unindent the selectiony d c # copy, delete, change the selectiono # jump to the other end of the selectiongv # reselect the last selectionCtrl+v I text Esc # type the same text at the start of many lines
Search and replace
/pattern # search forward (n for the next match, N for the previous)?pattern # search backward* # search for the word under the cursor (the # key goes backward):%s/old/new/g # replace every match in the file:%s/old/new/gc # same, asking for confirmation each time:5,10s/old/new/g # only on lines 5 to 10:noh # clear the search highlighting
Files and windows
:w :q :wq :q! # save, quit, save and quit, quit without savingZZ ZQ # save and quit, quit without saving:w newname # save as a new file:e file # open another file:r file # insert a file below the cursor:r !date # insert the output of a command:sp :vsp # split the window horizontally, verticallyCtrl+w w # switch to the next windowCtrl+w h j k l # move to the window in that direction:ls :b2 # list buffers, jump to buffer 2:%!sort # filter the whole file through sort
Useful settings
Put permanent ones in ~/.vimrc, without the leading colon.
| Setting | Effect |
|---|---|
| :set number | Show line numbers |
| :set relativenumber | Show distances from the cursor line |
| :set hlsearch incsearch | Highlight matches and search as you type |
| :set ignorecase smartcase | Case-insensitive unless you type a capital |
| :set tabstop=4 shiftwidth=4 expandtab | Four-space indentation |
| :set autoindent | Keep the indent of the previous line |
| :set paste | Paste without auto-indent breaking the text |
| :syntax on | Syntax highlighting |
Macros, marks and registers
qa # start recording a macro into register aq # stop recording@a @@ # play the macro, repeat the last macro10@a # play it 10 timesma 'a # set mark a, jump back to mark a"ayy "ap # copy a line into register a, paste from register a"+y "+p # copy to and paste from the system clipboard:reg # show registers
More cheat sheets
Questions
How do I exit Vim?
Press Esc, then type :q and Enter to quit, :wq to save and quit, or :q! to quit without saving. ZZ also saves and quits.
How do I search and replace in Vim?
Use :%s/old/new/g to replace every match in the file. Add c at the end (:%s/old/new/gc) to confirm each replacement.
What is the difference between normal mode and insert mode?
Normal mode runs commands such as dd or yy. Insert mode types text. Press i to enter insert mode and Esc to return to normal mode.
How do I learn Vim quickly?
Run vimtutor in a terminal. It is an interactive tutorial installed with Vim and takes about half an hour.