Skip to content
ohsudo, Home
Linux

Vim Cheat Sheet

Vim keys for people who edit config files on servers: modes, how to exit and save, moving, editing, search and replace, visual mode, splits and sudo saves

Updated November 6, 2026 · Applies to Vim 8+ · Neovim · vi on most servers

Get out

Esc back to normal mode (press it first, whenever you're lost)
:q quit
:q! quit and throw away changes
:w save
:wq save and quit
ZZ save and quit
:w !sudo tee % save a file you opened without sudo

Modes

i insert before the cursor
a insert after the cursor
I insert at the start of the line
A insert at the end of the line
o new line below, and insert
O new line above, and insert
v visual: select characters
V visual: select lines
Ctrl-v visual: select a column
Esc back to normal mode

Move

h j k l left, down, up, right
w next word
b previous word
0 start of the line
^ first non-blank character
$ end of the line
gg first line of the file
G last line of the file
42G line 42 (or :42)
Ctrl-d half a page down
Ctrl-u half a page up
% the matching bracket

Edit

x delete a character
dd delete (cut) a line
5dd delete 5 lines
dw delete to the next word
D delete to the end of the line
yy copy a line
5yy copy 5 lines
p paste after
P paste before
u undo
Ctrl-r redo
. repeat the last change
r replace one character
cw change a word
cc change the whole line
>> indent the line
<< unindent the line
J join the next line onto this one

These combine: an action, then a movement. d$ deletes to the end of the line, yG copies to the end of the file, and ci" changes the text inside quotes.

/text search forward
?text search backward
n next match
N previous match
* search for the word under the cursor
:noh clear the highlighting

Replace

:s/old/new/ first match on this line
:s/old/new/g all matches on this line
:%s/old/new/g all matches in the file
:%s/old/new/gc the same, and ask each time
:10,20s/old/new/g in lines 10 to 20

Add \c to ignore case, as in :%s/old\c/new/g. Patterns use Vim’s own regex syntax, where a \v at the start makes them closer to what you know from other tools.

Files and windows

:e file open a file
:r file insert a file here
:sp file split horizontally
:vsp file split vertically
Ctrl-w w next window
:tabe file open in a new tab
gt next tab
:ls open files
:bn next file

Handy for config files

:set number line numbers
:set paste paste without auto-indent
:set nopaste back to normal
:set list show tabs and line ends
:set ff=unix convert Windows line endings (then :w)
:%s/\s\+$// remove trailing spaces
:!nginx -t run a command, without leaving
:syntax on syntax colors

To keep settings, put them in ~/.vimrc, without the colon:

set number
syntax on
set expandtab shiftwidth=4 tabstop=4

YAML breaks on tabs, so expandtab matters there.

When things look wrong

Other things you may need