Settings

Theme

Show HN: vim-multiple-cursors - True Sublime Text style multiple selection

github.com

191 points by tma 13 years ago · 38 comments

Reader

codemac 13 years ago

If there is one thing I love about vim, sublime, light table, etc.. is it gives emacs fans new things to implement in elisp!

But seriously, I love the work that has been going into all these different text editors. It seems to be net benefit for everyone. Multiple cursors are like a visual kbd-macro-edit..

Obligatory emacs equivalent source:

- https://github.com/magnars/multiple-cursors.el - http://emacsrocks.com/e13.html

By the ever awesome Magnar Sveen

  • tmaOP 13 years ago

    I should probably put a reference to him on the project page, as watching his video recently was a big push towards making me implement this in Vim. His expand-region plugin also inspired me to create vim-expand-region. I wonder what I'll 'copy' next. :)

    • swah 13 years ago

      That was a great video - I just wish he'd used an app to show all keys he's pressing (since there's something to pickup from advanced emacs' users).

      Used Emacs from a few years and I don't remember any more how to do all those things!

    • gaving 13 years ago

      Thanks for mentioning (& developing) this, vim-expand-region is awesome.

      • tmaOP 13 years ago

        Thanks! expand-region and multiple-cursors run in the same vein in that it takes some functionality Vim already offers, and makes them a little easier to use to the average user. I'm a pretty new and definitely average Vim user myself, and remembering just 2 keystrokes to select some region of text is definitely much easier on my brain muscle than having to remember the dozen of text objects offered by Vim as well as other plugins. Maybe one day all of them will be burnt into my muscle memory, but that simply isn't the case today and expand-region helps me remain productive while learning. :)

  • sjm 13 years ago

    I love Magnar's multiple cursors. I end up using it at least a few times a day, and it always saves me a ton of time.

adimitrov 13 years ago

Issues:

> * Multi key commands like ciw do not work at the moment

> * Single key commands that do not terminate properly cause unexpected behavior

> * Undo behavior is unpredictable

> * Performance in terminal vim degrades significantly with more cursors

