Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 'git grep' to work effectively in vim

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>
like image 571
Régis B. Avatar asked Feb 21 '14 10:02

Régis B.


People also ask

How does git grep work?

Git Grep. Git ships with a command called grep that allows you to easily search through any committed tree, the working directory, or even the index for a string or regular expression. For the examples that follow, we'll search through the source code for Git itself.

Which statement is the best comparison between git grep and grep?

The git grep version will only search in files tracked by git, whereas the grep version will search everything in the directory. So far so similar; either one could be better depending on what you want to achieve.

What files are searchable by git grep?

`git grep` command is used to search in the checkout branch and local files.


1 Answers

This works for me:

:command -nargs=+ Ggr execute 'silent Ggrep!' <q-args> | cw | redraw!
like image 172
user149765 Avatar answered Sep 18 '22 18:09

user149765