VIM adventures

A proficient VIM practitioner can resemble a conjurer, the way they bounce around the text editor with lightning speed and efficiency, effortlessly executing complex commands and manipulations as if casting arcane incantations, it can be a sight to behold.

8 years ago now, I was an intern as a Computational Biologist with the goal to assist PhD students and doctors write bash scripts designed to run on the high-performance computer (HPC) at Earlham Institute, formerly known as TGAC. I used the Simple Linux Utility for Resource Management (SLURM) for executing jobs on the HPC cluster. Scripts to grok data, remove anomalies, oraganise results, etc.

One of our resident computational biologists was a dedicated VIM enthusiast, the way he navigated the code was truly mesmerizing, a genuine hacker. I wanted to be able to do those things too, but at the time I already had so much to learn. I was plunged headfirst into the world of biology without really any biology knowledge since my GCSES, so I just learnt the fundamentals of VIM and tried to keep up.

That period was my first introduction to VIM and I haven’t used it since.

I’m a very big fan of fully-fledged IDEs, particularly those developed by JetBrains. The debugger is a tool I can’t imagine working without. Currently, I am a full-stack dev writing C# and Vue.js, so Rider and Webstorm are my go-to choices. I am always running in debug mode.

When I’m working on my C C++ code I’m always running from the debugger. I type in the code, I run it many times. The first thing I do after writing code is I set a break point and step through the function. Now, a lot of people say ‘well I do that in my head’ well, your head is a faulty interpretter

John Carmack –

I have learnt all the Rider shortcuts, but still, something is missing. I’m a relatively fast typist, but I am nowhere near as fast at editing code as the Earlham institute hacker, what am I missing? VIM commands. I installed IdeaVim.

Let’s say you have the following piece of code, and you want to edit the param to be ‘int age’, how might you do it, without using your mouse? I would:

  1. hit End to get to the end of the line
  2. left arrow to get within the parenthesis
  3. ctrl+backspace twice to delete address then string
  4. start typing in ‘int age’
    public bool Validate(string address)
        // some validation

To do the following with VIM commands:

  1. ci(
  2. type in ‘int age’

Which is to say “change” + “in” + “brackets” which will remove the code within the brackets and chuck you into insert mode so you can begin editing.

Learning VIM is quite an undertaking, there are countless commands to learn which may be overwhelming to those starting out. But there is method to the madness, the commands have grammar and when you learn the basic building blocks of the verbs and nouns you’ll be able to know what a command is you’ve never done before without looking it up. For instance, in the example above you could replace the verb “change” to “yank” (copy) or to “delete”:

  • yi( copies whatever is within the parenthesis
  • di( deletes whatever is within the parenthesis

Or you could substitute the i for a, which would you give you a further 3 commands:

  • ca( changes the content within and including the parenthesis
  • ya( copies the content within and including the parenthesis
  • da( deletes the content within and including the parenthesis

There are endless examples where you can sub nouns/verbs in to perform a different command you didn’t know you needed.

I’d highly recommend checking out Igor Irianto’s blog on Mastering Vim Grammar for a more in-depth explanation.

From my mere 2 months of learning VIM I now fully understand its capabilities and maybe, on the occasional rare moment, found myself comfortably seated on the wizard’s table, effortlessly weaving through the code as though my cursor is a bobsleigh and the editor has well-defined grooves that didn’t exist before.

As mentioned, it can look like magic to the uninitiated. Picture this: this week I’d just done a bit of a refactor and wanted a second opinion, so I rang up another engineer. I explained the changes i’d made, unsure whether it was a worthy change or over engineering. I listen as he suggests a change, that change being exactly how the code was before the refactor, although he didn’t know that. With a mischievous grin I say, “ahh, now, watch how fast I can make that change using VIM”. I then hit Ctrl+z about 10 times. He didn’t suspect a thing.

Leave a Reply