Modes: insert, normal and quitting

Vim · Essentials · 8 min

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: x deletes a character, dd deletes a line, u undoes.
  • Insert mode is for typing text. Press i in 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:

  • :w writes the file, :w name.txt writes to a new name.
  • :q quits, but refuses if there are unsaved changes.
  • :wq (or :x, or ZZ in 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 command

Remember

  • 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.

Check yourself

  1. You opened a file, typed some text and now want to leave without saving. What do you type?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: vimhelp.org/starting.txt.html