Every Vim session depends on knowing which mode you are in and how to save and quit.
Vim is a modal editor. The same key means different things depending on the mode you are in.
- Normal mode is where Vim starts. Keys are commands:
xdeletes a character,dddeletes a line,uundoes. - Insert mode is for typing text. Press
iin Normal mode to enter it; the status line shows-- INSERT --. - Command-line mode starts when you type
:in Normal mode. You type an Ex command and press Enter. - Visual mode (
v) selects text; it gets its own lesson later.
Press Esc (or Ctrl-[) to return to Normal mode from anywhere. When in doubt, press Esc.
Saving and quitting are command-line commands:
:wwrites the file,:w name.txtwrites to a new name.:qquits, but refuses if there are unsaved changes.:wq(or:x, orZZin Normal mode) saves and quits.:q!quits and throws away changes.
The idea behind modes: most editing time is spent moving and changing existing text, not typing new text. Normal mode makes every key a one-stroke command, so you rarely need the mouse or Ctrl chords. Run vimtutor in a terminal for a 30-minute hands-on tour.
Example
vim notes.txt " open (or create) a file from the shell
i " Normal -> Insert mode
Hello, Vim! " type text
<Esc> " back to Normal mode
:w " write the file
:q " quit
:q! " quit, discarding unsaved changes
:help :w " open the built-in help for any commandRemember
- Normal mode = commands, Insert mode = typing, Esc always goes back to Normal
- :w saves, :q quits, :wq saves and quits, :q! quits without saving
- :help <topic> opens the excellent built-in manual
Watch out for
- Typing text while still in Normal mode: every letter runs a command. Press u to undo the damage.