by edanm · 14y ago · 0 comments
For anyone that doesn't know it, Multiple Cursors is the ONE BRILLIANT FEATURE that Sublime Text has. Don't get me wrong, tt's great in many other ways too, but Multiple Cursors is, IMO, a feature that should become part of every friggin' text editor in existence, it's that important. And it only exists in Sublime Text.
It gives you multiple cursors at seperate places in your document. Each one acts completely independently, moving around with your commands. Everything works as expected, including copy-pasting (it will match the copy-pastes up to your cursors).
Most text editors have a column-select mode, except that Multiple Cursors are the same thing on steroids:
1. The selections don't have to be contiguous. So if you have a bunch of:
<div>
<p> Text And more Text</p>
<p> Something </p>
</div>You can put the cursor only on the <p>s.
2. Since each cursor acts independently, you can do operations that you simply can't do in column mode. For example, place the cursor on each <p>, select to the end of that </p>, and delete the text. This is even if the contents of the </p> are completely different.
3. Multi Cursor gives the same power as macros, but: They're visual! You see all the changes you're making in realtime. Any long-time vim user has probably recorded a macro, tried playing it back, and discovered that it didn't work. Well, with Multi Cursors, you see the cursor and changes in real-time. Make a wrong command? Undo it instantly.
Want an example of another actual use case? I used to have a plugin for Sublime Text that let me split a selection into multiple selections, based on a character. So, let's say I had this code:
foo(int a, char b, char c)
I could select the paramater list, run my shortcut to split by ",", then I'd get a different selection on each paramater. Then, I could simply multi-select three lines below it inside the actual function, paste, and I've got this code:
foo(int a, char b, char c)
int a
char b
char c
A dead-simple editing task I do 300 times a day.Another example? Let's say I have two "if" blocks in the top of a function:
if (somecondition):
return
if (anothercondition): return
Now, let's say for a second that I just realized I don't want to return, I want to do some processing, then return. This happens a few times a day for me. I just multi-select both "if"s, hit enter, and add the code. If it's two functions and I need to use the function names, I can even go back, copy the function name, and paste it as part of the processing!!Phew. If it seems I'm a fanboy for Multiple Cursors, I totally am. It's a brilliant tool that only exists in Sublime Text. I've unfortunately gone back to Vim, and am too far in Vim's world to manage working in Sublime Text, which is why I miss multiple cursors so much!
But I AM working on a Vim plugin that gives Vim Multiple-Cursors. Been working on it for a while, but I think I'm getting near a more-or-less stable version. Hopefully, it will be released soon.
</way-too-long-rant>
Fortunately, other editors are already adopting similar features. Just today I was fiddling around with Ace, a web-based code editor, and noticed that they support multiple cursors too! Just select multiple words while pressing the Ctrl (or Cmd) key.
Live demo: http://ace.ajax.org/build/kitchen-sink.html
The nicest thing is that I only found this by accident because I'm so used to using multiple cursors in ST2. It's a great feeling to be surprised that something works just the way you sub-conciously expected :)
Yup, work's being done to add this to Emacs as well although it is not quite there yet:
I'd love it if ST managed to replace vim. I (very unfortunately!) don't see it happening soon. Vim just has so many features and so many plugins.
NOTE: Half of my plugins in Vim are only there to make vim behave like a decent, modern editor. In fact, half of my plugins in Vim are there to make it behave more like Sublime Text.
But, as you said, multiple cursors is a lot like visual feedback for macros. I miss such a "live" feel sometimes.
You got me interested: I'm looking forward for your Vim plugin.
But there is one use-case of macros that cannot be replaced with multiple cursors -- one where the places being affected by the macro are themselves computed by the macro.
Btw, another place this can't be used is across multiple files, at least the way ST does it.
def qux(bar, baz, qux)
pass
def quuux(grim, gram, grom)
pass
def quuuuuux(hip, hop, zoom)
pass
Delete second argument (of 3+ arg functions): /def<CR> # search
qd # record as 'd'
2f,dF,n # find second arg
q # done recording
@d # again (or n to skip)
Example intentionally simplified/convoluted. I'm doing stuff like this on the spot routinely, and there's no way multiple cursors could do this because I apply verbs to text objects that ST has (unless I'm missing something) no way of knowing about in a visual paradigm without resorting to guessing what I mean.What would be killer is mixing both, i.e visually selecting where to apply a vim macro.
I'd select all three "def"s simultaneously, then move by word three/four times using ctrl+right, or plain old "w" if using ST's vim mode. When I'm on the second param, I'll delete forward to the next ",". I could also just ctrl+right my way until the next word, which would be the same.
Just to recap the keys I'd press:
/def (select first def) ctrl+d * 2 (select using multi-cursors the next two defs ctrl+right * 4 (to get to the second param) ctrl+shift+right (to select the entire second param) delete
And if I mess up at any point, I instantly see it. If I did something that only works for the first case but not the others, I see it immediately.
Note: Using vintage mode, Sublime Text has vim shortcuts. Without it, Multiple Cursors still rock, but are missing a LOT of their power (exactly because of situations like this).
In fact, in Sublime Text 1, for which vim mode didn't exist, I actually implemented a plugin to emulate the "f" and "t" movements because they were so good.
I think it's actually useful, not simplified/convoluted..
No code duplication. No goto
if (somecondition):
return
if (anothercondition):
return
Now, let's say for a second that I just realized I don't
want to return, I want to do some processing, then
return. This happens a few times a day for me. I just
multi-select both "if"s, hit enter, and add the code.
Not a great example, because this only leads to code duplication.