Vi efficiency for a programmer

How could you boost your efficiency using Vi as a programmer? In this article, I’d like to share my experience after couple of years using Vi as my main programming editor.

Precision at its baseline

First of all, Vi is about precision at its baseline. We have to appreciate that to even get started using Vi. What does it mean?

4j // move the cursor 4 lines down
2cw // change the next two words
dt) // delete character till the next )

You could move your cursor down by pressing Down arrow four times, or in a Vi way you could type 4j to tell it to move down four lines. Using the arrow approach, we are flexible to move the cursor anywhere we want, however if you are confident we only want to move down four lines, then 4j shows a lot more confidence built with precision. It’s quite debatable which of these two ways is more efficient, since the answer to this question really depends on your personalities and the type of work you do. After all, if you like the way of being accurate, and believe the precision can be one ingredient to the efficiency, then Vi is an editor you should consider.

To be precise isn’t saying that you can’t change your mind afterwards, it’s only emphasizing that If you happen to be accurate and correct, then you might get the job done a bit quicker, maybe only 0.1 second quicker. But this 0.1 second matters a lot to some of us, especially to type using keyboard.

You can practice of moving down four lines million times using the Down arrow, but that is 4 key stroke overall, so reducing to 2 key stroke with 4j seems a bit more robust and repeatable. For us aiming for accuracy and only trying to get better by 0.1 second, Vi gives us a bit upper edge out of the box. Of course if you ever enters the Vi world, there’s just so many commands to remember and so many ways to getting something done, which itself gives us a lot of things to practice to get better.

Two modes dilemma

Just as in what floats you can sink you as well, Vi efficiency comes with its own dilemma, which is two modes, command and insert modes. In order to support commands like 4j , we need to switch to command mode with Esc. And whenever you are ready to type, we can switch to the insert mode, such as using i.

If you are a person who like to write a chunk of stuff before making changes, two modes turns out to be efficient. In the insert mode, you type as much as you could. And then switch to the command mode where you can efficiently to make small changes, such as fixing a typo or change a word or two here and there.

But there’s situation where the writing and fixing are intervened. For instance, you might be writing a paragraph where you don’t know what you are writing, so you could be correcting yourself every second. In that case, using Vi in a strict way, you could be switching between two modes constantly. No matter how you master the Esc key, you can still feel frustrated two modes is an over-kill. Yes, in that case, we might regret the invention of Vi.

Master of your own destiny

But Vi never says you have to use two modes and never says when you should switch from one mode to another mode, or how frequent you should do that. Isn’t that an irony? Since your destiny is always in your hand, not in the tools or paths you chose.

This requires some planning. In case you make a mistake in editing, the first thing you are tempted to do is to correct yourself. But this might not be a smart move. Might not be, and might be, so we need to practice to know for sure. Here we only want to emphasize the “might not be” case.

If we make a mistake and ignore it, we can save us from switching to the command mode. Because we know after we correct ourself, we need to switch to the insert mode again to resume the work, which is a bit costly. Instead, if we could face our mistake without correcting it, we can resume the work without switching. How do we make that to work?

I don’t know exactly, but how about putting your correct version in a new line? So you can have two versions side by side. If you ever come back to fix it later, you can just do delete one of them, ex. dd . There of course other similar ways, for instance, by copying one of the version to a named registry; or by creating a marker so we can come to this location later. There’s a lot of other ways, but all in all, the ultimate goal is to defer the switching to another mode until later.

You can see, this is not exactly related to Vi, this is a bit more philosophic point of way in terms of editing. The human brain doesn’t like to switch between two different ideas back and forth quickly, it could exhaust your brain or even make you crazy. However switching under a comfortable frequency could be a good exercise to boost our productivity, since our brain also likes some surprise every once a while. So the key here is the frequency that you want to come up with to make yourself comfortable as well as efficient to the maximum extent.

Summary

Some of us say Vi is efficient, while some of us say Vi could be very frustrating to use. I’d like to say Vi can be quite efficient if you can master the frequency of switching between the command and insert modes in your work. If you can figure out a right balance, then you can be really efficient. The funny thing is that Vi isn’t mandatory to achieve this balance in the end.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fang Jin

Fang Jin

Front-end Engineer, book author of “Designing React Hooks the Right Way” sold at Amazon.