A Times Table and FizzBuzz From an Empty File, in a Few Vim Keystrokes

2026-12-03来源 dev.toEnglish

Start with an empty file. Type nine zeros, paste the line eight times, then repeat one short command per column, and the zeros turn into a 9x9 times table. No script, no plugin, and the only numbers you put in the file are zeros.

That's the last chapter of a 12-minute video I made about Vim editing. It starts at hjkl and builds up, one step at a time, to tricks like this:

The editor in the video is NeNe Nib, a free text editor for Windows 11 that I'm building. It works like Notepad, and Ctrl+Shift+Space switches the same tab to Vim editing. There's no .vimrc to write and nothing else to install.

Every key sequence below also gives the same result in Vim 9.1, so you can try it in your own Vim. One catch: in gVim on Windows with mswin.vim loaded, Ctrl-v pastes and Ctrl-a selects all, so use terminal Vim or vim -u NONE.

A times table from zeros

Type a row of nine zeros, copy it, and paste it eight times:

i0 0 0 0 0 0 0 0 0<Esc>yy8p 

Now you have nine rows of zeros. For each column, move to it, select it down all nine rows with Ctrl-v, and add with a count. Start with the rightmost column and work left:

gg016l<C-v>8j9g<C-a> gg014l<C-v>8j8g<C-a> gg012l<C-v>8j7g<C-a> gg010l<C-v>8j6g<C-a> gg08l<C-v>8j5g<C-a> gg06l<C-v>8j4g<C-a> gg04l<C-v>8j3g<C-a> gg02l<C-v>8j2g<C-a> gg0<C-v>8jg<C-a> 

One line per column. gg0 goes to the top left, and 16l moves to the 9th zero. Each zero plus its space is two characters, so the count before l drops by 2 for each column to the left.

g Ctrl-a adds a growing amount down the selection: 1 to the first line, 2 to the second, and so on. A count sets the step, so 9g<C-a> adds 9 to the first line, 18 to the second, 27 to the third. That's the 9 column of the table. The first column gets a plain g<C-a>, which gives 1 to 9:

1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 ... 9 18 27 36 45 54 63 72 81 

Why right to left? A number that grows to two digits pushes everything to its right. Working from the right, the columns still waiting on the left haven't moved, so the counts 16, 14, 12 and so on still land on them.

FizzBuzz without dividing

The second trick starts from an empty file and a single 1.

1 to 100

Record a macro that copies the line and adds 1, then play it 98 times:

i1<Esc> qayyp<C-a>q 98@a 

The first line and the recording itself make two lines, so 98 more runs give 100.

Fizz on every third line

Go to line 3, record "append Fizz, move down 3 lines", and play it 32 times:

3GqbA Fizz<Esc>3jq 32@b 

Recording handles line 3, and the 32 runs handle lines 6, 9, and so on up to 99. Use exactly 32: from line 99, 3j stops on line 100 instead of failing, so a 33rd run would put a Fizz on 100.

Buzz on every fifth line

The same idea from line 5. Recording plus 19 runs covers lines 5, 10, … 100:

5GqcA Buzz<Esc>5jq 19@c 

Clean up

Lines like 15 now read 15 Fizz Buzz. Join the two words, then delete the number on every line that has a word after it:

:%s/z B/zB/ :%s/^[0-9]\+ // 

The second substitute only matches a number followed by a space, so lines with just a number keep it. The rest become Fizz, Buzz or FizzBuzz, and all 100 lines match the correct output. Nothing ever checks whether a number divides by 3 or 5. The macros just step through the lines at that interval.

The chapters before

If those tricks feel like a jump, the video builds up to them in this order:

  • Motions: w, b, e, 0, ^, $, f, t, %, *, and Ctrl-o to jump back
  • d, c and y with any motion, and . to repeat
  • Text objects: ciw, ci", ci(, dap
  • Visual block: add the same text to the start or end of many lines
  • Registers and marks
  • Macros, g Ctrl-a, cgn, :g and :v, and substitutions that swap parts of a line

Before you install

  • It runs on Windows 11 (x64) only.
  • NeNe Nib doesn't read .vimrc files or load Vim plugins.
  • In the current release (1.2.0), Vim's indent commands (>> and friends) insert an 8-column tab, and that can't be changed yet.
  • :g supports only d and s, and :sort isn't there yet.

Try it

Get NeNe Nib on the Microsoft Store (free)

Open an empty file, press Ctrl+Shift+Space, and build the times table. If you know a shorter way to do it, I'd like to see it in the comments.

── Hideyuki Mori 🔗 hideyuki-mori.com

原文:https://www.a8q9.cn/en/article/a-times-table-and-fizzbuzz-from-an-empty-file-in-a-few-vim-keystrokes-5d0a27.html

相关文章