At some point you will be logged into a server over SSH[1] with no
GUI[2], a config file needs editing, and the only editor available
is vi. You need to know enough not to be stuck. This page is that
minimum.
Why vi
vi is part of the POSIX[3] standard. Every Unix-like system is required
to have it. On modern Linux, vi is almost always a symlink to Vim[4][5][6]
(Vi IMproved) — but the interface you need to know is the original Vi
interface, because that is what the standard guarantees.
When you SSH into a minimal container, a fresh VPS, a router running
BusyBox, or the Dreamcast development environment — vi is there.
nano may not be. vim by name may not be. The POSIX vi interface
is what you can count on.
Both vi and sed inherit their command syntax from ed[7], the original
Unix editor. This is why vi's :s/old/new/g and sed's s/old/new/g
use identical syntax — they are cousins in a lineage that goes back
forty years. Knowing one helps you recognize patterns in the other.
The three modes
vi is modal. The same key does different things depending on which
mode you are in. This is the source of every "how do I exit Vim" joke,
and it is the entire reason it feels alien at first.
Normal mode — the default. Keys are commands, not characters.
Navigate, delete, copy, paste. This is where vi starts.
Insert mode — keys type characters. This is the mode every other editor is always in.
Command mode — commands prefixed with :. Save, quit,
search-and-replace.
Opening a file
vi main.cYou land in normal mode. Nothing you type will insert text yet.
Entering insert mode
i insert before the cursor
a insert after the cursor (append)
o open a new line below and enter insert modePress i. Type your change. The file has not been saved yet.
Back to normal mode
EscEsc always returns you to normal mode. When in doubt: press Esc.
Saving and quitting
Must be in normal mode. Press Esc first if unsure.
:w write (save)
:q quit — only if no unsaved changes
:wq write and quit
:q! quit without saving, discard everything:q! is the escape hatch. If something went wrong and you want out,
:q! abandons everything.
Navigating in normal mode
h j k l left, down, up, right
w forward one word
b backward one word
0 beginning of line
$ end of line
gg beginning of file
G end of file
:123 jump to line 123Arrow keys work too. hjkl are faster once your fingers know them —
you do not need to learn them today.
Searching
/pattern search forward
n next match
N previous matchThe first time I tried to exit vi I typed :q and nothing happened
because I was in insert mode and :q went into the buffer. Then I
pressed Escape and typed :q and it said the buffer was modified.
Then I tried closing the terminal. The session ended but the next SSH
login opened the recovered file. The correct sequence is Esc to
guarantee normal mode, then :q! to abandon and quit. That is all you
need to not be stuck.
Modal editing feels wrong until it does not. The moment it clicked for
me was when I needed to delete three words: d3w. No mouse, no click,
no selection. One motion, immediate. The model is not insert-by-default;
it is navigate-by-default. Once that shift happens, going back to a
non-modal editor feels slow.
Navigation everywhere
If you have used less to page through a file, these keys are not
new. j/k scroll down and up, / searches, n jumps to the next
match. That was Vi-compatible navigation — less adopted it
deliberately. The same mental model, the same muscle memory.
This carries across the Unix toolset. man runs less internally, so
every manual page responds to the same keys. git log and git diff
open in a pager with identical navigation. Many interactive terminal
programs — htop, mutt, tig — use Vi bindings as their default
because the people who wrote them already knew them.
Once Vi navigation is in your fingers, a large part of the terminal already speaks the same language.
The minimum you need
vi filename— open a filei— enter insert mode- Type your change
Esc— return to normal mode:wq— save and quit
That is it. Everything else is optional until you decide you want more.
Going further
The title of this page says surviving. If you want more than survival, run:
vimtutorIt is built into Vim — an interactive tutorial that takes about thirty
minutes. You will come out knowing motions, operators, and text
objects: the layer that makes Vim genuinely fast. The reputation is
that Vim has a brutal learning curve. vimtutor disagrees.