Sblog

Vim - because fast is fun

Date: 2024-02-14

What makes fast editing fun

Improving at anything can be fun, but improving at something that you do every day feels even more satisfying because you get to apply it all the time. I've always loved watching videos of experts doing their craft so trying to get better at the most basic thing I do every day seems to follow logically.

After learning a better way to do something, doing it the old way feels slow and boring. When I think about the days when I was using the arrow keys I imagine myself like a boomer using a computer.


You can start inside your favorite editor

As far as I'm aware all the major editors have a plugin that allows you to use at the very least the basic vim motions. You can stay in the comfort of your VS C**e or your Webstorm and get a taste of the sweet vim cake. This could be as far as you want to go but Neovim can get you so much more.

I started my journey this way with the vim plugin for VS C**e. I had heard about how fast you can become with vim motions but I wasn't ready to commit to switching my whole editor just yet. At the time I thought of vim as an editor only that you get by default on all the servers that I deploy to/administrate. While the fact that it is found on (practically) all Linux machines is true, I was not aware at the time about the rich ecosystem of neovim which makes neovim more than just an editor (more about this below).


Basic motions get you further than you think

You'd be surprised just how helpful even the basic motions from navigating words with w, e, and b, to fine-grained movement with hjkl, to changing everything inside of quotes/brackets etc, to navigating up and down by paragraphs with { and }.

Let's start with the w, e, b motions. If you compare them to the alternative in other editors - it's either ctrl + arrow keys or the mouse. With the ctrl + arrow keys, you have to move your hand away from the home row which slows you down. The mouse is obviously even slower because you have to move your hand away from the keyboard entirely and then move it back to type. The vim motions move you back and forth between words that are either space or punctuation delimited, but you also get the additional versions W/E/B1 which only move between space-delimited word boundaries.

Next, let's look at one of the basic vertical motions - the paragraph motions { and }. These motions jump to the previous/next empty line. If you're grouping lines of related code, this is very useful for going up and down your smaller logical groupings of LoC. I think it might be possible to do this with other editors as well but at least in VS C**e you don't have a default keybind for it.

If you want to move in more granular ways, you have the arrow key equivalent of the hjkl. The reason why they're faster to use is the same again - you don't have to move your hand away from the home row.

Finally, the last motions that I'll mention in the basics section are the f and t mappings (and their F/T variants). These allow you to jump to the next character that follows them with the difference that f jumps on the next character while t jumps right before it. The shift modifier variants of these motions allow you to do the same but backward. You can go backward and forward to the next result of the "single character search" with the , and ; keys respectively. By default, the f/t motions only work on the current line but you can change that with a very good neovim plugin - flash.nvim2.




  1. In vim help/manuals you'll usually see keybinds that use the shift modifier shortened to their capitalized version, or in the case of shift + [ or ] as { and } respectively.
  2. The plugin is a powerhouse of movement functionality options of which I personally only use some of the more basic ones.

Intermediate concepts that get you even further beyond

Once you start getting used to the basics you can start combining them together, which is probably one of the most powerful editing tricks that you can do in vim. Some of the common chords have become like chords that are ingrained in my muscle memory.

The [c]hange, [y]ank and [d]elete mappings compose very well with most motions. One example use of this is ci( which deletes everything in the current set of parenthesis and drops you in insert mode(or the nearest pair of parenthesis after the cursor if you're not already inside one). The same trick works with basically all enclosing pairs (quotes, square brackets, etc). Another chord I like is combining the c/d/y keybinds with the f/t motions to combine a change with a line search. A concrete example of that would be ct; which deletes everything from the cursor up until the next semicolon and drops you in insert mode.

Most commands and motions can be prefixed with a number that repeats the motion that many times. This is especially useful for relative vertical jumps up and down with number + j/k. You'd obviously need to know how many lines you want to move up or down which is why you can turn on relative line numbers which solves that problem.

Macros! You can record and replay any sequence of keys verbatim. One of my favorite uses of this is recording a macro where I want to update multiple lines similarly. What I do is record a macro on a single line, making sure to do the edits in a way that would work for all lines I want to change. Then I select all lines that I want to apply the macro to and run it on them. That executes the macro on each line individually making all the same edits.

There are lots more examples that I haven't covered and a probably a fair amount more that I don't know about yet. The part that I want to emphasize is that they all compose nicely with each other.


Distros exist

After using the vim plugin in VS C**e for a while I wanted to try neovim itself. I'd heard about some preconfigured nvim environments so and I checked out a few of the more popular ones like LunarVim, NvChad and AstroNvim. Lunar and NvChad I only really messed about for a few minutes. I did use AstroNvim as a daily driver for a month or two before I decided to make my own config. I would recommend trying any of them to start off or maybe even stay on long term. Looking at them again I'd probably give NvChad a longer try if I didn't use a custom config already (hey, at least I'm not an Arch Linux user).


Customizing is easy to start - kickstart.nvim exists

Distros are all well and good but I wanted to configure things the way I liked and the kickstart.nvim project by TJ Devries gave me a solid foundation. It's a solid template to start your PDE (Personalized Development Environment) as TJ likes to call it. It contains some of the very basics that you'd probably want all neatly packed into a single well-commented Lua file of about 600 LoC. Most of the customization that I did on top of it was for a few weeks after I started tinkering. I've mostly settled on my configuration now except for the occasional trial of a plugin that I see on youtube. Though I try not to bloat my config with too many and I have not kept all of them. I would recommend using kickstart for anyone that wants to make nvim their own.

Using Lua - an actual programming language, instead of json or what-have-you data format for configuration might sound weird to you if you're coming from most IDEs. It does however lend itself very useful to have the capabilities of a scripting language when you're making your config and Lua is one of the best embeddable scripting languages. It's dead simple and should not deter anyone from trying out a custom config.


(Edit) Performance

One thing that I totally overlooked is just how snappy vim/nvim is. The IDE users will tell you that a few seconds of boot time is nothing but almost instant startup does feel nice.

I recently updated my plugin config so most plugins that are not essential to the initial UI are now lazily loaded and the startup time is around 70ms. Some would say that this is blazingly fast 🔥.

Resources