A guide to the Vim Basics tutorial: opening a file, insert mode and normal mode, saving and quitting with :wq, :q!, and ZZ, moving with hjkl, and searching with /.
Vim is the text editor you will meet on virtually every Linux server. It works differently from ordinary editors: keys are commands first, and text input is a separate mode. That difference is why "how do I exit Vim" became a famous question, and it is the first thing this tutorial settles.
Getting out: :q, :wq, :q!
:q " quit (only if nothing changed)
:wq " write the file, then quit
:q! " quit and discard changes
ZZ " same as :wq (normal mode, no colon)
Press Esc first so you are in normal mode, then type the command and press Enter. If you see E37: No write since last change, Vim is protecting unsaved edits: decide between :wq (keep them) and :q! (discard them).
Three ways out: pick by what should happen to your changes.
When in doubt, press Esc. It always brings you back to normal mode, where these commands work.
Two modes: normal and insert
Right after opening a file, Vim is in normal mode: pressing letters runs commands instead of typing text. Press i to enter insert mode, shown as -- INSERT -- at the bottom, and type as in any editor. Press Esc to return to normal mode.
i " insert mode (type text)
Esc " back to normal mode
The two modes you actually move between, and the keys that switch them.
Moving the cursor
h j k l " left, down, up, right
gg " first line
G " last line
Arrow keys also work, but hjkl keeps your fingers on the home row. gg and G are the fastest way to reach the top or bottom of a file.
Each key moves the cursor one step in its own direction.
Searching
/error " search forward for "error"
n " jump to the next match
Searching is also how experienced users move: / plus a few characters lands the cursor exactly where you want to edit.
These few keys are enough to survive any unexpected Vim session: open, look around, change something, and leave with :wq or :q!.