What does `$*`(a dollar sign followed by a star) mean in the default setting of `grepprg` in vim?

268 views Asked by At

In a vanilla vim on Mac, when I type :set grepprg?, it returns the following:

grepprg=grep -n $* /dev/null.

I understand what -n and /dev/null means, thanks to an old question here. I also understand what $ and * means individually. However, I am not sure what to make of $*. I tried to look it up in the vim doc, but all that I could find was

The placeholder "$*" is allowed to specify where the arguments will be included.

I sense that I am missing some important connection here. I would really appreciate if someone could explain to me how $* works as a placeholder.

Update: Thanks to the detailed explanation from @romainl, I realized that I was misinterpreting $* as regex, whereas they are part of the convention in shell script. In fact, there already exists old post about this particular convention. Silly me!

1

There are 1 answers

1
romainl On BEST ANSWER

I'm not sure what kind of explanation is needed beyond what you have already quoted:

The placeholder "$*" is allowed to specify where the arguments will be included.

$* is just a placeholder and it works like all placeholders: before being actually sent to the shell, the command is built out of &grepprg and $*, if present, is replaced by any pattern, filename, flags, etc. provided by the user.

Say you want to search for foo\ bar in all JavaScript files under the current directory. The command would be:

:grep 'foo\ bar' *.js

After you press <CR>, Vim grabs any argument you gave to :grep, in this case:

'foo\ bar' *.js

then, if there is a $* in &grepprg, it is replaced with the given argument:

grep -n 'foo\ bar' *.js /dev/null

or, if there is no $* in &grepprg, the given argument is appended to it, and only then sends the whole command to a shell.

$* means "in this command, I specifically want the user-provided arguments to appear here".

As for the meaning of $*$ and * have no intrinsic meaning and $* could have been $$$PLACEHOLDER$$$ or anything. $* may have been chosen because it is used in shell script to represent all the arguments given to a function or script, which is somewhat close in meaning to what is happening in &grepprg with $*.