Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

for example suppose I did

:let foo=pattern

and now I want to perform Ggrep patter but use variable foo instead of the literal patter string.

:echo foo

outputs pattern, but

:Ggrep foo

just looks for foo


building a string of command and then running :execute on it is not a solution, its a hack. And it breaks with any non-trivial variable values.

share|improve this question
what is Ggrep (capital G?)? – akira Aug 8 '11 at 11:30
git grep from 'fugutive' vim plugin – Vitaly Kushner Aug 9 '11 at 10:08
up vote 1 down vote accepted

It's not about variable but maybe vim abbreviate command can helps. Try from command line:

:abbreviate foo pattern


:Ggrep foo<space>

It will complete 'foo' to your 'pattern'.

share|improve this answer

what about:

:execute ':grep ' . foo
share|improve this answer
how do I properly escape foo's content? it might contain special characters like ", ', /, \, etc – Vitaly Kushner Aug 8 '11 at 11:34
@Vitaly Kushner: See ":help escape()" and ":help fnameescape()". – garyjohn Aug 8 '11 at 16:45
I know escape(), but not sure about what characters to escape – Vitaly Kushner Aug 10 '11 at 13:00
fnameescape() is not good, its not a filename argument, its an arg to plugin function. suppose I :let foo = "aaa\"bbbb'cccc" – Vitaly Kushner Aug 10 '11 at 13:02
I am pulling my hair out over this too. You can't issue a command like let xyz = tabpagenr() then tabnext xyz. Vim's scripting language is as flawed as its modal environment is flawless. – puk Mar 1 '12 at 7:01

If you don't like the :exe solution, you can "read" the contents of a variable into to the command line by using the = expression register. For example, type :Ggrep then press Ctrl-r and then type =foo and press Enter. Assuming the variable foo contained "pattern", your command line should now look like:

:Ggrep pattern

This has the advantage that you can see the actual command that will be run, and even modify it before pressing Enter a second time.


:help "=
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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