Vim Cheatsheet

last modified: | 8 min read #cheatsheet #tools #vim

This cheatsheet is focused on how to work with vanilla (Neo)Vim.

General

Help

  • :h[elp] keyword - open help for keyword
  • K - open man page for word under cursor

Exiting

  • :x or ZZ - writes (only if changes were made) and quit the file
  • :wq or :x - writes (even when no changes were made) and quit the file
  • :wqa - save all tabs and quit
  • :q - quit (fails if unsaved changes were made)
  • :q! or ZQ - quit and throw away unsaved changes

File Operations

  • :w - save the file
  • :w !sudo tee % - save the file using sudo
  • :sav[eas] file - save file as

Movement

Cursor Movement

  • h - move cursor left
  • j - move cursor down
  • k - move cursor up
  • l - move cursor right

Cursor Movement Relative to Screen

  • H - move to top of screen
  • M - move to middle of screen
  • L - move to bottom of screen
  • zz - center cursor on screen

Occurence Movement

  • fx - jump no next occurence of character x
  • Fx - jump to previous occurence of character x
  • tx - jump to before next occurence of character x
  • Tx - jump to after previous occurence of character x

Quick Movement

  • % - move to matching character (default supported pairs: (), {}, []) - use :h matchpairs in Vim for more information)
  • ^ - jump to the first non-blank character of the line
  • $ - jump to the end of the line
  • g_ - jump to the last non-blank character of the line
  • gg - jump to the first line of the document
  • G - jump to the last line of the document
  • :5 - jump to line 5 in the document

Content Movement

  • w - jump forwards to the start of a word
  • W - jump forwards to the start of a word (words can contain punctuation)
  • e - jump to the end of a word
  • E - jump to the end of a word (words can contain punctuation)
  • b - jump backwards to the start of a word
  • B - jump backwards to the start of a word (words can contain punctuation)
  • ge - jump backwards to the end of a word
  • gE - jump backwards to the end of a word (words can contain punctuation)

Screen Movement

  • Ctrl + e - move the screen down one line (without moving the cursor)
  • Ctrl + y - move the screen up one line (without moving the cursor)
  • Ctrl + b - move back one full screen
  • Ctrl + f - move forward one full screen
  • Ctrl + d - move forward 1/2 a screen
  • Ctrl + u - move backward 1/2 a screen

Declaration Movement

  • gd - jump to local declaration
  • gD - jump to global declaration
  • gf - jump to file

Repeat Movement

  • ; - repeat previous f, t, F, T movement
  • , - repeat previous f, t, F, T movement backwards

