14

Fugitive.vim allows me to run git grep commands; unfortunately, the results are not stored in a quickfix-list, so I need to run :cw after a :Ggrep in order to have an easily parseable result list.

I would like to type :Ggr "def my_function" instead of:

:Ggrep "def my_function"
:cw

How can I define this :Ggr command in my .vimrc file?

EDIT

Once the :Ggr command is defined, I can map to git grep on the word under the cursor, which is really awesome:

nnoremap <C-F> :Ggr <cword><CR>
Régis B.
  • 10,092
  • 6
  • 54
  • 90
  • possible duplicate of [VIM - multiple commands on same line](http://stackoverflow.com/questions/3249275/vim-multiple-commands-on-same-line) – Etheryte Feb 21 '14 at 10:14
  • 1
    Note that I will have to pass the argument of :Ggr to :Ggrep... So it's not as simple as running two commands in a row. – Régis B. Feb 21 '14 at 10:16
  • 6
    `:Ggrep` does fill the `quickfix` list however the window does not automatically open. Maybe you want the `quickfix` window to open after any grep invocation, `autocmd QuickFixCmdPost *grep* cwindow`, as suggested on the [vim-fugitive FAQ section](https://github.com/tpope/vim-fugitive#faq). – Peter Rincker Feb 21 '14 at 14:15

3 Answers3

13

You can use the <args> symbol to insert the arguments given to your custom command:

:command -nargs=+ Ggr execute 'Ggrep' <q-args> | cw

Note: As the :Ggrep command doesn't have the -bar argument, it cannot be chained, so :execute has to be used.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • This is almost perfect: the Ggrep command opens a window that needs to be closed before the cw command is run, so I have to press to close it. Any idea how to improve this? – Régis B. Feb 21 '14 at 10:37
  • You can emulate that key press via `... | execute "normal \" | cw`, or (perhaps easier) try `... | close | cw`. – Ingo Karkat Feb 21 '14 at 10:39
  • That doesn't seem to work, I still have the "Press ENTER of type command to continue" message that is displayed following the :Ggrep command. – Régis B. Feb 21 '14 at 10:47
  • Oh, that hit-enter prompt, not a window; I misunderstood; yeah, that's difficult to avoid. – Ingo Karkat Feb 21 '14 at 10:49
  • Too bad. Your answer is sufficient anyway. – Régis B. Feb 21 '14 at 10:57
13

This works for me:

:command -nargs=+ Ggr execute 'silent Ggrep!' <q-args> | cw | redraw!
user149765
  • 154
  • 1
  • 3
  • 6
    You could have added that as a comment to my answer, since it's only a small tweak to it. Many would consider what you did bad form. At least document the changes and why they are necessary. – Ingo Karkat Dec 31 '14 at 21:59
  • 1
    This seems not to be needed anymore. As of 2022, and as per Fugitive's doc, this should be enough: `command -nargs=+ Ggr execute 'Ggrep! -q' ` – introiboad Oct 18 '22 at 08:26
2

Already mentioned in one of the comments, but I thought the plugin-recommended solution on the fugitive.vim site deserved its own answer:

autocmd QuickFixCmdPost *grep* cwindow

(from: https://github.com/tpope/vim-fugitive)

zzxyz
  • 2,953
  • 1
  • 16
  • 31