1

I accidentally stumbled on a solution for a problem I had: I wanted to get an overview over all occurrances of the word under the cursor, after I used # to find them individually.

For some reason I guessed the command :g % and it did what I wanted. But I could not find an explanation of this, neither by reading :help :g nor :help g nor :help % nor :help pattern nor :help cword.

I experimented some more with this and I figured out

  • that it had nothing to do with the cursor position and it would just repeat the last search,
  • that I could replace the % with any single character (or at least, all that I tried worked), but there had to be one, and there had to be at least one space between :g and that character.

For example, :g a, :g x, :g #, :g c all worked.

Typing one more character would use this as a start of a new search apparently.

Where can I find the documentation for this invocation? :help :g shows :g/{pattern}/[cmd] and does say that the / can be replaced by a different character, so I figured it could be a space, but then the syntax should be

:g<space>{pattern}<space>[cmd]

and I understand that the default cmd is :p which prints the matching lines, but I still don't understand why the last search pattern is inserted automatically and what the relevance of that trailing other character (%, a, x above) is.

I'm using Vim 8.2.2434.

1 Answer 1

1

The way I understand it:

:g[global] /{pattern}/ [cmd]
  1. You can have spaces between the global keyword and the pattern separator
  2. The pattern separator can be any non alphanumeric character (except \, | or ")
  3. The ending pattern separator is optional if not cmd is to be specified
  4. If the pattern is empty the last pattern (/ register) is used
  5. If the cmd is not specified p (print) is used

With these rules :g# is equivalent to :g##p or :g//p or :g#Ctrl-r/#p

1
  • Remark: on Vim 8.2 and later :g a is not working with Vim 8.1 it is working. Feb 21 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.