C.W.K.
Stream
Lesson 06 of 06 · published

The Count Prefix — Multiplying Commands

~8 min · vim, counts, productivity

Level 0Trapped
0 XP0/35 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

Almost every command takes a count

You already saw 5j and 3w. The principle scales: a leading number tells Vim "do this command N times." The count integrates with the operator-motion grammar — counts can sit on either side, or both, of an operator.

Counts on motions

5j down five lines. 3w three words forward. 10l ten characters right. 4) four sentences forward. 2} two paragraphs forward. Anywhere a motion goes, a count multiplies it.

Counts on edits

3dd delete three lines. 5x delete five characters. 2yy yank two lines. 4>> indent four lines. 10p paste the last yank ten times in a row.

Counts on insertions — the surprise

This one always delights newcomers: in Insert mode, you can prefix the entry command with a count, and Vim repeats whatever you typed. 10i-<Esc> inserts ten dashes. 5oTODO<Esc> opens five new lines, each containing TODO. 3aword<Esc> appends "word" three times.

The grammar with two counts

3d2w delete six words. 2c3l change six characters. The two counts multiply. In practice you'll usually pick one count and put it on whichever side feels natural.

Pair counts with relative line numbers. Set relativenumber (Track 4) and the gutter shows the distance from your cursor. Want to delete down to that line? Read the number off the gutter — say it's 7 — and type 7dd. Vim becomes a video game where targets have their distances printed on them.

Code

Counts on motions·vim
5j        " down 5 lines
3w        " forward 3 words
10l       " right 10 characters
4)        " 4 sentences forward
2}        " 2 paragraphs forward
5gg       " go to line 5
Counts on operators·vim
3dd       " delete 3 lines
2yy       " yank 2 lines
5x        " delete 5 characters
4>>       " indent 4 lines
10p       " paste 10 times
d3w       " delete 3 words (count after operator)
2d3w      " delete 6 words (counts multiply)
Counts on Insert entries — the trick·vim
10i-<Esc>           " insert ----------
5o<Esc>             " open 5 blank lines below
3aword<Esc>         " append 'word' three times
20iA<Esc>           " insert 20 capital A's

External links

Exercise

Practice each of these in a scratch file and predict the result before pressing: (1) 20i-<Esc>, (2) 5o<Esc>, (3) 3yyp (yank 3 lines, then paste), (4) 10<Ctrl-a> on the number 5, (5) 2d3w. The goal: the count prefix should feel as natural as the operator+motion.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.