I am a big advocate of leveraging tools to help me do my job faster, and a part of that is knowing how to utilise the power of a text editor/IDE to speed up day to day tasks.
However, I am not a fan of:
- spending hours configuring things
- memorising hundreds of keyboard shortcuts
- having to do without when away from my machine
I love Sublime Text, for a variety of reasons, and whilst it's not unique to this editor, one of the killer features I use every day is multiple cursors.
One commands to rule them all
The simplest way to make multiple selections is to press
⌘+D to select the next matching occurence:
Traversing with multiple cursors
Multiple cursors become especially useful once you've learnt how to traverse a document whilst using them.
You have a number of options available for complicated selections, but the main things I use are:
Option+Rightmove one word left/right
⌘+Rightmove to start/end of line
They're intuitive, easily remembered, and can be combined with
Shift to select text.
Additionally, for manipulating the selections I use:
- Cut line/selection(s):
- Duplicate line/selection(s):
So taking my example, this is how you would change the latter portion of the method definition – using
Option+Right – despite the fact there are a different number of words each of different lengths.
Note that whilst the username line gets out of sync because it has one less word, its easily remedied by moving a few characters back, and then reselecting "populated".
Copy & paste selections
Another killer feature of multiple selections is that Sublime Text knows how to handle copying and pasting them.
You can grab the bits you want, exit multiple selections (with
Esc) and paste it elsewhere, or match the number of selections (in this case: 3) and paste into another multiple selection – super useful if you've got several things to name:
Another thing I often find myself doing is lining things up for readability, now whilst Sublime Text won't do this out of box, Sublime Alignment can.
Now alignment is as simple as using multiple cursors, then hitting
In fact, if it happens to be equals that you're aligning, you needn't even use multiple cursors, just
⌘+Alt+A on a selection.
Dealing with numbers
Another thing I often find myself doing is numbering things, which is pretty laborious done manually.
Again, Sublime doesn't have anything for this out of the box, but you get a lot of the way there with the Increment Selection package.
Set up a multiple selection, open the Command Palette (
⌘+Shift+P), and look for "Incremement selection" – this will insert 1-N into each cursor, or if you've selected a number, it will increment up from the first value. Alternatively, use the shortcut,
Permutations (reverse, shuffle, sort and unique)
If you're anything like me, unalphabetised lists drive you nuts. Fortunately, there are permutation commands to help you out for both lines and selections – search for any of the above in the Command Palette (
⌘+Shift+P) or just look for "permute".
If you're in need of case sensitive selections, set find/replace (
Ctrl+F) into case senstive mode.
Note you can also jump straight into multiple selections by hitting find all (and it works with the regex option too).
Additional selections with
If all else fails, and you're struggling to get the selection you want, you can always reach for the mouse, and
⌘+Click to add in extra cursors.
I hope this helps you get started with multiple cursors, if you're overwhelmed, just start playing with
⌘+D and move from there, it's can be tricky to get your head around at first, but once you've got the hang of moving around your code with multiple cursors the sky's the limit.
If you have any questions/suggestions, do submit them in the comments.