Vim Life

Vim Life

The Worst Ever Knuckle Tattoos, or Just a Preposterous Misuse of Macros?

I love macros.

For me they are one of the ESSENTIAL parts of the Vim experience: useful, intuitive1, and POWERFUL. Macros are so powerful, in fact, that I used them to implement Conway’s Game of Life.

Try It Out

You can download the file here. (1.8KB)

The Game of Life stretches the capabilities of Vim’s macros to EXTREME degrees2. In order to ensure OPTIMUM performance, you should instruct Vim to IGNORE your configuration when loading the file:

vim -Nu NONE vimlife.txt

My TOP TIP for editing the starting pattern is to do so exclusively in Replace mode. Press R to start editing, x and Space to resuscitate/kill cells, and use the arrow keys (Heresy!) to move around.

When you're done, press Esc to return to normal mode, and then type ggwyW@0 to run the game!

The Rules

When writing it, I imposed some arbitrary restrictions on myself:

No Vimscript
Allowing Vimscript with all its commands, variables, functions, and looping constructs would have made it way too easy. Plus it’s been done before3. OLD HAT!
No Expression Register
Similarly, allowing a feature that provides straightforward access to ARITHMETIC CALCULATIONS feels like CHEATING. Simple motion and editing commands only, please!
No Literal Control Characters
My solution keeps it strictly ASCII 32-126, BABY! No hijinks involving CTRL-commands or ESC characters allowed!
Must Fit in a Standard 80×24 Terminal
I don’t have any particular rationale for this rule. I just thought it would be NIFTY.
The File Must Include Instructions
Really, this is just being POLITE.

How It Works

When you record a macro, Vim stores your keystrokes in a register. But you can also add text DIRECTLY into a register by yanking it.

So, the commands to start the game simply move the cursor to the start of the file gg, move forward a word w, yank a WORD yW (which stores it in the "0 register) and then execute the contents of that register as a macro @0.

The macro then proceeds by yanking various parts of the buffer into registers, creating new macros and executing them as it goes.

Even More Gory Details

If you really want to know how it works, Pelafina H Lievre (the creator of the sed Game of Life that originally inspired me to make this) wrote this wonderful analysis:

But Why Would You Do This?

Humans are driven to explore the unknown, discover new worlds, push the boundaries of our scientific and technical limits, and then push further. The intangible desire to explore and challenge the boundaries of what we know and where we have been has provided benefits to our society for centuries.

Human space exploration helps to address fundamental questions about our place in the Universe and the history of our solar system. Through addressing the challenges related to human space exploration we expand technology, create new industries, and help to foster a peaceful connection with other nations. Curiosity and exploration are vital to the human spirit and accepting the challenge of going deeper into space will invite the citizens of the world today and the generations of tomorrow to join NASA on this exciting journey.

Did You Seriously Just Copy and Paste That from the NASA Website?

Um…

If you enjoyed this, why not check out my Vim plugins.

Comments? Profound admiration? Money? Macros you’ve recorded that you’re particularly proud of? Email them to me at: esc@normalmo.de, tweet @normalmo_de, or go ask me a question on the Vi/Vim Stack Exchange.

While You Were Gone

1: The great thing about learning the macro-recording feature is that you already know how to use it. Often there are other ways to carry out similar edits with ex commands or more complex Vimscript, but that requires LEARNING those commands. But every Vim user already knows a BUNCH of normal mode commands. So all you need to know to record a macro is how to start a recording (press q followed by any letter), how to stop a recording (also q), and how to playback your macro, (press @ followed by your letter). If you can do a repetitive change with your normal editing operations, you can invariably do it with a macro.

2: Fun fact: I was planning to use asciinema for the embedded terminal recording in this post. But the asciicast file weighed in at 166MB, (a touch over their 5MB upload limit), and even if I’d decided to eat the S3 fees and self-host, it would apparently have SNAFFLED something in the order of 1.5GB of RAM on your computer when you tried to play it. The demo may only be 45 seconds long, but it is doing a LOT.

3: There's even a version inside Vim Try the command :e $VIMRUNTIME/macros/life/life.vim.