Skip to content
Intermediate LinuxLesson 19 of 27 4 min read

Vim Editor Mastery

Modes, motions, and the editing workflow behind the editor on almost every Linux box.

On this page7 sections

You've already met nano for quick edits. vim (and its ancestor vi) is the editor you'll find preinstalled on almost every Linux system on Earth - minimal containers, embedded devices, and freshly provisioned servers all ship it, often without nano. Learning enough vim to comfortably edit a config file over SSH is one of the highest-leverage skills in this entire course.

Why vim feels strange at first

Vim is a modal editor: the same keys do different things depending on which mode you're in. That's the whole design - once memorized, editing becomes a sequence of small, composable commands instead of constant mouse-and-arrow-key movement.

ModePurposeHow you get there
NormalNavigation and commands (the default mode)Esc from any other mode
InsertTyping text, like a normal editori, a, or o from Normal mode
VisualSelecting text to act onv (character), V (line), Ctrl-v (block)
Command-lineSaving, quitting, search-and-replace: from Normal mode

The single most common beginner mistake is typing text while still in Normal mode - it looks like nothing is happening (or worse, like random commands are firing) because every letter is being read as a command, not as text.

Moving around (Normal mode)

KeysMoves to
h j k lleft / down / up / right (the original arrow keys, still muscle memory for many admins)
w / bnext word / previous word
0 / $start / end of the current line
gg / Gstart / end of the whole file
Ctrl-f / Ctrl-bpage down / page up

Editing (Normal mode)

KeysDoes
ienter Insert mode before the cursor
aenter Insert mode after the cursor
oopen a new line below and enter Insert mode
xdelete the character under the cursor
dddelete (cut) the current line
yyyank (copy) the current line
ppaste after the cursor / current line
uundo
Ctrl-rredo
.repeat the last change

Most of vim's power comes from combining a count, an operator, and a motion: 3dd deletes three lines, d$ deletes to the end of the line, dw deletes to the start of the next word.

Search and replace

/pattern          search forward for "pattern"
n                  jump to the next match
N                  jump to the previous match
:%s/old/new/g      replace every "old" with "new", in the whole file

That last one is the command you'll type most often once you're comfortable - it's the same substitution idea as sed's s/find/replace/g, because vim and sed share the same lineage.

Saving and quitting (Command-line mode)

CommandDoes
:wwrite (save) the file
:qquit (fails if there are unsaved changes)
:wq or ZZsave and quit
:q!quit and discard changes

Typing vim <file> (or vi <file>) on its own line in the practice terminal opens a real, interactive vim session - the modes and motions above all work for real. Two simplifications versus real vim: Visual mode (v/V/Ctrl-v) always selects whole lines here, like real vim's Visual Line mode specifically, and . (repeat last change) isn't implemented. Piping or chaining vim with another command falls back to a read-only preview instead, since a full-screen app doesn't compose with pipes in a way that means anything.

Try it

Run these in the practice terminal (opens in a new tab). It opens in a new tab, so you keep your place here.
bash
echo "first draft" > notes.txt
vim notes.txt

That opens the real editor. From there: press i, type a sentence, Esc, then :wq to save and quit - or work through the motions and editing commands from this lesson directly. Every Linux system also ships a free, interactive, 30-minute tutorial for the real thing: run vimtutor.

Further reading

  • "Practical Vim" by Drew Neil (Pragmatic Bookshelf) - the best modern book on thinking in vim's language of operators and motions.
  • Run vimtutor on any real Linux machine - a free, interactive, 30-minute tutorial that ships with vim itself.

Challenge

Create notes.txt containing the line first draft, then append the line reviewed to it - in one line, without opening an editor. Open the file in vim in the practice terminal afterwards to check your work.

Want to experiment first? Try things in the practice terminal (opens in a new tab), then enter the command that solves the task here.

Quiz

Lock in the concepts. Score at least 80% to mark the lesson complete.

0 of 3 answered

Done with this one?

Skipped the challenge? You can mark the lesson complete by hand.

Your notes

Kept on this browser only.