The article mentions ”gn” in passing, but it doesn’t give it enough credit: this is an enormously useful motion, almost more useful on its own than the rest of the article. The thing that makes it golden is that it is a motion, and thus combined with a command, it is repeatable! So, like: 1. Search for something (either using / or *). 2. Type ”cgn” in Normal mode (i.e. change next search match) and replace what you s…
Why is this preferable to :%s?
A Vim Guide for Advanced Users
11–20 of 179 posts
Re: A Vim Guide for Advanced Users
#12The article mentions ”gn” in passing, but it doesn’t give it enough credit: this is an enormously useful motion, almost more useful on its own than the rest of the article. The thing that makes it golden is that it is a motion, and thus combined with a command, it is repeatable! So, like: 1. Search for something (either using / or *). 2. Type ”cgn” in Normal mode (i.e. change next search match) and replace what you s…
To solve this I wrote a little plugin (that I finished as a plugin yesterday and uploaded just now): https://github.com/orlp/vim-quick-replace
It requires vim-repeat (and a patched one because the current one has an issue): https://github.com/orlp/vim-repeat
The nice thing is that it always jumps to the next match before replacing, and if you choose to replace the current match it immediately jumps to the next one, so that you can evaluate whether you want to replace it.
You can start in two quick ways: using the current word under the cursor or the current visual selection, so it's very flexible. For me replacing a variable name is often just putting my cursor on it, pressing space + r, typing the replacement name and then a couple .'s, n's and if I mess up some u's.
To see it in action: https://i.imgur.com/wEX6O1w.mp4
Re: A Vim Guide for Advanced Users
#13Re: A Vim Guide for Advanced Users
#14The article mentions ”gn” in passing, but it doesn’t give it enough credit: this is an enormously useful motion, almost more useful on its own than the rest of the article. The thing that makes it golden is that it is a motion, and thus combined with a command, it is repeatable! So, like: 1. Search for something (either using / or *). 2. Type ”cgn” in Normal mode (i.e. change next search match) and replace what you s…
Awesome, that's definitely something I'll add to my bag of tricks. But > This replaces, like, 97% of uses of :s and is a much more convenient way to do search/replace, especially if you use * to search for the word under the cursor. It’s the bee’s knees! I'm not really sure about that. I can definitely see it replacing :s for a small number of repetitions, but for a large number. I think using :s with a range is quic…
Re: A Vim Guide for Advanced Users
#15How does gI differ from I?
Re: A Vim Guide for Advanced Users
#16Superb submission! I've been using vim for over a decade and I never knew about gx or gf. Thanks!
Very handy when using a terminal from within vim and looking at compiler errors/warnings or log files etc.
Re: A Vim Guide for Advanced Users
#17Capital marks. I use ma or ms and `a `s a lot, but did not know you could do global marks with mA and `A. Very useful!
I've been using `` for a long time to mean "go back to the last place I was editing" but sometimes it gets overridden when navigating (I'm not super familiar with the jumplist, so maybe I need to get a better understanding) but `[ and `] are exactly what I've been looking for all these years.
Re: A Vim Guide for Advanced Users
#18Thank you! Two things I really got from this: Capital marks. I use ma or ms and `a `s a lot, but did not know you could do global marks with mA and `A. Very useful! I've been using `` for a long time to mean "go back to the last place I was editing" but sometimes it gets overridden when navigating (I'm not super familiar with the jumplist, so maybe I need to get a better understanding) but `[ and `] are exactly what…