Skip to content

Vim cheatsheet

Basics

Exit

  • :q quit
  • :q! quit without saving
  • :qa close all files and quit
  • :wq or :x save and close file

Edit

  • i insert
  • a append
  • A append at the end of line
  • o next line
  • O previous line
  • r replace one character
  • R replace mode
  • x delete character
  • u undo
  • <C-R> redo


Copy/paste

  • yy copy line
  • p paste
  • P paste before
  • dd cut

Sort

  • :sort sort
  • :sort! sort in reverse order
  • :sort u sort and remove duplicates
  • :sort i ignore case

Search & replace

:s search and replace in current line

:%s search and replace in whole file

Note: ., *, \, [, ^, and $ are meta characters. +, ?, |, &, {, (, and ) must be escaped to use their special function.

Replace character with new line, use \r:

%s/;/\r/g

Tabs

  • Open file in a new tab
:tabedit file.txt

or make the command shorter

:tabe file.txt
  • Open the file.txt in a new tab, or go to the tab with file.txt if it’s already open:
:tab drop file.txt
  • Open multiple files in tabs at startup
vim -p file1.txt file2.txt file3.txt

Moving between tabs

  • gt or :tabn - next tab
  • gT or :tabp - previous tab
  • Ngt go to tab number N - 1gt first tab, 2gt second tab etc.

Rearranging tabs

tabm N - move tab to position N (starting from 0)


Tricks

Delete empty lines

g/^$/d

Paste mode

set paste

Disable mouse

set mouse=""

Recover .swp file

Open vim, type:

:recover file.name.swp