I spend a lot of my time working with text in some way, whether it be writing, programming, reading, making notes, or running terminal commands. Because of this, I consider my text editor my most essential tool. Previously, I wrote on writing software, and while this post on text editors will doubtless overlap in some ways with that, I consider the two areas – that of writing and that of text editing – distinct in a few important ways.
The most obvious difference between writing and text editing is, in my experience, the amount of time spent entering new text versus the amount of time spent altering existing text. Although I’m not always a linear writer, I still spend the majority of my time writing entering new text, usually at least a sentence at a time. On the other hand, with tasks like programming and organisational note-taking, much more of my time is spent altering and performing duplications and transformations on existing text.
It’s also true that text editing is very much the domain of programmers and other technical workers. Writing has a broader scope and is generally speaking a less technical and precise endeavour – you don’t need to write in a monospaced font, and writing doesn’t fail to compile when your editor replaces its straight quotes with prettier smart ones.
So, with that audience and focus in mind, let’s dig into some text editing software! This is going to be an ongoing series of blog posts, with each individual entry focusing on a different editor or family of similar editors. For this first entry, we’ll be focusing on…
Vim
How do you know someone is a Vim user?
Don’t worry, they’ll tell you.
![The editor of the beast](https://davidyat.es/content/images/2015/07/vim.png)
I’m going to state my biases up front here: my text editor of choice has for some years been Vim, or Vi Improved, the old-fashioned extended edition of vi, one of the earliest Unix text editors. I do make an effort to try out new and different editors as often as I can, but I seem to gravitate back to Vim for most things. And here’s why:
If you want to make a wide range of complex changes to existing text with minimal grunt work, there is nothing else that even comes close to doing it as well as Vim. This is for the same reason that it completely confounds anyone trying to use it for the first time after a lifetime of more traditional and modern editors – its composable command interface.
In an ordinary modern text editor, the user is presented with a blinking cursor at all times. In order to enter text, the user has only to press the keys on their keyboard which correspond with the letters, numbers or symbols they wish to enter. Press the a
key to type an “a” and so on. This is very intuitive – so much so that most people introduced to the paradigm early enough wouldn’t even be capable of imagining a reason why editing should be any different.
As entering text is not the only action performed in a text editor, other commands are needed than just “press x
to write x”. A few buttons on the keyboard, such as Page Up
and Page Down
, Backspace
and Insert
, are available for non-text-entry uses, but these are not nearly enough for even the most modest text editors, and so we come to key chords. These are things like Ctrl-C
to copy, and Ctrl-V
to paste, where the user is required to hold down two or more keys at once in order to perform some action. These can get very complicated – just ask an Emacs user.
Vim takes an entirely different approach to text editing: rather than reserving most of the keys on the keyboard for the dull task of text entry, Vim changes the function of all of them according to the current mode. In Insert mode, things work much the same as in a normal text editor, only with different chorded shortcut commands1 and some slightly irritating but changeable defaults such as the disabling of Backspace
. However, Normal mode, which the editor opens in and defaults to, is entirely different.
- To move around your file one character at a time, you use the keys
h
,j
,k
, andl
as left, down, up and right respectively (these keys had arrows on really old keyboards). - To jump your cursor ahead to the end of current word, you press
e
. - To jump your cursor ahead to the start of the next word, you press
w
. - To delete a letter, you press
x
. - To enter visual (i.e. selection) mode, you press
v
. - To enter insert mode at your cursor’s position, you press
i
. - To delete a word, you press
d
thena
thenw
. - To delete everything until the next instance of the letter ‘c’ in the current line, you press
d
thent
thenc
. - To remove a word and drop into insert mode in order to replace it, you press
c
theni
thenw
. - To paste something from the clipboard, you press
"
, then+
, thenp
. x
is not the same asX
andi
is not the same asI
. If at any time you accidentally hit Caps Lock without realising, you can do some crazy stuff to your file (all undoable with lowercaseu
).
People’s initial reaction to all this tends to go something like: “This makes absolutely no sense! Why do you have to memorise all of these arcane commands just to edit text? This vi thing is from the 70s! Back then everything was difficult and arcane because computers didn’t have enough resources to accommodate user friendliness. That must be why vi was made in this inefficient, unintuitive manner! Isn’t it wonderful that we have modern text editors these days?”
It’s a train of thought that seems logical and plausible, but it’s wrong. There are two main reasons Vim seems alien and arbitrary to modern sensibilities, and they have little to do with the environment of constrained resources in which it was first written.
Reason 1: Vim’s particular style of editing was not adopted by later programs. It could have been, but later programmer’s text editors overwhelming opted to instead emulate vi/Vim’s rival Emacs, with its non-modal editing and focus on extensibility as the source of its power: see Ultraedit, Notepad++, Sublime Text and Atom.
Reason 2: If you just look at Vim’s commands as they are described in a format like the bulleted list above, you miss the wider context of Vim’s editing language.
Some inklings of this language can be gleaned from the letters which denote individual actions and parts of actions: d is for delete, w is for word, p is for paste. There are a few more slightly more esoteric ones like y for copy (termed yank) and v to enter selection mode (visual) but most of the really common stuff is named in an intuitive and straightforward manner.
Like a spoken language, Vim’s editing language is split up into different parts of speech. There are the verbs (delete, yank, insert, change), the nouns (word, letter sentence, paragraph) and the adjectives/articles which modify the nouns (inner, a)2. One could probably also fit adverbs in there somehow, but I’m sure I’ve got the point across well enough without attempting that.
If the user just specifies a noun, like word or sentence, this will move the cursor in some relevant manner. So, as said above, w
moves to the start of the next word, e
to the end of the current word, and s
to the start of the next sentence. None of these commands modify the text.
Nouns can also be more specific. You can move the cursor within a line by issuing the command fc
, which will find the next instance of the letter ‘c’ from the current position of the cursor to the end of the line and place the cursor there. Fc
performs the same function, but looks backwards to the start of the line. And tc
and Tc
do similar, but only move the cursor till the specified character, stopping one character in advance.
Provide verbs and articles in addition to nouns, and things get a little more exciting. daw
from above, then, means delete a word. This command takes out a single word, as determined by which one is under the cursor, and also removed the space character which follows it. The same could be achieved with an entire sentence by issuing the command das
, or a paragraph with dap
. You can remove whole bracketed clauses or function bodies with da(
, da[
or da{
. And if you want to preserve the trailing space after a word, sentence or paragraph – or, more usefully, empty a bracketed clause without removing the brackets – all you need do is replace the a
with an i
(for inner). These are called text objects. You can also choose to delete an arbitrary sequence of characters, up to a specificied delimiter, such as the character ‘c’, with dtc
, which translates to delete till c.
And let’s say you don’t want to delete any of these things but would rather copy them. Then you just replace the d
with a y
(yank, i.e. copy). If you want to delete some text object and then immediately enter insert mode to put something else in its place, just replace the d
with a c
(for change). To make the object uppercase, issue a gUiw
or gUi(
. Operators (verbs) work on motions (nouns), and once you get used to it, editing text feels like dictating high level commands to an intelligent if fairly literal-minded servant.
What’s really exciting is that all of the preceding is but a small taste of the power and potential of Vim’s editing language. Because everything is expressed in a terse editing language, powerful and specific macros can be created, either as one-offs for use in a single editing session, or as permanent keybindings tailored to your use of Vim. For example, I have a keybinding for splitting comma-spliced sentences in two by (1) jumping ahead to the next comma, (2) replacing it with a full stop, (3) jumping to the beginning of the next word and (4) capitalising its first letter – all in a single command.
Shortcuts are available to repeat the most recent action performed, whether it be a regular verb-noun action, a specific movement of the cursor (noun), or a command issued to Vim’s specialised terminal, called ex
mode.
ex
mode is another killer Vim feature. Typing :
in Normal mode brings up a command line interface at the bottom of the editor window, to which a variety of commands can be issued. On first sight, the interface may appear quite similar to Sublime Text’s command palette, providing the user with a number of administrative functions such as writing files to disk (:w filename
) and opening new ones (:e filename
) for editing3.
However, ex
mode provides – in addition to various in-program administrative functions provided by both core Vim and any plugins that might be installed – an interface to a very powerful set of tools: your operating system’s command line.4
You can follow the command :!
with any terminal command. :!ls
gives you a list of files in the current directory, :!wc <somefile>
counts the words contained in the file given, and :!sl
displays an animation of an ASCII train. Most usefully for programming, you can type :!make && ./myprogram
or :!python myprogram.py
to (compile and) run the program you’re busy writing, all in your editor. The possibilities are literally endless.
And beyond all that, Vim has all of the features of the most fully featured modern text editors: column select, autocompletion (though you need a plugin to invoke it with less arcane keyboard shortcuts and make it better integrated with programming languages), special character insertion, spellcheck and an integrated file explorer. There’s even an inline calculator and an undo system which almost resembles a miniature
![windowmanager](https://davidyat.es/content/images/2015/07/splits-1.png)
Random files in $HOME
!
Vim has some faults, such as being single-threaded, having awful plugin management and the various shortcomings of its plugin scripting language. It’s not perfect, and yes, there are plenty of people who don’t want to use a text editor with a learning curve – completely understandable (if entirely wrong).
But even if you don’t see yourself using Vim heavily, I think it’s good to understand what it does, and why it works like it does, because it extends your notions of what’s possible. Before I learnt Vim, I didn’t even know I wanted a text editor with an editing language. And that’s why I’m writing this series: because by trying different editors, I hope to get an idea of what sort of features are out there, and how badly I never knew I needed them.
Appendix A: Useful Vim resources
Below are a few different resources I’ve found invaluable in learning and using Vim.
- Practical Vim by Drew Neil
- Steve Losh’s Vim setup and his book, Learn Vimscript the Hard Way
- Everyone Who Tried to Convince Me to use Vim was Wrong: a gradual approach to moving to Vim from a mainstream modern editor.
- Vim misconceptions: yes, you can use the mouse.
- Vim Tab Madness: Vim is old enough that a tab isn’t what you think it is.
- Vim Koans
Appendix B: My six essential Vim plugins
I didn’t even go into Vim’s extensibility much in the article proper beyond a few mentions of keybindings, but like any respectable programmer’s text editor, Vim can be extended to do all sorts of things. And over the few decades of its existence, it’s accrued an enormous library of all sorts of useful stuff to extend its already wide-reaching capabilities for a lot of very specific tasks.
You can waste spend a lot of time trying out different plugins5 (and I have) but after a while you’ll probably settle down with just a few that you can’t live without. Here are mine:
- Vundle: Something Vim doesn’t do well out of the box is manage plugins, making the third-party plugin manager Vundle absolutely essential. Instead of mucking around with files in various
~/.vim
subdirectories, Vundle lets you install plugins just by including a part of their Github URLs in your~/.vimrc
file and running anex
command or two. - YouCompleteMe: powerful Tab-autocompletion for multiple languages.
- vimwiki: a small plugin to give your Vim the power of programs like CherryTree and Zim Desktop Wiki to create your own personal Wikipedia. Great for taking and organising notes.
- vimshell (requires vimproc): allows you to put terminal emulators in your Vim buffers (a very powerful concept I discuss in the next post in this series).
- vim-surround: creates text objects for characters which surround others (brackets and quotes), allowing you to construct commands like “change the single quotes around this word to double quotes” and “surround the selected text with round brackets”.
- vim-airline: gives Vim a pretty, informative bar at the bottom and a list of open buffers at the top (see above screenshots).
I have more than these few installed, I’ll admit, but they’re the only ones I would miss significantly if Vim plugins where suddenly outlawed.
-
vi is old enough to predate universal conventions about which key chords should be used to copy, paste, undo and so on. It’s not a big deal though, because you can remap all of your keys and chords fairly robustly if you want. ↩︎
-
That last one is possibly a bit of a stretch, but hey, I never claimed to be a linguist. ↩︎
-
And quitting, for that matter.
:q!
is the only Vim command many people know. ↩︎ -
Although Vim will run on Windows, using it there is very depressing for this reason. ↩︎
-
And you’ll find everything from language-specific helpers to games and news readers. ↩︎