Bram Moolenaar died last week. Didn’t feel right to just write up a Neovim trick without pausing to mention that, so I thought instead this week I’d reflect a little bit on why I use this editor, and why I’m grateful it exists.
The thing about Vim is, it gives me a feeling of mastery over my computer. That’s the real reason I use it. I actually don’t care if it’s “better” than using an IDE. I like the way it feels to use it. Everything I write about in this newsletter is really about justifying my Vim usage — about getting a setup that’s visibly productive enough that IDE people don’t hassle me about switching.
I really like computers, but for a long time I was frightened of and confused by them. I had this idea that code required a lot math — and not the fun math, like logic and proofs, but the boring math, like quadratic equations and trigonometry, that were mostly memorizing things without any idea what they were for. I know, ridiculous, but I tried a couple of times and couldn’t make it past Java compiler errors.
So I didn’t really start learning to code until I was 23, at work. I’d grown up using Windows machines, and first learned to code on Windows. I bought a Mac because all the tutorials I was reading assumed I was using a Mac or a Linux machine, and Mac had this thing called brew
, and that seemed much easier than the hijinks I was having to get up to on Windows to install things like Ruby.
I started using a Unix-based system. And I started to actually understand computers.
There’s a lot of pieces to that. More of the software I was using was open source, so I could go look at the source when I ran into something that wasn’t fully explained in the documentation. More of the software I was using was “small, sharp tools” that interlocked with each other to do more complicated things. I started learning about pipes. I started using the command-line.
I started using Vim.
I’ve been wanting to write something about this for a while and I keep stopping because I can’t explain how cool I felt using command-line tools, and learning to make increasingly complex edits using just the keyboard. I’d grown up kind of wishing I could be an engineer or a hacker, and had resigned myself to the idea that I just wasn’t good enough at math. And then, suddenly, here I was— with a weird mechanical keyboard— typing commands and watching things happen.
It took a couple of years of pairing, but I got pretty good at Vim. I went from barely keeping up with my pairs — who even when they weren’t much older than me had been programming much longer — to consistently impressing them. Even if they were better programmers, I had learned Vim. These days this is always one of the first things that people comment on when they pair with me— “woah, you’re fast on the command-line.”
Basically, Vim cured my imposter syndrome.
(An aside, but— writing this makes me realize that Vim and the command-line let me use my touch typing skills for every interaction with my computer. My typing speed has always been very fast, and has always surprised people, even long before I could do anything with it but write English.)
So that’s one reason that I’m grateful to Bram Moolenaar — for making Vim at all, for taking vi and running with it. But I’m also grateful for how he did it. Open source, for free.
It’s not that I don’t want to pay for tools. It’s that I don’t trust corporations to maintain software. Corporations are genuinely good at a lot of things but maintaining software is not one of them. It’s not even their fault. They go out of business. They get bought. Even if the software is technically open source, if development is controlled by a corporation — or even a group of corporations — there’s no guarantee it’ll be there, ten, twenty, thirty years from now.
And for a lot of things, that’s okay. I don’t mind having to learn new APIs, or new ways to deploy programs that are basically still just Unix processes. But my editor? My editor is practically part of me. I want it to last. I want the effort that I put into mastering it to still be useful ten, twenty, thirty years from now.
The projects that I really trust to stick around for the long haul are open source, have distributed committers, and are maintained by people who have made it their life’s work to maintain that particular program.
I intend to still be writing code in 2053. Vim — or something very much like it — will be there for me.
Automatically format on writes
Okay one quick Vim configuration tip: If you want something to happen every time you save, there’s an API for that.
vim.api.nvim_create_autocmd('BufWritePre', {
buffer = vim.fn.bufnr(),
callback = function()
vim.lsp.buf.format({ timeout_ms = 3000 })
end,
})
In this case I’m having it call vim.lsp.buf.format({ timeout_ms = 3000 }
to automatically run the format command from the active language server but you could put pretty much whatever you want in that callback function.
And using autocommands, you can hook into all kinds of Vim events. The full list is in the Vim documentation under Events. Before writing? After writing? Every time you load a file? When you detect swap file? On startup? After you execute a shell command?
There are even commands for when the user doesn’t press a key for a while. Want to add a screensaver to your Vim configuration? Now you (more or less) know how.