Skip to content
abdallahmekky

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.

All cheat sheets

Modes

KeyMode
EscNormal mode: commands, movement, deleting
i a oInsert mode: type text
vVisual mode: select characters
VVisual line mode: select whole lines
Ctrl+vVisual block mode: select a rectangle
:Command-line mode: :w, :q, :%s/a/b/g and more

Entering insert mode

KeyWhat it does
i / aInsert before / after the cursor
I / AInsert at the start / end of the line
o / OOpen a new line below / above
s / SReplace the character / the whole line
giInsert 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.

KeysWhat it does
ciwChange 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
dapDelete the whole paragraph
yi(Copy the text inside parentheses
citChange 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

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.

SettingEffect
:set numberShow line numbers
:set relativenumberShow distances from the cursor line
:set hlsearch incsearchHighlight matches and search as you type
:set ignorecase smartcaseCase-insensitive unless you type a capital
:set tabstop=4 shiftwidth=4 expandtabFour-space indentation
:set autoindentKeep the indent of the previous line
:set pastePaste without auto-indent breaking the text
:syntax onSyntax 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.