30 Apr 2015
The underestimated power of multiple cursors
When it comes to fast and efficient transformation of structured text or code, nothing can beat Vim and its expressive keystrokes in combination with its text objects. Thats common wisdom in the developer scene, that’s what I thought for a long time too.
But is it really true?
During my career, I spent an awful lot of time with “sharpening the saw”: making my various text editors more effective and learning new and faster ways to cut trough my code. At the same time, I’m a big believer in keeping software as simple as possible, that’s why I really enjoy using TextMate as my current default. For me, it’s all about keeping the cognitive overload as low as possible, which allows me to concentrate better on the current problem to solve.
Last year, I invented a little game, just for myself. There’s this site called Vimgolf, a beautiful and competitive site where the goal is to find the least amount of key strokes to transform some text or a snippet of code.
Its absolutely stunning how creative the top entries of the various challenges in the game are. But after following it for a while and trying one or another challenge myself, I started noticing some patterns where a couple of basic features of TextMate, especially the multiple cursors, required far less key presses and almost no need to think about how to apply them – just basic movement and the knowledge how to set the cursors to the right position.
Let me introduce you to it and show you some examples, let’s play .
Here is the short list of built in features of TextMate essential to fast text editing:
- ⌘e and ⌘⌥f: “Use Selection for Find” / “Find all” to quickly set cursors anywhere in the document or the selected region
- ⌃w: “Select Word”. On every subsequent key press it finds the next word and creates another cursor
- ⌃t: Transpose. Based on the current selection, it works on single characters, words…
- Auto-Paired Characters
These, combined with simple movements and the ability to move block wise when holding ⌥ are enough to quickly transform almost any text.
Don’t believe me that those are enough? See for yourself as I go through a couple of challenges on the Vimgolf site.
I’m concentration on challenges which realistic scenarios, as those like Do you demand a shrubbery? are not really common and can’t be beaten.
As a side note: The game requires the editor to save and close the editor. To keep the comparison fair, I count ⌘s + ⏎ to the number of presses.
Vertical Limit is a typical example you have to deal with as programmer on a daily basis, transforming lists to something like arrays, function parameters, etc. The start file is this:
There is no vertical limit for vim Ninjas
The output should look like this:
[ "There", "is", "no", "vertical", "limit", "for", "vim", "Ninjas" ]
Top entry in the Vim challenge is 21 one strokes. TextMate needs only 19 ;)
You still don’t believe me, do you?
Here we go:
The keys I used here are:
Pretty neat, huh?
But it doesn’t end here…
I forgot quotes
The challenge “I forgot quotes” is also pretty common:
foo = a ab abc
foo = "a" "ab" "abc"
Top entry Vim: 10 key strokes
TextMate: 8 key strokes!
Those keys are the minimum:
Fix the XML
I’m not sure if “Fix the XML” counts as a real world example, but anyway:
<I> <hate> <xml>hello <files>files </hate> <hate> <xml>xml <files>world </hate> </I>
<I> <hate> <xml>hello</xml> <files>files</files> </hate> <hate> <xml>xml</xml> <files>world</files> </hate> </I>
Top entry Vim: 18 key presses.
Normally I’d use “Insert Close Tag” from the HTML bundle, but let’s do this with just the basic features:
Here the keys I used:
16 presses, chords, whatever you like them to call… Yay :)
Please keep in mind that I don’t want to step on anyones toes. Vim is in some scenarios more powerful. There are a couple of challenges that can’t be beaten on the site, at least not without making use of more advanced Bundle commands. My point is, that multiple cursors is such an underestimated and super powerful feature for text transformation. It almost hurts that its not natively available in Vim. There is vim-multiple-cursors - which is a great plugin – but sadly it slows down heavily as soon as lots of cursors are used.