Getting 'git grep' to work effectively in vim
Asked Answered
F

3

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>
Forensic answered 21/2, 2014 at 10:4 Comment(3)
possible duplicate of VIM - multiple commands on same lineDowery
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.Home
: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.Hyacinthus
B
13

This works for me:

:command -nargs=+ Ggr execute 'silent Ggrep!' <q-args> | cw | redraw!
Balfore answered 31/12, 2014 at 12:40 Comment(2)
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.Bobbyebobbysocks
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' <q-args>Bickart
B
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.

Bobbyebobbysocks answered 21/2, 2014 at 10:28 Comment(5)
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 <enter> to close it. Any idea how to improve this?Home
You can emulate that key press via ... | execute "normal \<CR>" | cw, or (perhaps easier) try ... | close | cw.Bobbyebobbysocks
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.Home
Oh, that hit-enter prompt, not a window; I misunderstood; yeah, that's difficult to avoid.Bobbyebobbysocks
Too bad. Your answer is sufficient anyway.Home
B
13

This works for me:

:command -nargs=+ Ggr execute 'silent Ggrep!' <q-args> | cw | redraw!
Balfore answered 31/12, 2014 at 12:40 Comment(2)
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.Bobbyebobbysocks
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' <q-args>Bickart
R
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)

Recife answered 16/7, 2018 at 19:33 Comment(1)
This is correct. However, this requires to press <return> once after the :Ggr command, which is inconvenient.Home

© 2022 - 2024 — McMap. All rights reserved.