Live data from Hacker News

A Vim Guide for Advanced Users

thevaluable.dev

111–120 of 179 posts

Re: A Vim Guide for Advanced Users

#111
post #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): ht…

IMO the "correct" way to handle this is with vanilla vim's built-in macros. Find the next match, record a macro making the edit you want, then find the next match and finish recording the macro. The macro starts with the cursor over the match you want, the edit can be more complex than a simple replacement, and skipping is simply a matter of pressing `n` instead of `@@`.

Vim can also take a command that yields a list of filenames as a launch parameter via something like `vim $(ag -l target_string)`, letting you go through each file with a `:n`, letting you complete large refactors surprisingly quickly.

Re: A Vim Guide for Advanced Users

#112
post #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): ht…

But if you're not sure you wanted it, you can just press u to undo and n to skip.

Since Vim keeps an undo tree, you are guaranteed not to lose anything anyway.

Re: A Vim Guide for Advanced Users

#113
post #110

The number increment example doesn't seem to work for me. It says to start with this: 1. a 1. b 1. c The post says to "select in VISUAL mode the last two lines and hit `g CTRL+a`". So I guess with shift+v select the bottom two lines, and then I hit g, space, and Ctrl+a. I get: 1. a 2. b 2. c The post says that the last line should now start with 3 instead of 1, or in my case 2. The behavior is the same as when omitti…

Don't press space. "g" and then "ctrl-a"

Re: A Vim Guide for Advanced Users

#114
post #110

The number increment example doesn't seem to work for me. It says to start with this: 1. a 1. b 1. c The post says to "select in VISUAL mode the last two lines and hit `g CTRL+a`". So I guess with shift+v select the bottom two lines, and then I hit g, space, and Ctrl+a. I get: 1. a 2. b 2. c The post says that the last line should now start with 3 instead of 1, or in my case 2. The behavior is the same as when omitti…

The docs for this is in `:help v_CTRL-A`.

It should actually say VISUAL BLOCK mode. This is where you use `CTRL-v` to select a block of text. You can read more in `:help blockwise-visual`.

Also see the 'nrformats' option: `:help 'nrformats'`. You can include alphabetic characters to do the same thing with A-Z, for example.

https://vimhelp.org/change.txt.html#v_CTRL-A

Re: A Vim Guide for Advanced Users

#115

This was a really nice refresher and I learned a few new cool tricks (like gx). I'm a bit surprised that the 'u' flag for sort wasn't mentioned. That's the most useful one in my mind and I use it almost every time.

My thoughts exactly about ':sort u'

The u option making it keep only unique lines, for anyone else left wondering.

Personally, instead of re-learning how to do stuff with vim-sepecific commands, I just use the existing command line tool, especially since I don't do this on a daily/weekly basis (but I use sort in command line pipes at least weekly). The command is literally "sort" and for unique you just pass the -u flag, e.g. select something in Vim and type :!sort -u

Re: A Vim Guide for Advanced Users

#116
post #110

The number increment example doesn't seem to work for me. It says to start with this: 1. a 1. b 1. c The post says to "select in VISUAL mode the last two lines and hit `g CTRL+a`". So I guess with shift+v select the bottom two lines, and then I hit g, space, and Ctrl+a. I get: 1. a 2. b 2. c The post says that the last line should now start with 3 instead of 1, or in my case 2. The behavior is the same as when omitti…

Did you try not pressing space?

Re: A Vim Guide for Advanced Users

#118
post #110

The number increment example doesn't seem to work for me. It says to start with this: 1. a 1. b 1. c The post says to "select in VISUAL mode the last two lines and hit `g CTRL+a`". So I guess with shift+v select the bottom two lines, and then I hit g, space, and Ctrl+a. I get: 1. a 2. b 2. c The post says that the last line should now start with 3 instead of 1, or in my case 2. The behavior is the same as when omitti…

Did you try not pressing space?

evidently not :)

Re: A Vim Guide for Advanced Users

#119
post #110

The number increment example doesn't seem to work for me. It says to start with this: 1. a 1. b 1. c The post says to "select in VISUAL mode the last two lines and hit `g CTRL+a`". So I guess with shift+v select the bottom two lines, and then I hit g, space, and Ctrl+a. I get: 1. a 2. b 2. c The post says that the last line should now start with 3 instead of 1, or in my case 2. The behavior is the same as when omitti…

The docs for this is in `:help v_CTRL-A`. It should actually say VISUAL BLOCK mode. This is where you use `CTRL-v` to select a block of text. You can read more in `:help blockwise-visual`. Also see the 'nrformats' option: `:help 'nrformats'`. You can include alphabetic characters to do the same thing with A-Z, for example. https://vimhelp.org/change.txt.html#v_CTRL-A

It seems to work in both visual block mode and visual line mode for me, for what it's worth. (When not pressing space as mentioned in another reply.)

Re: A Vim Guide for Advanced Users

#120
post #110

The number increment example doesn't seem to work for me. It says to start with this: 1. a 1. b 1. c The post says to "select in VISUAL mode the last two lines and hit `g CTRL+a`". So I guess with shift+v select the bottom two lines, and then I hit g, space, and Ctrl+a. I get: 1. a 2. b 2. c The post says that the last line should now start with 3 instead of 1, or in my case 2. The behavior is the same as when omitti…

Don't press space. "g" and then "ctrl-a"

Thank you!
Post reply on HN