Terminal-Based Text Editors:

Add a touch of Vim to your productivity.

Lee Bardon
Analytics Vidhya
5 min readApr 17, 2020

--

Relevant xkcd :)

We’ve all been there. You’re just about to commit a feature update for your latest industry-disrupting app “CatPicGenerator_2.0”, when you find yourself in a strange and disorientating place…

In your excitement, you managed you type “git commit” without attaching a flag and a message. Which is fine… we all make mistakes, right? Well, git is less forgiving than your fellow programmers might be, so it hoists you into the above environment to force you to make corrections. The trouble is, you have absolutely no idea what to do next.

First, you hit q… Hmm. Nope.

Then, you frantically hit Esc, Enter, ctrl-alt-delete, q q q q q q q QQQQQ!!1!!!!!1

Finally you mash your face into the keyboard, close the terminal, gingerly fold down your screen, and toss your laptop out the window, wide-and-glassy-eyed with utter frustration.

But it doesn’t need to be this way! You’ve simply been introduced — albeit against your will — to a terminal-based text editor called Vim and, while the learning curve is admittedly steep, it can be an efficient, powerful tool to have at your disposal. Let’s take a look.

Why Use Vim?

My own introduction to text based editors (and programming in general) occurred in academic surroundings, during my time as a computational scientist. The seasoned physicists around me knew nothing so fancy as VS Code, or Atom, or RubyMine or PyCharm. As far as they were concerned, the choice was simple: Vi/Vim or EMACS for writing some sweet Fortran95 or shell scripts, and Latex for preparing documents.

Somewhat arbitrarily, I decided that Vim was my text editor of choice. However, there are many who make a much more reasoned decision. They argue that, while the learning curve is steep, gaining a good degree of mastery in terminal-based editors like Vim, can hugely increase productivity over GUI-based editors.

For a start, it forces you to use the keyboard only, which means no pausing to move the mouse/trackpad, click on the screen, drag, highlight, drop, delete…. all of these actions can be done at the touch of a key, whether via pre-existing keyboard shortcuts, or ones that you’ve defined yourself in the .vimrc file. One of Vim’s core philosophies argues that we spend more time editing text than writing new text, so it’s built to enable rapid document editing, leaving your workflow unbroken in the process.

It’s also ubiquitous. It’s available as standard on Linux, Mac and Windows, and is often the default editor in scenarios such as e.g. the Github debacle mentioned at the beginning of this article. Indeed, it’s the only editor available to you, if you spend a lot of time in SSH’d terminal sessions, as I once did while interacting with HPC clusters.

But this immediately begs a question — if it’s so efficient, and so ubiquitous… why don’t more developers use it? We tend to hear a lot about, for example, VS Code or Atom, but very little about Vim.

The answer is that, like many powerful tools, it has a steep learning curve, and until you climb it, it can, at first, feel awkward and unintuitive to use.

The typical Vi/Vim learning curve.

Getting Started

For the sake of continuity, let’s return to our original Github scenario and work through how we can manage to complete our commit, or simply exit Vim, without any head-banging or laptop-tossing.

Vim supports multiple modes, but there are three that tend to be the most common; command mode, insert mode and visual mode.

Command Mode

Command line begins with a colon.

We can access the command line by hitting Esc. Here, we can perform operations by typing short commands after the colon and hitting enter. For example, we can save with w, quit with q, or save and quit with wq. We can even force a quit without saving using q!.

But we can do much more than that. Using :vsplit /path-to-doc/doc will open a new document in splitscreen mode, for easy reference. Copy, paste and delete is y, p and d, respectively, and we can apply this to a whole line using yy or dd, or, indeed, to any number of lines you like by prefixing yy or dd with the relevant number (e.g. 5yy to copy 5 lines).

Visual Mode

Visual mode is super useful for identifying the text that we’d like to manipulate within a document. We can initiate character-based selection using v (lower-case), select whole lines via V (upper-case), and entire blocks using Ctrl+v.

Selected characters/lines/blocks can then be deleted, copied or modified with any other Vim editing command.

Visual mode, used for selecting characters, lines and blocks.

Insert Mode

This one is fairly self-explanatory. Insert mode is where we actually insert content into our document. We access it by hitting Esc followed by i.

Insert mode! It needs no explanation :)

So, with all of the above in mind, how would we navigate our Github-debacle? Well, we could either:

  1. Finish our commit by entering Insert Mode (Esc followed by i), and adding our flag and message, or;
  2. Exit Vim by entering Command Mode (Esc followed by a semi-colon) and quitting with q!.

See? Your sanity, and your laptop, have been saved, and we’ve barely scratched the surface of Vim’s power. If you have a spare 30 minutes, type Vimtutor in to your terminal and follow along. You can thank me later…. And yes, I do accept cash, thanks for asking.

Have fun!

--

--

Lee Bardon
Analytics Vidhya

Computational scientist and programming enthusiast