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.
| Mode | Purpose | How you get there |
|---|---|---|
| Normal | Navigation and commands (the default mode) | Esc from any other mode |
| Insert | Typing text, like a normal editor | i, a, or o from Normal mode |
| Visual | Selecting text to act on | v (character), V (line), Ctrl-v (block) |
| Command-line | Saving, 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)
| Keys | Moves to |
|---|---|
h j k l | left / down / up / right (the original arrow keys, still muscle memory for many admins) |
w / b | next word / previous word |
0 / $ | start / end of the current line |
gg / G | start / end of the whole file |
Ctrl-f / Ctrl-b | page down / page up |
Editing (Normal mode)
| Keys | Does |
|---|---|
i | enter Insert mode before the cursor |
a | enter Insert mode after the cursor |
o | open a new line below and enter Insert mode |
x | delete the character under the cursor |
dd | delete (cut) the current line |
yy | yank (copy) the current line |
p | paste after the cursor / current line |
u | undo |
Ctrl-r | redo |
. | 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)
| Command | Does |
|---|---|
:w | write (save) the file |
:q | quit (fails if there are unsaved changes) |
:wq or ZZ | save and quit |
:q! | quit and discard changes |
Typing
vim <file>(orvi <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 chainingvimwith 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
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
vimtutoron 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.
Done with this one?
Skipped the challenge? You can mark the lesson complete by hand.