Live data from Hacker News

A Vim Guide for Advanced Users

thevaluable.dev

121–130 of 179 posts

Re: A Vim Guide for Advanced Users

#122

Nice, but still wondering if I should know vim since nano is enough for me :)

I know the basics because vi ships with practically ever *nix (unlike Emacs), so it's there for when I need to edit config files, but I do 97% of my real work in something more like IntelliJ, so there just aren't big returns for getting better at vim.

Re: A Vim Guide for Advanced Users

#123
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…

That does sound useful, but I do this with `:s/foo/bar/gc`, then I press `y` each time, which is pretty similar to pressing `.` each time in your example. I do like that the `cgn` approach works better for something you searched for without knowing a priori that you were going to do a replacement on it.

Re: A Vim Guide for Advanced Users

#124
Those commands may look useless at first sight, but combined can create powerful routines. As an example, my routine to refactor non-typesafe codebases.

- Find the pattern that you want to replace (the last dot is for current dir)

:grep -r 'pattern' .

- Open the quickfix window and check if matches are correct

:copen

- Find and replace the entries of the quickfix list

:cdo s/pattern/replacement/ | update

Re: A Vim Guide for Advanced Users

#125
post #64

Earlier quoted context omitted.

Oh I never thought about that either. Thank you.

I rarely actually do it because f and t motions, possibly with a number, can usually handle these cases. For example, c3tA to change up to but not including the third capital A after the current position

If it's more than one 'A' I'd use visual mode. I'm always impressed people can accurately count up to like three ;)

Re: A Vim Guide for Advanced Users

#126

Nice, but still wondering if I should know vim since nano is enough for me :)

I know the basics because vi ships with practically ever *nix (unlike Emacs), so it's there for when I need to edit config files, but I do 97% of my real work in something more like IntelliJ, so there just aren't big returns for getting better at vim.

The Vi bindings in IntelliJ-based IDEs are pretty damn good in my experience. Certainly I feel a lot happier { and }'ing my way around, and having my `ce` and `gg` and `C`, in Rider.

Re: A Vim Guide for Advanced Users

#128
post #107
post #103

Earlier quoted context omitted.

It's CTRL+X and then N to not save changes

Thanks! Gonna try to remember that combo.

You don't even need to remember it. At the bottom it lists keyboard shortcuts, and on the left is "^X Exit". When you press it with a modified file, it gives you:

    Save modified buffer?                                                                               
    Y Yes
    N No           ^C Cancel

Re: A Vim Guide for Advanced Users

#129
post #115

Earlier quoted context omitted.

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

Vim's sort is handy on Windows where the option to shell out usually doesn't exist. Additionally you have the full power of Vim regex to sort on subsets of lines.

Re: A Vim Guide for Advanced Users

#130
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…

I've done this by setting the / register with * as you mentioned, but I still use the :s command, type / and then use the ctrl-r / key combination to paste the contents of the / register, then type the second / and then the replacement.

The g and z set of commands are pretty useful in vim.

Post reply on HN