Enough to get real work done — go cold turkey after this chapter

Chapter 3 — Basic Editing

Enough to get real work done. After this chapter, you should go cold turkey — use Vim for everything. You'll be slow at first, but that's how you build muscle memory.


3.1 More Ways into Insert Mode

You know i enters insert mode at the cursor. But Vim gives you five more entry points, each putting you exactly where you need to be:

KeyActionUse When
aAppend after the cursorAdding to the end of a word
IInsert at the start of the lineAdding a comment or prefix
AAppend at the end of the lineAdding a semicolon or comment
oOpen a new line belowAdding a new statement
OOpen a new line aboveAdding a header or import
After pressing a at the end of 'greet' and typing '_all' — appended without moving
After pressing a at the end of 'greet' and typing '_all' — appended without moving

Each of these saves you a motion. Instead of pressing i and then navigating to the end of the line, just press A. Instead of pressing i, then Enter to make a new line, just press o. Pick the insert command that puts you where you need to be.

After pressing o — a new line opened below, cursor in insert mode
After pressing o — a new line opened below, cursor in insert mode
Appending text after the cursor
Appending text after the cursor

3.2 Word Motions

Moving character-by-character with h and l is slow. Word motions jump by words — much faster for navigating code.

KeyMotion
wForward to the start of the next word
bBackward to the start of the current/previous word
eForward to the end of the current/next word
geBackward to the end of the previous word
After pressing w four times — cursor jumped word by word
After pressing w four times — cursor jumped word by word

What counts as a "word"? Vim defines a word as a sequence of letters, digits, and underscores — or a sequence of other non-blank characters. So in my_var, that's one word. But in my-var, that's three words (my, -, var) because the hyphen breaks the word boundary.

These four motions will become your bread and butter. You'll use w and b constantly to navigate within lines.

Jumping forward by word with w
Jumping forward by word with w

3.3 Line Motions

Jump to specific positions on the current line:

KeyMotion
0Go to column zero (the very start)
^Go to the first non-whitespace character
$Go to the end of the line
After pressing ^ — cursor on the first non-whitespace character, skipping the indentation
After pressing ^ — cursor on the first non-whitespace character, skipping the indentation

The difference between 0 and ^ matters for indented code. 0 takes you to column zero — the leftmost position. ^ takes you to the first character that isn't a space or tab. In Python or any indented language, ^ is what you usually want.

Jumping to the end of a line with $
Jumping to the end of a line with $

3.4 File Motions

Jump anywhere in the file:

KeyMotion
ggGo to the first line of the file
GGo to the last line of the file
42GGo to line 42
:42EnterGo to line 42 (command-line version)
After pressing G — cursor jumped to the last line of the file
After pressing G — cursor jumped to the last line of the file

These are the motions that make scrolling with j and k feel primitive. Need to check the top of a file? gg. Need the bottom? G. Got a compiler error on line 42? 42G. Three keystrokes and you're there.

Jumping to the top of a file with gg
Jumping to the top of a file with gg

3.5 Deleting Text

Now we meet Vim's most important concept: the operator + motion pattern. d is the delete operator. On its own, it waits for a motion to tell it what to delete:

CommandDeletes
xThe character under the cursor
XThe character before the cursor (like Backspace)
dwFrom the cursor to the start of the next word
d$ or DFrom the cursor to the end of the line
ddThe entire current line
After pressing dw — the first word is gone, text shifted left
After pressing dw — the first word is gone, text shifted left

This is your first taste of Vim's composable grammar: d is the verb (delete), and the motion is the noun (what to delete). Every motion you know works with d: dj deletes two lines, dG deletes to the end of the file, dgg deletes to the top. You already know a dozen delete commands just by combining d with the motions from the last three sections.

After pressing dd — the entire first line is deleted
After pressing dd — the entire first line is deleted
Deleting a word with dw
Deleting a word with dw

3.6 Changing Text

Change = delete + enter insert mode. The c operator deletes the specified text and immediately drops you into insert mode, ready to type the replacement.

CommandChanges
cwChange a word (delete it, start typing)
c$ or CChange to end of line
cc or SChange the entire line
sSubstitute one character (delete it, enter insert mode)
After pressing cw and typing 'fox' — the word was replaced in one smooth motion
After pressing cw and typing 'fox' — the word was replaced in one smooth motion

The c operator follows the same grammar as d: cw changes a word, cj changes two lines, cG changes to the end of the file. Same motions, different operator. The grammar scales.

Changing a word with cw
Changing a word with cw

3.7 Yank and Put (Copy and Paste)

Vim calls it "yank" and "put" instead of "copy" and "paste." The y operator yanks (copies) text, and p puts (pastes) it.

CommandAction
yy or YYank the current line
ywYank a word
y$Yank to end of line
pPut after the cursor (or below for lines)
PPut before the cursor (or above for lines)
After yy then p — the first line was yanked and pasted below
After yy then p — the first line was yanked and pasted below

Two handy tricks with yank and put:

ddp — swap two lines. Delete a line (it's now in the paste register), then put it after the line below. Instant line swap.

ddpSwap current line with the line below

xp — swap two characters. Delete a character, put it after the next one. Fixes typos like 'teh' → 'the' in two keystrokes.

xpSwap two characters
Yanking (copying) a line with yy
Yanking (copying) a line with yy

3.8 Replace

Sometimes you just need to fix one character. rX replaces the character under the cursor with X — without entering insert mode. No mode switch, no Esc needed.

rXReplace character under cursor with X
After pressing rX — the first character was replaced with X, still in normal mode
After pressing rX — the first character was replaced with X, still in normal mode

This is a micro-optimization that adds up. Need to change a comma to a semicolon? r;. Need to fix a capitalization? rG. One motion, one keystroke, done.

Replacing a single character with r
Replacing a single character with r

Quick Reference

KeyAction
aAppend after cursor
IInsert at start of line
AAppend at end of line
oOpen new line below
OOpen new line above
wNext word start
bPrevious word start
eEnd of word
0Start of line (column 0)
^First non-whitespace
$End of line
ggTop of file
GBottom of file
xDelete character
dwDelete word
ddDelete line
DDelete to end of line
cwChange word
ccChange entire line
CChange to end of line
sSubstitute character
yyYank (copy) line
ywYank word
pPut (paste) after
PPut before
rXReplace character with X