6 ms·
Yeah, just recently I had to edit text like this 'name1', 'name2', 'name3', to: 'name1': Enum.Name1, 'name2': Enum.Name2, 'name3': Enum.Name3, b
by knuthsat 5y ago
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.
- mpalmer 5y agoFor 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!
- chousuke 5y agoI 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.
- heatmiser 5y agoI’m often fallback on the sed match-replace strategy for things like this.
- addicted 5y agoOr, 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.
- michaelmior 5y ago> 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/mg979/vim-visual-multi * https://github.com/terryma/vim-multiple-cursors https://github.com/terryma/vim-multiple-cursors
- knuthsat 5y agoI did record it with qd...q and repeated it with @d @@
- isaacremuant 5y agoVisual select and normal@t (assuming you saved it in t) is a good way to do it once for every line.
- continuational 5y agoNope, you can do find > select all to get multiple selections and edit those simultaneously.
- ThatGeoGuy 5y agoThis 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.
- chrismorgan 5y agoHuh, 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.
- Scarbutt 5y ago
- br3akaway 5y agoText like that should work well with block selection/block insert mode.
- deleted 5y ago[deleted]
- mFixman 5y agoThis 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.
- ARandomerDude 5y agoYes! 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.
- chrismorgan 5y agoTIL 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)."\";"
- mFixman 5y agoVim'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.
- layer8 5y agoI 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.
- vukgr 5y agoI'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.
- cassepipe 5y agoSo the second time, you're entering Visual Block mode or plain Visual mode? What does the g Ctrl+A do?
- vukgr 5y agog 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.
- cassepipe 5y agoThanks
- lelanthran 5y agoColumn editing in vim: https://youtu.be/dYDu9Hrg7yM?t=639 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.
- williamdclt 5y agoSorry 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
- chrismorgan 5y ago> 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.