Vim Cheatsheet

Show All

Hide All

Cursor Movement

  • h - move cursor left one character
  • j - move cursor down one line
  • k - move cursor up one line
  • l - move cursor right one character
  • w - jump forwards to the start of a word
  • W - jump forwards to the start of a WORD (WORD break = whitespace)
  • e - jump forwards to the end of a word
  • E - jump forwards to the end of a WORD (WORD break = whitespace)
  • b - jump backwards to the start of a word
  • B - jump backwards to the start of a WORD (WORD break = whitespace)
  • 0 - jump to the start of the line
  • ^ - jump to the first non-blank character of the line
  • $ - jump to the end of the line
  • G - go to the last line of the document
  • 5G - go to line 5
  • gg - go to line 1
  • `` - jump to previous cursor position
  • g; - jump to last edit made
  • fx - jump to next occurrence of character x
  • Fx - jump to previous occurrence of character x
  • tx - jump to character before thenext occurrence of character x
  • Tx - jump to character before the previous occurrence of character x
  • ; - Repeat latest f, t, F or T search.
  • , - Repeat latest f, t, F or T search in other direction.
  • } - Jump to next paragraph (or function/block, when editing code)
  • { - Jump to previous paragraph (or function/block, when editing code)
  • % - Jump to match of the current brace, bracket, parenthesis, comment

Cut and Paste

  • yy - yank (copy) a line
  • 2yy - yank (copy) 2 lines
  • yw - yank (copy) word
  • y$ - yank (copy) to end of line
  • y^ - yank (copy) to start of line
  • p - put (paste) the clipboard after cursor
  • 3p - put (paste) the clipboard after cursor 3 times
  • P - put (paste) before cursor
  • dd - delete (cut) a line
  • 2dd - delete (cut) 2 lines
  • dw - delete (cut) word
  • D - delete (cut) to the end of the line
  • x - delete (cut) character
  • xp - cut and paste a letter

Working with Multiple Files

  • :e filename - edit a file called 'filename' in a new buffer
  • :ls - list open buffers
  • :b2 - go to buffer 2 in list of open buffers
  • :bn - go to the next buffer
  • :bp - go to the previous buffer
  • :bd - delete a buffer (close a file)
  • :sp filename - open a file in a new buffer and split window
  • :vsp filename - open a file in a new buffer and vertically split window
  • :hide - hide window
  • :only - only this window stays open
  • Ctrl + ww - switch windows

Exiting

  • :w - write (save) the file, but don't exit
  • :wq or :x or ZZ - write (save) and quit
  • :q - quit (fails if there are unsaved changes)
  • :q! or ZQ - quit and throw away unsaved changes

Insert Text

  • i - insert before the cursor
  • I - insert at the beginning of the line
  • a - insert (append) after the cursor
  • A - insert (append) at the end of the line
  • o - append (open) a new line below the current line
  • O - append (open) a new line above the current line
  • Esc - exit insert mode

Editing

  • r - replace a single character
  • J - join line below to the current one
  • cw - change (replace) to the end of the word
  • cc - change (replace) entire line
  • C - change (replace) to the end of the line
  • ci" - change (replace) text within " quotes (also works for ', <, (, {, etc)
  • s - delete character and substitute text
  • u - undo
  • Ctrl + r - redo
  • . - repeat last command

Visual Selection

  • v - start visual mode, mark lines, then do a command (like y-yank)
  • V - start linewise visual mode
  • o - move to other end of marked area
  • Ctrl + v - start visual block mode
  • O - move to other corner of block
  • aw - mark a word
  • ab - a block with ()
  • aB - a block with {}
  • ib - inner block with ()
  • iB - inner block with {}
  • Esc - exit visual mode

Editing Visual Selection

  • > - shift text right
  • < - shift text left
  • :le 5 - text becomes indented 5 spaces
  • y - yank (copy) marked text
  • d - delete marked text
  • ~ - switch case

Tabs

  • :tabe filename - edit a file called 'filename' in a new tab
  • gt or :tabn - move to the next tab
  • gT or :tabp - move to the previous tab
  • #gt - move to tab number #
  • :tabmove # - move current tab to the #th position (indexed from 0)
  • :tabc - close the current tab and all its windows
  • :tabo - close all tabs except for the current one

Marks

  • mx - create mark 'x'
  • `x - jump to mark 'x'
  • d`x - delete up to mark 'x'
  • :delm x - delete mark 'x'
  • :delm q-x - delete marks 'q' through 'x'
Check out the vim learning game to practice some vim basics.