There are some more than just these. I really wish Vim were easier to hack. The cursor and drawing models are terribly rigid in Vim, so this plugin probably has to resort to some awful hackery to achieve what it does. :-(

That said, good work, but I'll hold off on trying it out, for now. I'd like at least undo to be fixed before I can use it frustration-free in my production-environment, i.e. producing actual code.

I really hope you continue your work, because I'd love to see multi-cursor functionality implemented in Vim!

  • tmaOP 13 years ago

    Thanks for the feedback. I'd love for any issues you've seen to be reported so I can get on fixing them. I finally got the plugin to a usable state today and decided to release it early to get feedback, knowing that there're a ton of bugs and missing features. And you're right, figuring out ways to hack around Vim's peculiar ways wasn't an easy task by any means.

    • adimitrov 13 years ago

      Absolutely fine, I love that you're doing this! I'll give it a run tonight when I have some free time, maybe even look into the undo-stuff, which I think is most important to me.

edanm 13 years ago

For everyone here writing that they don't see a need for this because vim has macros/regexp's:

Please, please, try it out. I'm telling you, this is leaps and bounds above macros/regexp's for most use cases. It's the difference between a visual way to make changes, and a "disconnected" way in which you write commands and hope they work.

Seriously, I consider Multiple Cursors one of the greatest advances in text editing, and everyone who doesn't even bother to try it is just being silly.

  • micampe 13 years ago

    > It's the difference between a visual way to make changes, and a "disconnected" way in which you write commands and hope they work.

    Exactly, it's like ed vs vi.

  • _delirium 13 years ago

    I've been trying it, and do find it nice for small things, but a bit tedious for bigger things. Taking one of the examples in the animated GIF on the linked page, if you want to split a line on commas, with multiple cursors you have to select every single comma to 'initialize' the cursors. If the line has 3 fields, this is easier than using the regex, but if it has 10, then I find it a lot easier to just regex-split rather than spawning 10 cursors. I think I find 3 or 4 cursors nice, but past that prefer batch-style commands to manually managing a big set of cursors.

    • losvedir 13 years ago

      > with multiple cursors you have to select every single comma to 'initialize' the cursors.

      I hear this. Hopefully, the author will be able to implement what Sublime has: a find all field (which accepts regular expressions), and results in multiple cursors on all the matches.

      I do this all the time in Sublime and it's so natural:

      Cmd+F, /regex/, opt+Enter, [replace, or commands or whatever]

      It's just as fast as your usual find and replace or quick macro, except that you have instant feedback, are able to undo, jump around by word, or (with vim-mode), use t or f to find a character, ci", etc.

      Selecting a match one case at a time is great, though, when you need to skip some matches. Say you have `old_var`, `old_var2`, and `old_var3`. Just start at the top of the function, and match, match, skip, match, skip, [type and replace]. Sure, you could do a regex on old_var[^\d], but depending on what you're matching and what you're skipping it sometimes is just not worth puzzling out the right regex.

    • dhruvmittal 13 years ago

      I've been playing with it a bit and I've been seeing the same thing. I do like that this gives me a little flexibility, so I'll probably continue to use multiple cursors for the smaller stuff and the more traditional vim methods for anything larger.

    • edanm 13 years ago

      losvedir is right, in ST, "Find" will automatically create separate cursors and takes regex's.

      I used to also have plugin for ST1, which allowed you to do common things like selecting a line, then splitting it by a separator. For example, you'd select a line, split it by ", ", and get a cursor on each paramater. It worked wonderfully for these kinds of things.

  • tmaOP 13 years ago

    Yep, the only reason why I wrote this was because of the missing live feedback functionality in existing Vim features, and it's the one feature that the plugin can't do without. I'm certainly not advocating that this be a replacement for regexp or macros, but for certain small tasks like quickly renaming a variable inside a function, it's much easier on my brain.

ParadigmComplex 13 years ago

I threw my own attempt at this on HN a ways back, but it didn't get any traction: https://news.ycombinator.com/item?id=4906343

Mine supports things like multi-key commands and has predictable (if not entirely desirable) undo behavior, but it has issues in its own right. I figure we may both learn a thing or two from each other's attempts.

Vim really does need this feature to get some mindshare back from ST2, and I'd love to see this well-implemented, even if its by someone else.

grn 13 years ago

I must admit that I see little value in it given great capabilities that Vim already has. The three example edits can be made using regular expressions and macros.

rane 13 years ago

The rendering of multiple cursors is really buggy in tmux + vim, to the extent that it's hard to tell what has been selected.

I have used this command for the purposes of changing a variable name. It could be better but gets the job done most of the time.

> :.,$s/\<<C-r><C-w>\>//gc<Left><Left><Left>

Short demo: http://cl.ly/image/403j153F3u1h

  • susi22 13 years ago

    I do this: *Ncw (then change the variable name) Then just n.n.n.n. until things are replaced.

farslan 13 years ago

That's awesome tma. Thank you for hacking on this. I'll plan to include this into subvim (ST2 like Vim) :)

  • flipcoder 13 years ago

    subvim? :O awesome idea (although I wouldn't use it since I'm so used to my vim setup, but I could see lots of people using it)

    I'm linking it here so people can find it: https://github.com/fatih/subvim

  • tmaOP 13 years ago

    Thanks! I was planning on pinging you to check it out, but it looks like it found its way to you already. Also thanks for jumping in and finding problems already!

  • lttlrck 13 years ago

    I am looking forward to trying subvim this on Linux.

cab9485 13 years ago

Thank you for posting this. I didn't even read the article but my mind is a black hole when it comes to remembering names and I have been forgetting Sublime for a while now.

nkuttler 13 years ago

It looks to me like using a regex is more practical, something like

:10,20s/poorly_named_var/nice_var/g

  • joesb 13 years ago

    If you do nothing more than a scoped search-and-replace then regexp may be more practical. But if the edit action also involve moving cursors around after selection, or when some match should be skipped then it takes less effort to use multiple cursors to edit than trying to comes up with regexp and edit command that accomplished what you need.

  • deckiedan 13 years ago

    I wish vim had a visual regex multi-replace tool, like the visual search tool. So matches would select as you typed, and changes would update in real time too...

alan-saul 13 years ago

Looks great, I was actually about to start working on an identical project today!

pw7 13 years ago

Looks great tma! Will give this a spin as soon as I can.

gokce 13 years ago

I don't need to get jealous of sublime anymore..

ashleyblackmore 13 years ago

Thank you Terry, this is great

neduma 13 years ago

Awesome. Gonna try it.

aeon10 13 years ago

what is the font used in the example gifs

jQueryIsAwesome 13 years ago

Slightly related: If you are using ST2 I recommend the "Clipboard Manager" plugin to have multiple clipboards, it gets anything that is copied inside ST2 or any other software, it works with multiple cursors which is really handy.

  • jjjjjosh 13 years ago

    I just tested this with native copy-paste, and - at least on OSX - as long as whatever you're pasting has the same number of lines as you have cursors, you'll get that same line-to-cursor mapping.

    That being said, let me also recommend my favorite clipboard history tool, Jumpcut (http://jumpcut.sourceforge.net/).

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection