vi / Vim Commands: The Cheat Sheet You Actually Need

The vi and Vim commands you actually need – modes explained, save and quit, navigation, search and replace, undo, and copy-paste, with a printable table.

Last updated: July 2026

You’re SSH’d into a server, typed vi config.php, and now nothing responds the way you expect. That’s because vi is modal — understand two modes and everything clicks.

The two modes

  • Normal mode (default): keys are commands. Press Esc any time to get back here.
  • Insert mode: keys type text. Enter it with i (insert here), a (after cursor), o (new line below).

Everything below is typed in Normal mode.

Save and quit — the ones everyone googles

CommandAction
:wSave
:wq or ZZSave and quit
:qQuit (fails if unsaved changes)
:q!Quit and discard changes
:w newnameSave as a new file

Stuck with “E45: ‘readonly’ option is set”? You opened a root-owned file without sudo. Save anyway with:

:w !sudo tee %

Moving around

CommandAction
h j k lleft / down / up / right (arrows work too)
0 / $start / end of line
gg / Gfirst / last line
:42 or 42Gjump to line 42
Ctrl-f / Ctrl-bpage down / up

Editing

CommandAction
xdelete character
dddelete (cut) line — 5dd cuts 5 lines
yycopy line — 5yy copies 5
p / Ppaste below / above
uundo
Ctrl-rredo
.repeat last change

Search and replace

/error          " search forward for 'error'; n = next, N = previous
?error          " search backward
:%s/old/new/g   " replace every 'old' with 'new' in the file
:%s/old/new/gc  " same, but confirm each one
:noh            " clear search highlighting

Three habits that make vi pleasant

  1. set number (or in ~/.vimrc) shows line numbers — invaluable when an error says “line 87”.
  2. Learn ci" — change inside quotes. Cursor anywhere in "some value", type ci", and you’re editing a fresh empty string. Works with ({' too.
  3. vimtutor in your terminal is a 25-minute interactive lesson; it pays for itself the same day.

On modern distros vi is usually Vim (or its slim build); every command above works in both. For scheduling the scripts you just edited, see the crontab guide.

Comments

comments