Why I use vim

2023-06-24

#editors 

In my opinion, a craftsman should strive for mastery of the tools of their trade. This applies as much to digital products as it does to physical goods. As programmers, we use physical tools like keyboards and monitors, but also digital tools like a code editor.

I’d like to take some time explaining why vim is a great editor and why I’ve chosen to use it for the last decade.

It’s fun

The main reason why I use vim, aside from all the valid reasons below, is that it’s fun to use. To me, the process of manipulating code in vim, learning how to get faster, and exploring hidden features is a blast. Sometimes writing code can be a bit boring and having a way to spice things up can help keep your brain engaged.

Find a job you enjoy doing, and you will never have to work a day in your life.

Vim is a “modal” editor: when you are not in “insert” mode, each key performs some sort of action or movement. This means that things like moving around in a file or updating code become a bit of a game / puzzle. It’s an experience I haven’t been able to find in many other code editors.

Vim may appear simple or limited at first glance, but it has a surprising amount of depth and it can take a long time to gain mastery. There is always room to learn and level up your efficiency and, in my opinion, that’s a good thing. Life would be boring without challenge.

It’s efficient

The reason you are most likely to find in these types of posts is that vim is efficient. By keeping your hands on they keyboard, instead of reaching for a mouse, you actually save yourself time. It might seem silly to talk about saving a fraction of a second, but consider just how many times you reach for your mouse in a day of coding. As an additional benefit, keeping your hands on the keyboard may help save you from repetitive stress injuries.

Vim’s “normal” mode allows you to use every alphanumeric key for another purpose. This makes moving around in files a breeze. Here’s some examples:

There are a lot of options, all of them only require one or two key presses, many of which are centered on, or near, the home row. This efficiency allows a skilled user to be surgical with their edits: get into the file, go right to where you need to be, and make a quick change. If speed was a goal (it shouldn’t be), you can find it here.

It’s powerful

Along the same lines, vim is incredibly powerful at manipulating “text objects”. Vim treats characters, words, paragraphs, functions, etc. as objects. When you want to perform an action like, for example, delete two words, you type d2w. Action (d), amount (2), text object (w). Once you understand this “grammar” and start thinking about your files as a collection of text objects, you can start updating code in ways not possible with a traditional non-modal editor.

Another feature that is not common in other editors is “macros”. I love these things. The idea is that you record keystrokes into a register (more on that in a second). Once you’re happy with your macro, you can “replay” it. This comes in handy whenever I have a lot of text (e.g. output from an API call) that I need to manipulate. I simply record a macro of me formatting one line, then I replay it on every line in the file.

Registers are another great feature: you can store edits, macros, recent commands, or clipboard contents in one of the 20ish registers vim provided. You can think of these as multiple clipboards. It’s very helpful for keeping a few things around for later pasting.

Vim supports undo / redo as well as jumping forward and backward by an amount of time. I can run :earlier 5m to see what the file was like 5 minutes ago. So neat! There are also a number of plugins that provide rich undo trees, if you wish to extend vim in that way.

Speaking of which, there is a robust ecosystem of plugins you can install for vim. It’s not as streamlined as installing a plugin on VSCode, but it’s still fairly painless and there are package managers that handle the heavy lifting. And if you want to completely customize your experience, you can write your own code that can be run in vim. This is where neovim really shines: it’s script-able with lua (which is an absolute delight to work in).

It’s ubiquitous

The other common reason why people prefer vim is that it’s EVERYWHERE. You’ll find some version of vim (or it’s predecessor vi) on almost any personal computer (mac, windows, linux, bsd). If you SSH into a server, vi(m) will almost certainly be there, unlike any graphical editor. Nothing, not even emacs, can compete with vim in it’s ubiquity. It’s even the default for writing git commit messages!

Due to the efficient and powerful nature of vim’s modal editing, a lot of software have embraced a “vim mode” option for entering text or navigating around. VSCode, for example, has a great plugin for vim mode (which I have had the pleasure of contributing to). The same can be said for obsidian, evernote, notion, etc. There are plugins for every major browser that mimic the navigation movements too.

In short, vim the editor is on almost all machines and vim motions are an option on a lot of other software.

Closing thoughts

The above reasons, while not exhaustive, explain why I much prefer working with code and prose in vim.

With the exception of the last, these reason are why I am also interested in exploring another modal editor: Helix. I plan on doing a follow-up post once I’ve had a few weeks with that editor.