4 ms·
> "change these two words, but only if it's at the end of a line, and keep the words in between them the same" How do you do this in vim? Could you give an exa
by samb1729 10y ago
> "change these two words, but only if it's at the end of a line, and keep the words in between them the same"
How do you do this in vim? Could you give an example?
I've tried a few times to love vim but emacs keeps pulling me back.
- khedoros1 10y agoI'd think something like: :%s/word1\(.*\)word2$/word3\1word4/ Over each line in the whole file, search for word 1 and word 2 (which is at the end of the file), separated by arbitrary non-newline characters, and change them to word3 and word4.
- juki 10y agoThe Emacs equivalent would be M-x replace-regexp word1\(.*\)word2$ RET word3\1word4 RET Assuming you have `replace-regexp` bound to a key, it's practically the same. Works on the whole buffer (by default), or the region (if selected).
- SwellJoe 10y agokhedoros' example is correct. vim has very sed-like search and replace, so UNIX command line skills translate back and forth. It's doubly useful, to me, that whatever tricks like that I pick up can be done from scripts with little to no changes (vim has a few differences, such as being able to specify which lines to replace either by number or visually or from beginning or end of file, but the matching/replacing logic is very similar). Also, I work in Perl a lot, which also has similar conventions for searching and replacing (though it has a more powerful regex engine with somewhat better syntax, like being able to include comments and newlines), so again, it's a skill that translates reasonably well back and forth. I don't really try to convince people that vim is better than any other thing. I get the appeal of things like Atom and VS Code and Sublime. vim was just the first really power editor I learned really well (on an Amiga, no less), and it's pretty ingrained (and I barely tap into its power...I used it pretty close to stock, and have no clue how to do tons of things without googling it), but it's very fast, for me. Heck, I don't even use hjkl cursor movement most of the time. I grew up on a keyboard with arrow keys, so that's what I reach for. I'm a sloppy vim user, but it's still more productive for me than other editors in many contexts.