Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yeah, just recently I had to edit text like this

  'name1',
  'name2',
  'name3',
to:

  'name1': Enum.Name1,
  'name2': Enum.Name2,
  'name3': Enum.Name3,
by the time I figured out a sequence of keyboard commands I'm pretty sure I could have multicursored it pretty easily, go to name1, create 2 cursors, select word & copy, start typing : Enum., paste selected word, select word, capitalize, type ,

In vim it was something like qdyi'f,i: Enum.<Esc>pbvlUq and then repeat macro.

I found it very slow to even recall.

For some reason the multicursor solution feels faster to me most times.



I'm surprised everyone's forgetting vim already has a proto-multicursor mode. Go into Visual Block mode (Ctrl-V) select all the lines then it's just A<delete ,>: Enum.Name0,

After that you can just select it again in visual mode and g Ctrl-A to get the right numbers.

If anything the g Ctrl-A part makes Vim way better than most multicursor editors.


So the second time, you're entering Visual Block mode or plain Visual mode? What does the g Ctrl+A do?


g Ctrl-A increments all the numbers, but every instance gets incremented by one than the previous. First line would be Enum1, second Enum2, third Enum3. It's a really neat feature imo.

Though I did make a mistake, if you selected everything it'd catch the name numbers too, so you'd have to go into visual block mode and only select the enum parts, then do g Ctrl-A.

That said the numbers probably weren't supposed to be taken seriously now that I think about it.


Thanks


Or, you could have hit qt, recorded your commands for the first change, and then replicated it for every other line with a simple @t, instead of redoing all the changes each time.

You don't have to learn command. You simply need to know how to edit with VIM.

And multi cursors only work for tabular data. The macro recordings can work for the entire document where you call a macro on a word/regex you searched for, for example.


I always find it strange that people recommend a different letter for the register when recommending macros. Why not just use `qq` and `@q` (with the added advantage that clearing the register is `qqq`) ?


The reason I don’t use q is it’s far too easy for me to accidentally hit a 3rd q when I didn’t intend to and not even realize it.

This was a bigger problem when Apple decided to go with those butterfly keys.


> multi cursors only work for tabular data

There are several plugins which allow for more complex use of multiple cursors.

* https://github.com/mg979/vim-visual-multi

* https://github.com/terryma/vim-multiple-cursors


I did record it with qd...q and repeated it with @d @@


Visual select and normal@t (assuming you saved it in t) is a good way to do it once for every line.


Nope, you can do find > select all to get multiple selections and edit those simultaneously.


This can be done in (neo)vim too, and with a bit better precision and less typing.

:g/<regex>/<command>

is the general form for [g]rabbing lines that match a regex and applying a command over them. You can also chain the command:

:g/<regex1>/g/<regex2>/<command>

I'll admit this isn't exactly something you intuit as easily as a drop-down menu, but (neo)vim does have tools for all these things out of the box.


Huh, somehow it’s never occurred to me to get binary logic by chaining :g and :v in this way—I’ve always done things in more complicated ways to make up for the lack.


Multiple cursors is for quick small editing tasks where you don't bother with a macro.


You probably are familiar, but for some newer folks: I almost always add a "go to beginning of next line" as the end of the macro so that I can e.g. 10@t to repeat it 10 times. Makes it much quicker than @t on each line once you have more than a few.


This looks like the sort of thing you would intuitively solve with visual selection and a regex.

```

    Vjj:s/\v'(.*)',/'\1': Enum.\u\1,/
```

This is all just muscle memory. It seems impossible until you get used to it, and then it's the most natural thing in the world.


TIL s/\u, thanks!

I know I’ve read that part of the documentation before, but probably not for over five years. In cases where I’ve wanted something like that, I’ve tended to reach for macros, visual block editing, and s/\=. A few days ago I did a somewhat more complicated one that s/\U wouldn’t be sufficient for:

  :'<,'>s/.*/\="    pub const ".substitute(toupper(submatch(0)), "[^A-Z0-9]\+", "_", "g").": &'static str = \"".submatch(0)."\";"


Vim's documentation is complete and very well-written. I've been using the editor for over 10 years and I still learn new things when perusing :help.


Yes! When I first came from Sublime 2 I thought Vim was irritatingly difficult but hoped it would be worth it. Now I wouldn't program without it.


I think I would've used something like :s/'\(.*\)',/'\1': Enum.\1,/g and then ctrl-v to block-select all the first characters and U to uppercase them.

Vi's not always about finding the most efficient way to do something, but having a composable language to do tricks like these comfortably builds up into your own dialect over time.


I’m often fallback on the sed match-replace strategy for things like this.


For me it's been a matter of practice, and forcing myself to use macros for things even when I was too slow with them to justify it in the moment. It pays off eventually and you can get a nice facility with it.

I also like using

  :g/^/norm $normal_commands
to apply anonymous macros across all lines. It works with visual selections too:

  :'<,'>g/^/norm $normal_commands
And naturally you can replace the ^ with any regex to selectively apply the macro to only lines that match!


Column editing in vim: https://youtu.be/dYDu9Hrg7yM?t=639

I saw a colleague doing the same thing (turn column of #defines into structs in an array) with multiple cursors in an IDE, took him much longer.

I paused to take a drag from my smoke while doing that.


Sorry but column editing is a stripped-down multicursor mode. I use vscode-vim (so, VSCode with Vim mode) and never use column editing because the IDE multicursor is much more powerful. Without being any sort of editing wizard, I'm finding your video example slow and mildly painful to watch


I would have used visual block mode to duplicate the 123 column and insert the remaining content for all three lines simultaneously. If the situation is a bit more complex, I sometimes use regex replace applied to a line range.


> vlU

I think you meant just vU, as you were only capitalising the letter under the cursor.

You can actually also achieve this without entering visual mode: gU{motion} changes to uppercase, so gUl uppercases the letter under the cursor.


Text like that should work well with block selection/block insert mode.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: