Vim - Modal Editing

The fact that Vim is a "modal" editor is the first and most obvious thing that people notice about it (what do you mean this editor doesn't 'edit' by default?). The fact that you need to hit keys before you can edit, and use ex commands to do common things like save and quit are probably the main reason that people either love or hate Vim.  Exiting Vim is famously non-obvious, one of the four tech jokes according to tech blogger Kat Maddox:

Kat might not be wrong:

But once you understand the thinking behind this system it makes a lot of sense. Most of us–certainly developers, but probably many other professions–spend the vast majority of our time editing and navigating documents rather that composing from scratch. In normal mode keys are set to navigate and make broad changes to the document. For example, h, j, k,and l move the cursor (vs arrow keys, which means you never leave the home row), w and g move forward and backwards by words, and tapping / begins a document-wide search. In one of my favorite books about Vim, Practical Vim by Drew Neil, he makes this analogy:

How much time do you reckon artists spend with their paint brushes in contact with the canvas? No doubt it would vary from artist to artist, but I’d be surprised if it counted for as much as half of the time painters spend at work.

Think of all of the things that painters do besides paint. They study their subject, adjust the lighting, and mix paints into new hues. And when it comes to applying paint to the canvas, who says they have to use brushes? A painter might switch to a palette knife to achieve a different texture or use a cotton swab to touch up the paint that’s already been applied.

The painter does not rest with a brush on the canvas. And so it is with Vim. Normal mode is the natural resting state. The clue is in the name, really.

By using combinations of navigation and edit commands (called phrases) you can swap out and edit large swaths of text. For example, ciw swaps out a word and puts you in edit mode (it means roughly "change inner word"), cit means "change inner tag", and ca] means "change around brackets":

Vim phrases

There are a lot of other combinations like these but the key thing is that they are initiated by keystrokes from "normal mode". Another really handy trick is the . command, which repeats the last series of commands. So if, for example, you want to swap out a span tag for a div tag for a large series of spans you can quickly search for and swap those tags out with the . key:

Vim dot command

The . command is extremely powerful once you get used to it. In the very opening chapter of Practical Vim Drew Neil writes:

Our work is repetitive by nature. Whether we’re making the same small change in several places or moving around between similar regions of a document, we repeat many actions. Anything that can streamline a repetitive workflow will save our time multifold.

Vim is optimized for repetition. Its efficiency stems from the way it tracks our most recent actions. We can always replay the last change with a single keystroke.

These phrases and navigation commands are probably only a bit more convenient than their equivalents in other editors (often initiated with combinations using ctrl, alt and such), but it's nice that you don't have to move your hands away from the home row, and when you add in repetition of whole edits with a single key, you start to see serious benefits.

I suppose I like this paradigm for the same reason that I found Colemak's benfits underwhelming, I'm not straight typing most of the time. Ex commands (initiatied by :) add a whole different dimensions, allowing you to run commands across the entire docment (find and replace, running shell commands, split windows etc..). Altogether it's such a well thought out editor, and really not so surprising that it's still such a popular choice with so many people.

Subscribe to Pithological

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe