Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 36 ยท Projects and Practice

Practice: Vim Golf Challenges

Practice Vim with golf-style challenges: transform text with the fewest keystrokes, compare solutions, learn the counting rules and check your answers with a stand-in verifier.

Vim golf is a game: given a start text and a target text, reach the target with as few keystrokes as possible. It is a playful way to learn the full vocabulary, because the best solutions use text objects, counts, . repeat and clever motions you might otherwise skip.

Speed is not the main goal for learning, though. A shorter solution is only better if you understand it and could type it again. Treat golf as a puzzle that exercises the editing language, not a competition to memorize tricks.

Counting keystrokes

Common rules (check the rules of the site you play on):

  • Each key you press counts as one: d, w, 3, Esc and Enter are one each.
  • Control keys count once: Ctrl-v is one stroke.
  • A command-line command counts every character, including : and Enter. That is why :%s/a/b/g<CR> (14 keys) is costly for tiny changes.
  • Start in normal mode with the cursor at the top-left, unless a challenge says otherwise.

Below, each challenge shows the start text, the target and a sample solution. Cover the solution and try it yourself first.

Challenge 1: remove a word

Start: The quick brown fox

Target: The brown fox

Solution: wdw (3 keys). w moves to quick, and dw deletes the word together with the space after it.

Challenge 2: transpose two letters

Start: teh

Target: the

Solution: lxp (3 keys). l moves onto the e, x deletes it (it goes into the unnamed register) and p puts it back after the cursor, which now sits on h. The same xp idea swaps any two neighbouring characters. For two neighbouring words, tricks like dwwP depend on the text around them, so test those on the real text.

Challenge 3: change inside quotes

Start: name = "old value"

Target: name = "new"

Solution: ci"new<Esc> (7 keys). With the cursor before the quotes, a quote text object looks forward on the line for the next quoted string, so no f" is needed. Text objects are the biggest golf win.

Challenge 4: number a list

Start:

TEXT
apple
pear
plum

Target:

TEXT
1. apple
2. pear
3. plum

Solution A (one command, works for any length): :%s/^/\=line('.').'. '/<CR>. The \= in the replacement evaluates an expression, here the line number.

Solution B (golf style): first put 0. in front of every line with :%s/^/0. /<CR>, then go to the top (gg), select the first column of digits with Ctrl-v and jj, and press g<C-a>. In Visual mode g Ctrl-a adds 1 to the first number of the first line, 2 to the second, and so on, which turns the zeros into 1, 2, 3.

Challenge 5: delete every second line

Start: ten lines numbered 1 to 10.

Target: lines 1, 3, 5, 7 and 9 remain.

Solution A: qajddq4@a (9 keys). Recording jdd once deletes line 2; 4@a repeats it four more times for lines 4, 6, 8 and 10.

Solution B: :g/^/+d<CR>. For every line, :g runs +d, which deletes the line below it. A line that was already deleted is skipped, so the lines 2, 4, 6, 8 and 10 disappear.

JS Stand-in: check a solution against the target

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Stand-in: count the keys of a solution

Press Run (or Ctrl + Enter) to see the console output here.

No output.

More practice ideas

  1. Play real golf: the community site vimgolf.com posts challenges with an online scoreboard; the leaderboard solutions are an excellent source of idioms. Read them after your own attempt.
  2. Edit without the arrow keys and without the mouse for a week. Every time you reach for them, ask what motion would do it.
  3. Drill text objects: ci(, da", yit, vap, ciw on real code.
  4. Learn one new motion per day: %, *, }, ]], gi, g;, H M L, zz.
  5. Rebuild your muscle memory with .: whenever you do something twice, ask whether . or a macro does it in fewer keys.
  6. Use vimtutor again after a month, and :help user-manual in order: it is a complete and well-written book.
  7. Keep a "Vim notes" file with the three new tricks you learned each week.

Self-test

A mixed bag; answer without a cheat sheet.

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which keys change the text inside the double quotes from anywhere on the line (inside the pair)?

    ci" deletes the text between the quotes and starts insert mode; di" deletes without inserting.
  2. 2. You need to repeat the last change on another word. Which key repeats it?

    The dot command repeats the last change; & repeats :s, ; repeats f/t, @: repeats an Ex command.
  3. 3. What does :g/^/m0 do?

    Each line is moved to the top in turn, so the last line ends up first and the order is reversed.
  4. 4. Why can a shorter golf solution be worse for learning?

    Practice for understanding first: a solution you can explain and repeat transfers to real work.

Summary

  • Vim golf is a puzzle game that rewards counts, text objects, . and clever motions.
  • Count every key, including Esc, Enter and each character of a : command.
  • Always test a solution on the real text; an idea that works on one line may fail on another.
  • Learn from other solutions after your own attempt, and aim to understand before you shorten.

You have now covered buffers, configuration, Neovim, workflows and practice. Return to the Vim and Neovim track page to continue with any chapter.

Next: Vim and Neovim