Skip to content

Vim cheat sheet

Every Vim command you need on one page: moving around, editing, copy and paste, search and replace, and how to save and exit. Search it or filter by group.

Stuck? Press Esc, then type :q! and Enter

160 commands

Save and exit

Vim commands: Save and exit
:wSave the file
:w name.txtSave as a new file name
:qQuit (refuses if there are unsaved changes)
:q!Quit and throw away unsaved changes
:wqSave and quit
:xSave (only if changed) and quit
ZZSave and quit, same as :x
ZQQuit without saving, same as :q!
:waSave all open files
:qaQuit all windows
:wqaSave all files and quit
:qa!Quit everything without saving
:w !sudo tee %Save a file you opened without permission to edit it

Modes

Vim commands: Modes
EscBack to Normal mode (press it whenever you are unsure)
iInsert mode, to type text
vVisual mode, to select characters
VVisual line mode, to select whole lines
Ctrl+vVisual block mode, to select a column
:Command-line mode, for commands such as :w
RReplace mode: typing overwrites text

Move the cursor

Vim commands: Move the cursor
h j k lLeft, down, up, right (arrow keys also work)
wStart of the next word
bStart of the previous word
eEnd of the word
W B ESame as w b e, but words are split only by spaces
0Start of the line
^First non-blank character of the line
$End of the line
ggFirst line of the file
GLast line of the file
:42 or 42GGo to line 42
Ctrl+dDown half a screen
Ctrl+uUp half a screen
Ctrl+fDown a full screen
Ctrl+bUp a full screen
}Next paragraph or block
{Previous paragraph or block
%Jump to the matching bracket ( ) [ ] { }
H M LTop, middle, bottom of the screen
zzScroll so the cursor line is in the middle
fxJump to the next x on the line
txJump to just before the next x
;Repeat the last f or t jump
Ctrl+oJump back to where you were
Ctrl+iJump forward again

Insert text

Vim commands: Insert text
iInsert before the cursor
aInsert after the cursor
IInsert at the start of the line
AInsert at the end of the line
oNew line below, and insert
ONew line above, and insert
eaInsert at the end of the word
Ctrl+wIn Insert mode: delete the word before the cursor
Ctrl+nIn Insert mode: autocomplete the word
EscStop inserting

Edit

Vim commands: Edit
xDelete the character under the cursor
XDelete the character before the cursor
rReplace one character (r then the new letter)
cwChange to the end of the word
ciwChange the whole word under the cursor
ci"Change the text inside quotes
cc or SChange the whole line
CChange to the end of the line
sDelete the character and start inserting
JJoin the next line onto this one
~Swap upper and lower case
gUUMake the line UPPERCASE
guuMake the line lowercase
>>Indent the line
<<Remove one indent
==Fix the indent of the line
gg=GFix the indent of the whole file
xpSwap two characters
ddpSwap two lines

Cut, copy and paste

Vim commands: Cut, copy and paste
yyCopy (yank) the line
3yyCopy 3 lines
ywCopy to the end of the word
yiwCopy the word under the cursor
y$Copy to the end of the line
ggyGCopy the whole file
ddCut (delete) the line
3ddCut 3 lines
dwCut to the end of the word
diwCut the word under the cursor
D or d$Cut to the end of the line
dGCut to the end of the file
dggCut to the start of the file
:%dDelete every line in the file
pPaste after the cursor (below, for lines)
PPaste before the cursor (above, for lines)
"+yCopy to the system clipboard (then a motion, or in Visual mode)
"+pPaste from the system clipboard

Visual mode

Vim commands: Visual mode
v V Ctrl+vSelect by character, line or block
ggVGSelect all
oJump to the other end of the selection
awSelect a word
ibSelect inside ( ) brackets
iBSelect inside { } braces
yCopy the selection
dCut the selection
cChange the selection
> <Indent or unindent the selection
u UMake the selection lower or UPPER case
gvSelect the last selection again
Ctrl+v, I, text, EscType the same text at the start of many lines

Search and replace

Vim commands: Search and replace
/wordSearch forward for word
?wordSearch backward
nNext match
NPrevious match
*Search for the word under the cursor
#Same, searching backward
/\cwordSearch ignoring upper and lower case
:nohClear the search highlighting
:s/old/new/Replace the first old on this line
:s/old/new/gReplace every old on this line
:%s/old/new/gReplace every old in the file
:%s/old/new/gcReplace in the file, asking each time (y / n / a / q)
:%s/old/new/giReplace in the file, ignoring case
:10,20s/old/new/gReplace on lines 10 to 20 only
:g/word/dDelete every line containing word
:v/word/dDelete every line not containing word

Undo and repeat

Vim commands: Undo and repeat
uUndo
Ctrl+rRedo
UUndo all changes on the last edited line
.Repeat the last change
:earlier 5mGo back to how the file was 5 minutes ago

Files, windows and tabs

Vim commands: Files, windows and tabs
:e fileOpen a file
:e!Reload the file, throwing away changes
:ExBrowse files in the current folder
:lsList open files (buffers)
:bn :bpNext or previous open file
:sp fileSplit the window horizontally
:vs fileSplit the window vertically
Ctrl+w wMove to the next window
Ctrl+w h/j/k/lMove to the window left, below, above, right
Ctrl+w qClose the window
:onlyClose every other window
:tabnew fileOpen a file in a new tab
gt gTNext or previous tab
:tabcloseClose the tab

Macros and marks

Vim commands: Macros and marks
qaStart recording a macro into register a
qStop recording
@aPlay macro a
@@Play the last macro again
10@aPlay macro a 10 times
maSet mark a at the cursor
'aJump to the line of mark a
`aJump to the exact spot of mark a
:marksList marks
:regShow what is in each register (copy slot)

Settings and help

Vim commands: Settings and help
:set numberShow line numbers (:set nu)
:set relativenumberShow line numbers relative to the cursor
:set nonumberHide line numbers
:syntax onTurn on syntax colouring
:set ignorecase smartcaseSearches ignore case unless you type a capital
:set hlsearchHighlight all search matches
:set wrap / nowrapWrap long lines, or not
:set expandtab tabstop=4 shiftwidth=4Indent with 4 spaces instead of tabs
:help wordBuilt-in help, e.g. :help :s
vimtutorRun in your terminal for a 30-minute interactive lesson

Ctrl+d means hold Ctrl and press d. gg means press g twice. Upper and lower case are different commands.

How to exit Vim (save or quit)

  1. Press Esc to get back to Normal mode. Press it twice if you are not sure.
  2. Type one of the commands below. It appears at the bottom of the screen.
  3. Press Enter.
Commands to save and exit Vim
CommandWhat it does
:wqSave and quit
:q!Quit without saving: your changes are lost
:qQuit; Vim refuses if you have unsaved changes
:wSave and keep editing
ZZSave and quit, no Enter needed

If :wq just types text into your file, you are still in Insert mode: press Esc first. If Vim says E37: No write since last change, it is protecting unsaved work; use :wq to keep it or :q! to drop it. If you opened Vim by accident from git, :wq accepts the commit message and :cq cancels.

Vim modes explained

Vim is confusing at first because the same key does different things depending on the mode. In Normal mode, dd deletes a line; in Insert mode, it types "dd". Vim starts in Normal mode, and Esc always takes you back there.

Vim modes and how to enter them
ModeGet there withUse it to
NormalEscMove around, delete, copy, paste, undo
Inserti, a, oType text like a normal editor
Visualv, V, Ctrl+vSelect text, then copy, cut or indent it
Command-line:Save, quit, search and replace, change settings

The bottom-left corner shows -- INSERT -- or -- VISUAL --; if it shows nothing, you are in Normal mode.

The 15 commands to learn first

You can edit any file with just these. Learn the rest when you need them.

Essential Vim commands for beginners
KeysWhat it does
iStart typing
EscStop typing
h j k lMove left, down, up, right
w / bNext word / previous word
0 / $Start / end of the line
gg / GTop / bottom of the file
xDelete a character
ddDelete (cut) a line
yyCopy a line
pPaste below
uUndo
Ctrl+rRedo
/wordFind word (n for the next one)
:wSave
:qQuit

To practise, run vimtutor in a terminal. It comes with Vim and walks you through the basics in about 30 minutes.

How to search and replace in Vim

To find text, press /, type the word and press Enter; n goes to the next match and N to the previous one. To replace, use the substitute command:

:%s/old/new/g

% means the whole file (leave it out for just the current line), s is substitute, and g replaces every match on a line instead of only the first. Add c to be asked before each change, or i to ignore case.

Example: a config file says colour in 12 places, but one of them is a filename you must not change.

Step 1: type the command and press Enter

:%s/colour/color/gc

Step 2: answer at each match

y = replace · n = skip · a = replace all the rest · q = stop

Result: press y eleven times and n on the filename. Vim reports 11 substitutions on 11 lines. If you get it wrong, u undoes the whole replace in one go.

How Vim commands combine

Most editing commands are an action plus a movement. d (delete), c (change) and y (copy) wait for you to say how far: dw deletes to the next word, d$ to the end of the line. Put a number in front to repeat: 3dd deletes three lines. Once you know a few of each, you know dozens of commands.

Vim actions combined with movements
Then…d (delete)c (change)y (copy)
w: to the next worddwcwyw
iw: the whole worddiwciwyiw
$: to the end of the lined$c$y$
i": inside quotesdi"ci"yi"
same key: the whole lineddccyy
G: to the end of the filedGcGyG

Copy and paste with the system clipboard

y and p use Vim's own clipboard, so text you copy in Vim will not paste into your browser. To use the normal clipboard, put "+ in front: "+yy copies a line, "+p pastes. To copy a whole file, use gg"+yG.

This needs a Vim built with clipboard support: run vim --version and look for +clipboard. If it says -clipboard, install a fuller package (such as vim-gtk3 on Ubuntu) or use Neovim. In a terminal you can also paste with the terminal's own shortcut while in Insert mode.

Frequently asked questions

How do I select all in Vim?

Type ggVG: gg goes to the top, V starts selecting lines and G extends to the bottom. Then y copies, d deletes, or "+y copies to the system clipboard.

How do I show line numbers in Vim?

Type :set number and press Enter. :set nonumber hides them. To keep them on, add the line set number to the file ~/.vimrc.

How do I go to a specific line?

Type :42 and press Enter, or 42G, to jump to line 42. gg goes to the first line and G to the last.

How do I delete everything in a file?

Type :%d and press Enter, or ggdG. Press u straight away if you change your mind.

Sources