Vim Tip of the Day: Living the High Life

The signature of Vim-style editing is operating in terms of high-level abstractions in the world of text like words, paragraphs and delimiters, and expressing actions at this level without having to “descend” to actually doing the character-by-character and heavy-handed selection-based grunt work of manipulating the text. Many people think that Vim is about minimizing keystrokes, but this is confusing an effect with a cause. The real essence is expressing ideas about text precisely. The fact that Vim thinks about text in the same way that you do, is the source of the power of linguistic editing.

A major benefit of this is being able to refer to such linguistic commands as natural units. Every action you perform in Vim is a linguistic action — e.g. “delete until the end of the line.” In contrast to other editors, your edit history is made up of such commands rather than low-level individually or arbitrarily-bounded edits. If you were to peek at it, it might look something like this:

> insert the text "Grocery lisp"
> replace character with t
> insert "<item></item>"
> change inside tag to "apples"
> paste contents 10 times
> change inside tag to "kale"
> change word to "ice cream"
> open a new line and insert "don't forget the eggs, half-and-half,  and milk!"
> change until comma to "bananas"
> delete character

Since your edit history is made up of such commands, it means that Vim’s undo u and redo C-r commands are much more useful than they might be in other editors. Each undo/redo action refers to a linguistic item in your edit history, rather than an elementary action like deleting a character. When you undo and redo, you are navigating through ideas you had, not the busy little details of typing.

This rich underlying representation also enables one of Vim’s most useful features — “repeat action” invoked by typing . (dot or period). This repeats the latest action in your edit history, which as we know could even be a complex action like “change surrounding parentheses to box brackets” (although this particular action requires the Vim surround plugin). In Emacs’s Evil, the . command also allows you to repeat any recent command and not only the latest one, making it even more useful. This feature isn’t present in other Vimlikes (including Vim itself) yet, as far as I am aware.

This representation also underlies other, less-prominent but still useful features like g; and g, and gi to visit and interact with naturally-bounded locations in your edit history.

Choosing between good habits and efficiency

This tip shows us why it is a good habit to Escape to normal mode after each thing that you do, instead of lingering in insert mode. That makes the text you type into a bounded item in your edit history, which can be repeated or undone as needed. But it also represents a common criticism of Vim-style editing (especially from Emacs users) — that escaping to Normal mode for simple edits incurs a cost in efficiency. For native Vim or NeoVim users, Insert mode provides a few facilities to avoid the efficiency cost here, such as C-o to momentarily execute a normal command without leaving insert mode (which also bounds the edits in the intuitively sensible way). For Emacs/Evil users, the other option here is to use native Emacs in place of Insert mode, so that simple edits can be done using familiar native optimizations. But when you’ve completed such edits, the place you’ll want to rest is in Normal mode.

2 comments

  1. Jochen Burkhard

    The single most useful resource of VIM trickery I ever came across.
    Stellar work and thank you very much for your contribution to the writing community!

Leave a Reply

Your email address will not be published. Required fields are marked *