4 ms·
One of my favourite things about Kakoune is global regex replacement. In Vim, it usually goes like: - Type `:%s/` - Spend 30 seconds thinking up a regex which
by thristian 8y ago
One of my favourite things about Kakoune is global regex replacement. In Vim, it usually goes like:
- Type `:%s/`
- Spend 30 seconds thinking up a regex which will hopefully match all the things I want to change, and none of the things I don't
- Write it out, hit Enter
- Run my tests, or run "git diff", or generally review the changes, and find a bunch of stuff is broken
- undo, restart from scratch
You can sometimes avoid screwing up the entire file with the "c" flag that asks for confirmation for each change, but it's too easy to get bored and distracted and type "y" too many times.
In Kakoune, a global regex replacement goes like:
- Type `%s`
- Spend five seconds thinking up a regex that approximately matches the things I want to change
- Interactively do any additional adjustments that are difficult to express in regex, like the `M` command that extends the selection to the next matching bracket
- Cycle through matches with `(` and `)`, checking to see if they're what I expected
- If not, I can remove individual selections with Alt-Space, or just hit %s and adjust my regex
- Type `c` and actually make the change
Because of the whole "multiple cursors" thing, I can get fast feedback on each part of the process, so I can get the result I want much more quickly.
- gopher_protocol 8y agoI'm not arguing that Kakoune's way isn't better (I think it probably is). But you can sort of do this in Vim as well, minus the more advanced interactive adjustments: - :set hlsearch - Type `/` - Spend five seconds thinking up a regex that approximately matches the things I want to change - Hit Enter - Cycle through matches with `n`/`N`, checking to see if they're what I expected - If they're not, press `/` then <Up> and edit the regex - When satisfied, type `:%s//`, enter the replacement you want to make, then `/g<Enter>` to apply the change everywhere
- Deimorz 8y agoYou can also just change the command to something like :%s/regex/replacement/gc The "c" modifier makes you confirm each replacement, and you can y/n for each of them.
- sevensor 8y agoMuch as I love kak, multiple selections can work against you for this use case. If the file is big enough, and there are enough matches, kak falls over long before vim breaks a sweat. I'm happy to use vim for one thing and kak for another.
- cesnja 8y agoI'm not sure what's the library providing the functionality, but regex replace is super nice in the Spacemacs emacs distribution. It takes the same arguments as vim, but shows and highlights the changes your regex will do while you're writing it out.