I’m Paul Herron, a full-stack developer and technical manager.
I focus mainly on back-end development with tools like Symfony
and devops with tools like Docker

Vim's modal magic

by Paul Herron on 09 March 2014

Vim is a strange beast with an interface quite unlike most other applications. Maybe you’ve encountered it but not yet taken the time to look into how it works, in which case you might know it like I did as the grim booby trap lurking within Unix; the one with the psychotic interface seemingly designed with the sole intention of tormenting you, the uninitiated user. It’s the one you end up in by accident when editing a cron job, realising with horror that you should have used Nano instead, but now you’re stuck inside Vim and while you could of course look up how to use this bizarre application, the easier option is just to reboot the machine and pretend this whole thing never happened.

This was more or less my train of though for quite a while until I came across a few blog posts explaining the virtues of this instrument of torture. I spent some time looking into it and quickly came to realise what an unusually powerful text-editing tool it could be.

Being modal

The key to what makes Vim so powerful (and so wretched, initially) is its modal interface. This is the idea that when you open a document and type in some text, nothing happens. Or more likely, bad and unexpected things happen: your document gets interspersed with gibberish or you delete every line.

What’s actually happening is that you’re required to go into an explicit “insert mode” before you can enter any text. Much of the time you’re not in insert mode, but are instead in “normal mode” where your entire keyboard is given over to weird and wonderful shortcuts. At the heart of this is the idea that actually inserting text is only one part of the picture; you’re likely to spend an awful lot of time doing other things: manipulating your existing text (deleting lines, duplicating bits, moving words around) or just navigating around the file. And that’s what Vim’s normal mode is for.

The idea of shortcuts for doing special things is obviously well established, in that nearly every application will allow you to press some combination of meta-key and character key to do some special action: Cmd-C to copy, Cmd-R to refresh, and so on. Vim takes this idea and pushes it to the extreme though, in that just about every single key on the keyboard has a special meaning and does something useful for you, and in many cases you don’t even need to press an additional meta-key for it to work. So for example, pressing x deletes a character and pressing 0 moves the cursor to the beginning of the line.

Eliminating meta-keys may seem like a subtle change, but it makes it practical to start chaining commands together in useful ways. For example, typing dw would delete the word under the cursor, dt. would do a “delete from the cursor till the next full stop”, and doing cib would let you “change all the text within brackets”.

You can also introduce counts to your chained commands, so for example you can say 3dw to delete the next 3 words after the cursor.

Essentially you’re forming little command “sentences” to issue to your editor, and some people do say that part of Vim’s unique appeal is that if feels like you’re “talking to your text editor” or programming it on the fly. For me personally it also feels like Vim has an understanding of semantics that isn’t really present in most other editors. I can say something likt ct@ to change the beginning portion of an email address; Vim has an understanding of what these semantic constructs are, and makes it possible for you to interact with them directly.

Vim has lots of useful features you’ll also find in other great text editors: macros, column editing, great support for external commands (e.g piping the current file through a markdown processor) etc. But it’s the power and expressiveness of the modal approach that keeps me – and I suspect many others – using it. Issuing these commands starts to feel very natural very quickly, to the extent I often catch myself trying to issue them to Google Docs and other places they’re not really meant to be! (VimFX is a wonderful Firefox plugin for this, by the way).

Anyway, I’ll leave you with a little selection of commands that hopefully shows how expressive one can be in Vim’s normal mode:

Back to homepage Back to blog listing