An easy way to issue a git grep command across a git repository and navigate between the results.
- Alternative (or addition) to ctags and cscopse.
- No need to refresh index.
- No dependencies.
- Cross language.
-
Install using your favorite package manager, e.g., Vundle:
- Add the following to your .vimrc:
Plugin 'eranfrie/gitgrep.vim'
. - Reload .vimrc.
- Run:
:PluginInstall
.
- Add the following to your .vimrc:
-
Manual installation: copy
gitgrep.vim
to your plugin directory (e.g.,~/.vim/plugin/
in Unix / Mac OS X).
Down
/Up
/PageDown
/PageUp
to navigate the menu.Enter
to select a result and jump to it.Esc
/Ctrl-C
to cancel.Backspace
to remove the last character from the pattern.- Any other key to add a character to the pattern.
-
GitGrep(flags, pattern)
- issue a git grep command and open the selection menu, where flags are git grep flags (can be empty string) and pattern is the pattern to look for. -
GitGrepBack()
- jump back to previous location. -
GitGrepIterPrev()
/GitGrepIterNext()
- iterate to the previous/next match.
- Change the default git grep command
let g:gitgrep_cmd = "grep -r"
- Exclude files using Vim's regex
let g:gitgrep_exclude_files = "<regex>"
E.g., exclude files stsarting witth test
or containing simulation
let g:gitgrep_exclude_files = "^test\\|simulation"
- Set the height (number of lines) of the selection menu
let g:gitgrep_menu_height = 15
- Set the color of the file path
let g:gitgrep_file_color = "blue"
- Set the color of the matched pattern
let g:gitgrep_pattern_color = "red"
- Disable loading the plugin
let g:loaded_gitgrep = 1
Keys are not mapped automatically. You can choose your own mapping,
Some recommendations:
Easily gitgrep for the word under cursor and jump back:
nnoremap <leader>g :call GitGrep("-w", expand("<cword>"))<CR>
nnoremap <leader>t :call GitGrepBack()<CR>
Create mappings with common grep flags:
command -bang -nargs=* GG call GitGrep("", expand(<q-args>))
command -bang -nargs=* GGw call GitGrep("-w", expand(<q-args>))
command -bang -nargs=* GGi call GitGrep("-i", expand(<q-args>))
Create mappings to iterate to the previous and next matches:
nnoremap <leader>p :call GitGrepIterPrev()<CR>
nnoremap <leader>n :call GitGrepIterNext()<CR>
Most basic usage - find all instances of "test":
:GG test
To use special characters such as space, escape the pattern:
:GG "def test"
Regex:
:GG def.*test
Additional grepping (can be used to filter files):
:GG def | grep test.py