Live data from Hacker News

A Vim Guide for Advanced Users

thevaluable.dev

11–20 of 179 posts

Re: A Vim Guide for Advanced Users

#11
post #7
post #5

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?

I find it more intuitive, and for a small number of iterations it's definitely faster.

Re: A Vim Guide for Advanced Users

#12
post #5

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…

This has one huge shortcoming: when the next match is off-screen, pressing . jumps immediately to the next match and replaces it, before you had the opportunity to review whether you want that.

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

#14
post #10
post #5

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…

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…

I too wish to add this to my repertoire. I don't think it will replace :s, but occasionally I find myself copying a line to use as a template. When I go to change each line I'm changing a particular part to different things. Assuming I remember, this trick seems like a good approach.

Re: A Vim Guide for Advanced Users

#16
post #2

Superb submission! I've been using vim for over a decade and I never knew about gx or gf. Thanks!

Related, gF will also scroll down to the line number if specified as: File.txt:45

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

#17
Thank 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 I've been looking for all these years.

Re: A Vim Guide for Advanced Users

#18

Thank 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…

I use global marks for things like my .vimrc (`V), .zshrc (`Z), and other config files that I edit semi-regularly. Definitely a useful tool for the box.
Post reply on HN