0

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!

chunyun
  • 13
  • 3

1 Answers1

1

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 $*.

romainl
  • 158,436
  • 18
  • 236
  • 264
  • Thank you very much for the detailed explanation! I think I was overthinking about the `$*`. When I said "I understand what they mean" in my original post, I was interpreting them individually as `$` --- end of line and `*` --- zero or multiple instances of the character to its left, as in regex. But apparently I was not aware of the conventional use of `$*` in shell script. – chunyun Apr 22 '21 at 16:28