Type one * into an empty file, then a few dozen keys, and a Sierpinski triangle grows out of it, row by row:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * No script and no plugin. Just macros, substitutions, visual block and g Ctrl-a. Here are five of these "spells", each with its keys and what the keys are doing. The video plays each one once at full speed, then again step by step:
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.
Every sequence below gives the same result in Vim 9.1 started as vim -u NONE -N, so you can try it in your own Vim. Your settings can change the result (gdefault flips the meaning of /g, for example), so start Vim without your vimrc. Keys are in Vim notation: <Esc>, <CR> for Enter, <C-v> and <C-a> for Ctrl-v and Ctrl-a, and <Space> where a trailing space would be easy to miss. Start each one in an empty buffer, in Normal mode.
1. Binary from 0 to 31
qa:%s/^/0/<CR>ggyGGp:.,$s/^0/1/<CR>q 4@a The macro puts a 0 in front of every line, copies the whole buffer below itself, and changes the leading 0 to 1 in the pasted half. On an empty buffer that gives 0 and 1. Each run adds a digit and doubles the lines: 4, 8, 16, then 32 lines from 00000 to 11111.
Now the decimal numbers on the right:
:%s/$/ = 0/<CR> 2G$<C-v>G$g<C-a> Every line gets = 0, then g Ctrl-a on a block from line 2 down adds 1, 2, 3… to the numbers in the block. The block starts at the end of the line on purpose. If it included the binary digits, Ctrl-a would add to them as a decimal number instead.
Last, pad the widths and move the bottom 16 lines next to the top 16:
:1,10s/$/ /<CR>:11,16s/$/ /<CR> 17G0<C-v>G$dgg$p:17,$d<CR> 00000 = 0 10000 = 16 00001 = 1 10001 = 17 00010 = 2 10010 = 18 ... 01111 = 15 11111 = 31 2. Next month's calendar
January 1, 2027 is a Friday, so the month needs 5 empty cells before it, Sunday to Thursday.
i-5<Esc>yy35p ggVGg<C-a> 36 lines of -5, and g Ctrl-a over all of them gives -4, -3, … 0, 1, … up to 31. The 5 numbers below 1 are the empty cells.
:%s/^/ /<CR>:%s/.*\(...\)$/\1/<CR> :%s/-.\| 0$/ /<CR> Right-align every number 3 characters wide (add spaces, keep the last 3 characters), then turn 0 and the negative numbers into spaces.
ggqa7gJjq4@a 36 is 7 × 5 + 1. 7gJ joins 7 lines into one week, so 5 joins leave 5 weeks and the 31st alone on the last line. It has to be gJ, not J: J removes the leading spaces of each line it joins, and the widths fall apart.
ggO January 2027<CR> Su Mo Tu We Th Fr Sa<Esc> :%s/^ //<CR>:%s/ *$//<CR> January 2027 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Change -5 and the line count, and it works for any month.
3. The primes up to 100
The sieve of Eratosthenes, with # as the "crossed out" mark.
i0<Esc>yy99pggVGg<C-a> 100 zeros become 1 to 100.
2Gqa2jI#<Esc>q48@a 3Gqb3jI#<Esc>q31@b 5Gqc5jI#<Esc>q18@c 7Gqd7jI#<Esc>q12@d Each macro starts on a prime, moves down by that prime, and marks the line. The run counts are just enough to reach line 100. So 4, 6, 8… are crossed out, then 6, 9, 12…, then the multiples of 5 and 7. You can stop at 7: the square root of 100 is 10, and every multiple of 11 or more below 100 has a smaller factor that already crossed it out.
ggI#<Esc>:%s/#.*/./<CR> :%s/^/ /<CR>:%s/.*\(....\)$/\1/<CR> ggqe10gJjq9@e 1 isn't prime, so mark it too. Turn every marked line into a dot, pad everything to 4 characters, and join every 10 lines into one row:
. 2 3 . 5 . 7 . . . 11 . 13 . . . 17 . 19 . . . 23 . . . . . 29 . 31 . . . . . 37 . . . 41 . 43 . . . 47 . . . . . 53 . . . . . 59 . 61 . . . . . 67 . . . 71 . 73 . . . . . 79 . . . 83 . . . . . 89 . . . . . . . 97 . . . 4. The Tower of Hanoi
The moves for 4 disks, from peg A to peg C. You type only the first move:
i[1] A -> C<Esc>"nyy That's the whole solution for one disk. "nyy keeps a copy in register n, the template for "move the biggest disk".
The recursion: to move n disks from A to C, park n-1 disks on B, move the biggest to C, then bring the n-1 disks from B to C. The moves for n-1 disks are already in the buffer, so the macro builds n from them:
qagg"lyG:%s/B/x/g<CR>:%s/C/B/g<CR>:%s/x/C/g<CR> G"np<C-a>"nyy "lpVG:s/A/x/g<CR>gv:s/B/A/g<CR>gv:s/x/B/g<CR>q -
"lyGsaves the current moves in registerl, then B and C swap places, usingxas a placeholder. The same moves now park the disks on B. -
"npputs the biggest-disk move below, and<C-a>raises its disk number by one."nyysaves it for the next round. -
"lppastes the saved moves again, and the selected copy swaps A and B. That's "from B to C". Pressing:in Visual mode fills in the range for you, andgvreselects the same lines.
2@a :%s/^/0. /<CR>ggVGg<C-a> Two more runs give 7 moves, then 15. Number them, and it's solved:
1. [1] A -> B 2. [2] A -> C 3. [1] B -> C 4. [3] A -> B 5. [1] C -> A 6. [2] C -> B 7. [1] A -> B 8. [4] A -> C 9. [1] B -> C 10. [2] B -> A 11. [1] C -> A 12. [3] B -> C 13. [1] A -> B 14. [2] A -> C 15. [1] B -> C 5. The Sierpinski triangle
i*<Esc> qa:%s/.*/&&/<CR>ggyGG$<C-v>gg0f*r<Space>Gpq 3@a The macro doubles every line to the right, copies the picture, blanks the right half of the top copy with a block replace, and pastes the copy below. One triangle becomes three: one on top, two below. 3 more runs, and it's 16 rows. If you know Pascal's triangle, this is it with a star on every odd number.
It's still a right triangle. Put a space after every character, then add leading spaces from the bottom up, one more per line:
:%s/./& /g<CR> Gkqbmz0<C-v>ggI<Space><Esc>'zkq14@b :%s/ *$//<CR> Macro b marks the line, inserts a space at the start of that line and every line above it, then jumps back to the mark and moves up one line. Trim the trailing spaces, and you get the triangle at the top of this post.
Before you install
- It runs on Windows 11 (x64) only.
- NeNe Nib doesn't read
.vimrcfiles or load Vim plugins. -
:gsupports onlydands, and:sort,:normaland the expression register aren't there yet. All five spells above stay inside what it supports.
Try it
Get NeNe Nib on the Microsoft Store (free)
Open an empty file, press Ctrl+Shift+Space, and type one star. If you can grow the triangle in fewer keys, I'd like to see how in the comments.
The earlier video goes from hjkl to a times table and FizzBuzz: A Times Table and FizzBuzz From an Empty File, in a Few Vim Keystrokes.
── Hideyuki Mori 🔗 hideyuki-mori.com

