Getting started with Vim (and how to quit it)

general / published / guide #21 / ≈ 4 min read

Vim is the editor people either swear by or run from. It's on almost every Unix-like system by default, it's ridiculously fast once it clicks, and it's famously confusing for the first hour. This guide gets you past the first hour.

Before anything else: if Vim ever opens accidentally and you're panicking, press Esc, then type :q! and hit Enter. That quits without saving. You can now run away. We'll come back to why that works.

The big idea: modes

Most editors have one mode — you type, and characters appear. Vim has several. The same key does different things depending on which mode you're in. This sounds awful, but it's the source of Vim's speed: in normal mode the letter keys are commands, not letters, so you can edit text without ever reaching for arrow keys or modifiers.

The four modes you'll meet:

  • Normal mode — what you start in. Keys are commands. Press Esc to return here from anywhere.
  • Insert mode — what every other editor is permanently in. Keys produce characters. Enter with i (or a, o).
  • Visual mode — for selecting text. Enter with v.
  • Command-line mode — for commands that start with : (save, quit, replace, etc.).

Step 1: Open a file and quit

vim hello.txt

You're in normal mode. To leave Vim without saving:

  1. Press Esc (to make sure you're in normal mode — pressing it twice is fine and never hurts).
  2. Type :q — you'll see it appear at the bottom of the screen.
  3. Press Enter.

If Vim complains "no write since last change," it means you've typed something. To quit anyway, use :q! — the ! means "yes, I really mean it."

Practice this five times before moving on. The fear of getting stuck in Vim disappears the moment exiting feels automatic.

Step 2: Type some text

From normal mode, press i. You're now in insert mode — the bottom of the screen says -- INSERT --. Type some text. Press Esc when you're done. You're back in normal mode.

Three ways to enter insert mode (you'll use all three eventually):

  • i — insert before the cursor.
  • a — append after the cursor.
  • o — open a new line below and start there.

Step 3: Save

From normal mode, type :w and press Enter. ("w" for "write.")

Common combinations:

  • :w — save.
  • :q — quit.
  • :wq — save and quit.
  • :x — same as :wq, fewer keystrokes.
  • ZZ — same again, no colon at all (capital Z, capital Z).
  • :q! — quit, discard changes.

Movement in normal mode

Arrow keys work, but the real Vim speed comes from never leaving the home row:

h j k l        # left, down, up, right
w              # forward one word
b              # back one word
0              # start of line
$              # end of line
gg             # top of file
G              # bottom of file
:42            # jump to line 42
/word          # search forward for "word"
n              # next match
N              # previous match
?word          # search backward

None of this needs a modifier key. That's the point — your fingers stay where they were while typing.

Editing in normal mode

x              # delete the character under the cursor
dd             # delete the whole line (cuts it into Vim's clipboard)
yy             # yank (copy) the whole line
p              # paste below
P              # paste above
u              # undo
Ctrl+r         # redo
.              # repeat your last edit

Many commands take a count: 5dd deletes 5 lines, 3yy copies 3 lines. Many take a motion: dw deletes to the end of the word, d$ deletes to end of line, dG deletes from here to end of file.

Vim is full of these little compositions: verb + motion. Once it clicks, you can edit text very, very quickly.

Find and replace

To replace every "foo" with "bar" in the file:

:%s/foo/bar/g

Read it as: : command mode, % all lines, s substitute, /foo/bar/ from foo to bar, g all occurrences in each line.

To confirm each one:

:%s/foo/bar/gc

Visual mode for selecting

Press v. Move with h j k l to extend the selection. Then act on it:

  • d delete the selection
  • y yank (copy) it
  • > indent
  • < outdent

V (capital) selects whole lines instead of characters. Ctrl+v selects a rectangular block — surprisingly useful for editing columns of text.

A minimal .vimrc

Vim's config file lives at ~/.vimrc. Create it with a few sane defaults:

set nocompatible
set number relativenumber
set ruler showcmd
set incsearch hlsearch ignorecase smartcase
set tabstop=4 shiftwidth=4 expandtab
syntax on
filetype plugin indent on

What this gets you: line numbers, live search as you type, case-insensitive search that becomes case-sensitive when you type a capital, four-space indentation, and syntax highlighting.

Run vimtutor — really

vimtutor

It ships with Vim. It's a 30-minute interactive lesson inside Vim itself, walking you through every concept above with practice exercises. It's the single best thing you can do to learn Vim, and somehow most people never run it.

The shortest summary

  • You start in normal mode. Letter keys are commands.
  • Press i to type text. Press Esc to stop.
  • :w saves. :q quits. :wq does both. :q! discards.
  • Movement: h j k l. Search: /word.
  • Delete: dd. Copy: yy. Paste: p. Undo: u.

That's 80% of Vim. The remaining 20% will reveal itself over years and never stop being a small joy.

← back to index