Paragraph Movement

  • } - jump to next paragraph (or function/block, when editing code)
  • { - jump to previous paragraph (or function/block, when editing code)

Insert Mode

Basic Insertion

  • 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

Append

  • o - append (open) a new line below the current line
  • O - append (open) a new line above the current line

Newline Insertion

  • Ctrl + j - begin new line during insert mode

Deletion

  • Ctrl + h - delete the character before the cursor during insert mode
  • Ctrl + w - delete word before the cursor during insert mode

Auto-complete Insertion

  • Ctrl + n - insert (auto-complete) next match before the cursor during insert mode
  • Ctrl + p - insert (auto-complete) previous match before the cursor during insert mode

Indentation

  • Ctrl + t - indent (move right) line one shiftwidth during insert mode
  • Ctrl + d - de-indent (move left) line one shiftwidth during insert mode

Exit Insert Mode

  • Esc - exit insert mode
  • Ctrl + c - exit insert mode
  • Ctrl + [ - exit insert mode

Register Insertion

  • Ctrl + rx - insert the contents of register x

Temporal Normal Mode

  • Ctrl + ox - Temporarily enter normal mode to issue one normal-mode command x.

Visual Mode

Basic Visual Mode

  • v - start visual mode, mark lines, then do a command (like y-yank)
  • V - start linewise visual mode
  • Ctrl + v - start visual block mode

Movement

  • o - move to other end of marked area
  • O - move to other corner of block

Exit Visual Mode

  • Esc - exit visual mode

Marking

  • aw - mark a word
  • ab - a block with ()
  • aB - a block with {}
  • at - a block with <> tags

Inner Marking

  • ib - inner block with ()
  • iB - inner block with {}
  • it - inner block with <> tags

Editing

Replace

  • r - replace a single character.
  • R - replace more than one character, until ESC is pressed.

Cut

  • cc - change (replace) entire line
  • C - change (replace) to the end of the line
  • c$ - change (replace) to the end of the line
  • ciw - change (replace) entire word
  • cw or ce - change (replace) to the end of the word

Delete and Insert

  • s - delete character and substitute text
  • S - delete line and substitute text (same as cc)

Copying

  • y - yank (copy) the marked text in visual mode
  • yy - yank (copy) a line
  • 2yy - yank (copy) 2 lines
  • yw - yank (copy) the characters of the word from the cursor position to the start of the next word
  • yiw - yank (copy) word under the cursor
  • yaw - yank (copy) word under the cursor and the space after or before it
  • y$ - yank (copy) to end of line
  • :#,#y - yank (copy) the content of the given lines (e.g :34,65y)

Paste

  • p - put (paste) the clipboard after cursor
  • P - put (paste) before cursor

Repeat

  • . - repeat last command

Join

  • J - join line below to the current one with one space in between
  • gJ - join line below to the current one without space in between

Case Switching

  • ~ - switch case under cursor
  • g~ - switch case up to motion
  • gu - change to lowercase up to motion
  • gU - change to uppercase up to motion

Indentation

  • == - reindent line
  • >> - indent line
  • << - de-indent line

Undo and Redo

  • u - undo
  • U - restore (undo) last changed line
  • Ctrl + r - redo

Transpose

  • xp - transpose two letters (delete and paste)

Reflow

  • gwip - reflow paragraph

Deletion with Cut

  • dd - delete (cut) a line
  • 2dd - delete (cut) 2 lines
  • dw - delete (cut) the characters of the word from the cursor position to the start of the next word
  • diw - delete (cut) word under the cursor
  • daw - delete (cut) word under the cursor and the space after or before it
  • D - delete (cut) to the end of the line
  • d$ - delete (cut) to the end of the line
  • x - delete (cut) character
  • :#,#d - delete (cut) the content of the given lines (e.g :34,65d)

Working with multiple files

Buffer Operations

  • :e[dit] file - edit a file in a new buffer
  • :bd[elete] - delete a buffer (close a file)
  • :ls or :buffers  - list all open buffers
  • :tab ba[ll] - edit all buffers as tabs

Split Operations

  • Ctrl + ws - split window
  • Ctrl + wv - split window vertically
  • Ctrl + ww - switch windows
  • Ctrl + wq - quit a window
  • Ctrl + wx - exchange current window with next one
  • Ctrl + w= - make all windows equal height & width
  • Ctrl + wH - make current window full height at far left (leftmost vertical window)
  • Ctrl + wL - make current window full height at far right (rightmost vertical window)
  • Ctrl + wJ - make current window full width at the very bottom (bottommost horizontal window)
  • Ctrl + wK - make current window full width at the very top (topmost horizontal window)

Tabs Operations

  • :tabnew or :tabnew {page.words.file} - open a file in a new tab
  • Ctrl + wT - move the current split window into its own tab
  • :tabm[ove] # - move current tab to the #th position (indexed from 0)
  • :tabc[lose] - close the current tab and all its windows
  • :tabo[nly] - close all tabs except for the current one
  • :tabdo command - run the command on all tabs (e.g. :tabdo q - closes all opened tabs)
  • :tabedit file2 - will open a new tab and take you to edit file2
  • $ vim -p file1.c file2.h - to open multiple files in tabs

Move between Buffers

  • :bn[ext] - go to the next buffer
  • :bp[revious] - go to the previous buffer
  • :b[uffer]# - go to a buffer by index #
  • :b[uffer] file - go to a buffer by file

Move between Splits

  • Ctrl + wh - move cursor to the left window (vertical split)
  • Ctrl + wl - move cursor to the right window (vertical split)
  • Ctrl + wj - move cursor to the window below (horizontal split)
  • Ctrl + wk - move cursor to the window above (horizontal split)

Buffers and Splits

  • :sp[lit] fil - open a file in a new buffer and split window
  • :vs[plit] file - open a file in a new buffer and vertically split window
  • :vert[ical] ba[ll] - edit all buffers as vertical windows

Move Between Tabs

  • :tabs - to list all the open tabs
  • gt or :tabn[ext] - move to the next tab
  • gT or :tabp[revious] - move to the previous tab
  • #gt - move to tab number #
  • :tabfirst - to directly move to the first tab
  • :tablast - to directly move to the last tab

Pane

  • :clo[se] - close current pane

References

  1. A lot of the content on this page I’ve found here: here