1

Reading (and re-reading) the Vim docs on when :h cmdline-special expansion happens versus :h `= expansion, the :grep example below has me stumped:

:let &grepprg = "echo 'grep: $*'"          " Adjust 'grepprg' for ease of inspection.
:grep % <cWORD> `="string"`
:!echo 'grep: % <cWORD> `="string"`'       " Note: This produces identical output.

Assuming a current file name of current-file.txt and that the cursor is on WORD current-WORD, the :grep command above outputs:

grep: current-file.txt current-WORD `="string"`

The first two arguments to :grep (interpreted by :h cmdline-special) were expanded, but the `= expression was not. Since :grep can accept file name arguments, can someone please clarify why only :h cmdline-special expansion was performed? My research into the docs is documented below.


:h cmdline-special states that strings like % and <cWORD> are expanded "at places where a file name can be used":

In Ex commands, at places where a file name can be used, the following characters have a special meaning. These can also be used in the expression function expand().

This explains why commands which do not expect a file name (e.g. :normal) do not perform this expansion:

:normal %          " Has same effect as typing "%" in normal mode

Now, looking at the docs for :h `=, the first line states

You can have the backticks expanded as a Vim expression, instead of as an external command, by putting an equal sign right after the first backtick, e.g:

   :e `=tempname()`

which seems to suggest that :h `= expansion applies in the same context as :h `-expansion. :h `-expansion states:

On Unix and a few other systems you can also use backticks for the file name argument, for example:

    :next `find . -name ver\\*.c -print`
    :view `ls -t *.patch  \| head -n1`

Thus, it seems that all forms of expansion mentioned in this post (:h cmdline-special, :h `=, :h `-expansion) apply to all file name arguments, but the :grep example illustrated that it is not so.

Is my interpretation of the docs incorrect or is there more to the notion of "file name argument"? Short of looking at the Vim source code, is there another resource which addresses this discrepancy?

1

Actually, it's pretty simple. "Backtick" only works where a file argument stands (like "wildcards", not "cmdline-special"), see :h backtick-expansion.

For example, backtick works for :edit, :lcd, :argadd, etc.

4
  • So, youre arguing that vim isn’t sure the non-first argument to :grep is a filename? Since OP points out that for other arguments the expansion is valid – D. Ben Knoble Jan 5 at 13:48
  • @D.BenKnoble As far as I understand, vim makes a difference between "cmdline-specials" and "wildcards". "Backticks" behave like "wildcards", that is, they are expanded only by (some) internal commands, while being passed onto the shell (:h :!cmd) unexpanded. – Matt Jan 5 at 14:47
  • It looks like the key is {file} in the help; these things only apply to that, if I read correctly – D. Ben Knoble Jan 5 at 15:15
  • @D.BenKnoble Yes, you can put it like this: where vim expects file argument, where help says "{file}", and where vim performs "wildcard" expansion. That all must be the same place. – Matt Jan 5 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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