4 ms·
When I need to edit the code around next five instance of "foo", but not every instance in the open file, then I don't know how to do that with structural regul
by wodenokoto 2y ago
When I need to edit the code around next five instance of "foo", but not every instance in the open file, then I don't know how to do that with structural regular expression, but pressing ctr+d five times and then edit what I want is really straight forward.
And I need to edit around a limited version of foo way more often than I need to edit all instances.
- globular-toast 2y agoYou would restrict your regexp to a region. In Emacs you just select a region somehow (like your press ctrl+d five times), then just do the regexp replace.
- vulcan01 2y agoNotice that you also don't need to write a regex with multiple cursors - instead, you can visually see your selections and changes as you go, using the same editing motions that you would typically use.
- msravi 2y agoIn vim, to replace in the next 5 lines (not instances) :.,.+5s/findtext/replacetext/g In neovim, you'll see a preview of the changes as you type the regexp. Neovim is really good.
- buster 2y agoIt's mostly a matter of taste. I do use multiple cursors quite often in Sublime Text when needing to edit some CSV or JSON file. It's probably the feature of Sublime Text, why i'm still having the editor around. It work's extremely well. I could type ":.,.+5s/findtext/replacetext/g" in vim (and remember the syntax!) or i just could do "ctrl-d, ctrl-d, ctrl-d, ctrl-d, ctrl-d, replacetext" and have a visual, immediate confirmation on what i am doing. But i bet you, that i'll press ctrl-d 5 times faster than you entering ":.,.+5s/".
- deleted 2y ago[deleted]
- samatman 2y agoI more commonly solve a problem like this one with /findtext<CR>creplacetext<Esc>n.n.n.n. Remembering the syntax is not a problem here. Learning it in the first place, on the other hand... I do like multiple cursors though. I end up switching to VSCode a couple times a week to do things where it's the right tool for the job.
- iudqnolq 2y agoWhat about holding down the shift arrow, pressing the down arrow five times, then ctrl-r? In most IDEs replace with an active selection will default to replacing only in the selection.
- johnisgood 2y agoYou do not have to remember the syntax; you can use Vimscript and you can create a custom keybinding that prompts you for the "findtext" and "replacetext" strings, then apply the search-and-replace operation over the next 5 lines (number of lines is easily adjustable and can be prompted for it, too; it does not have to be hard-coded either). Vim users don't have to choose between precision and ease of use as you can build macros or mappings that match ANY workflow, creating powerful, customized processes that minimize the need to remember exact syntax or type it all out, for example achieving results similar to multiple cursors but with Vim's inherent efficiency and scalability. So, while Sublime Text's multiple cursors are handy, Vim can match - and even exceed - this functionality with very little setup (emphasis: one time setup!). (I use VSCodium (which I really enjoy) for Go, PHP, and Elixir; IntelliJ IDEA for Java and Kotlin, and I use both Emacs and Vim for everything else).
- kstrauser 2y agoI speak vim, but vastly prefer multiple cursors when the editing is less trivial. Say you want to turn "foo bar" # For the 1st line "baz qux" # For the 2nd line into "bar" # For the foo key "qux" # For the baz key With multi cursors, you could select foo and baz and then interactively edit the rest of the line. I'm capable of doing that with vim, but by the time I'd even settled on an approach for it, I'd already be done in the other editor. I could use Emacs macros to do the same kind of thing, and I've probably used `C-x (` a zillion times to build procedures I could then run 30 more times on the following lines. For quick interaction, I still prefer multi cursors.
- skydhash 2y agoFor longer distance, I use search navigation, faster than scanning where to put the cursors.
- bla3 2y agoVim can do multiple cursors as long as they start on the same column: ff<ctrl-v>jdw
- Arch-TK 2y agoFor this specific example it's a visual block select of <foo > / <baz > cut and then visual select of <1st > / <2nd > and paste. But that's relying on things being the right width. This is two lines, if everything was a different length, I think by the time I got multiple cursors to do this, I would have long managed by just doing the same editing task twice (cut overwrite-paste then cut overwrite-paste). If I had to do this for many more lines I would have just used a substitution: :s/\(.*\)"\(\w\+\) \(\w\+\)" # \(.*\)\(\d\+\w\+\).\*/\1"\3" # \4\2 key/ And yes, that's 100% unreadable, but it was easy to write with the preview mode on modern neovim. Although in this case I think a macro would probably be what I would settle on. This macro would look like: ^wdw/the<ret>wvawpwciwkey<esc>j (<ret> and <esc> are ^M and ^[ but I replaced them to avoid confusion) But these are already the motions you would need to use to keep the multiple cursors in alignment. The thing is though that if you screw up it's usually easier to unscrew yourself when it's just one line than when you've gone ahead and screwed up 20 times. Yes it's still just an undo but when I've used multiple cursors, especially when you're trying to break up lines, you're forced to deal with that breaking up happening in all cursors. Things quickly get overwhelming. Multiple cursors are effectively (in my experience) just macro recording with automatic playback simultaneously at all the positions you care about. Whereas if you just record a macro and apply it wherever you would otherwise place your cursors, you get the same result without having the visual mess of dealing with multiple cursors. And it's just as error prone as macros.
- eviks 2y agoBut then you'd have to count beforehand and have no good way to correct instead of just going off that sweet immediate visual feedback
- tasty_freeze 2y agoVim, and I'm sure neovim, allow visually selecting an area by lines. If you then type ":" to initiate a search and replace, it will apply only to those lines.
- eviks 2y agoWhy would I want to go via this roundabout way? What happens if I decide to add/remove one more line to the mix later?
- brabel 2y agoI think even the people advocating these stupid keyboard combinations don't really use them... they just think that it makes them sound smart.
- tasty_freeze 2y agoThere is a way to express your opinion without insulting people about something you have no way of knowing. As a simple matter of fact, I have been using vim since the mid-ish 90s every day and do exactly what you claimed don't really do.
- brabel 2y agoI hate to break it to you but you don't. I know that because there have been studies trying to show how using the keyboard is more efficient than the mouse. Every time, even when people swear they don't use the mouse to navigate text files, they do, and they do it a lot, nearly as much as anyone else. It's a great demonstration to how you just can't trust people's testimonies as they lie while actually believing their own lies (i.e. the really believe to be telling the truth which is fascinating). I am proficient in emacs and don't need the mouse to do anything, but I am conscious I still reach for the mouse when more complex movements are required because it would be extremely dogmatic and unproductive to do otherwise, as it's just so much easier, and there's absolutely no shame in using the right tool for the job.
- specialist 2y agoI'd watch this twitch stream. Contemporary editors are like 5-axis CNC mills and I'm still banging the rocks together. My occasional use multiple cursors is very basic. Your strategy wouldn't even occur to me. Ages ago, Suresh Bhavanani (sp?) studied (suboptimum) usage of office and CADD applications. He was hired by the Army Corps of Engineers to help determine why adoption of CADD hadn't resulted in either higher productivity or improved quality. His theory, which I believe, is various people have various "strategies" of varying merit. But for some reason users didn't (couldn't) readily learn better techniques from each other. IIRC, His two proposals were 1) re-organize user interfaces around tasks, instead of a features and 2) update training materials emphasize strategies. IIRC, he compared manual and digital drafting. He identified a bunch of implicit and explicit "strategies" for manual work. eg Using ink and mylar, skilled drafters would work right-to-left, to avoid smudging prior work. Many early adopters carried those manual strategies over to CADD. Suboptimal, right? So then Suresh identified a bunch of digital appropriate strategies, leveraging the new tool. Suresh did the same for spreadsheets. Any way, this is a too long ramble now ending with the point: It'd be nice to see how others organize their work. I'm sure there's a lot of low hanging fruit "strategies". All the day to day tricks that experts use that don't get captured and explained in our training materials.