Vim Tips Wiki
m (Category: VersionControl)
(Remove html character entities)
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=1262
 
|id=1262
  +
|previous=1261
|title=git grep
 
  +
|next=1265
|created=June 18, 2006 3:06
+
|created=June 18, 2006
 
|complexity=basic
 
|complexity=basic
 
|author=Timo Hirvonen
 
|author=Timo Hirvonen
|version=5.7
+
|version=7.0
 
|rating=6/3
 
|rating=6/3
 
|category1=VersionControl
|text=
 
  +
|category2=
This adds ":G <pattern>" command which runs "git grep <pattern>".
 
 
 
 
fun GitGrep(...)
 
 
let save = &grepprg
 
 
set grepprg=git\ grep\ -n\ $*
 
 
let s = 'grep'
 
 
for i in a:000
 
 
let s = s . ' ' . i
 
 
endfor
 
 
exe s
 
 
let &grepprg = save
 
 
endfun
 
 
 
 
command -nargs=? G call GitGrep(<f-args>)
 
 
}}
 
}}
  +
'''git grep <pattern>''' searches for a pattern in a currently selected git branch. This adds ''':G <pattern>''' command to run the command from within Vim.
  +
<pre>
 
func GitGrep(...)
 
let save = &grepprg
 
set grepprg=git\ grep\ -n\ $*
 
let s = 'grep'
 
for i in a:000
 
let s = s . ' ' . i
  +
endfor
 
exe s
 
let &grepprg = save
 
endfun
 
command -nargs=? G call GitGrep(<f-args>)
  +
</pre>
   
 
You can also limit searching to files matching a pattern (git will do the pattern matching):
== Comments ==
 
  +
<pre>
I find Yegappan's grep.vim plugin to be more useful.
 
 
:G <pattern> -- '*.c'
 
  +
</pre>
'''Anonymous'''
 
, June 18, 2006 8:56
 
----
 
there is a vimgrep in 7.0, which does not require external commands. While it is great that there are other grep solutions (and in 6.x they were invaluable!) I think vimgrep is a little less cumbersome to set up and use
 
 
'''Anonymous'''
 
, June 18, 2006 13:36
 
----
 
vimgrep doesn't do the same as git grep. With git grep I can do:
 
 
G pattern
 
 
and it greps for the pattern in every file in the GIT repo. Very useful
 
if you want to find all uses of some symbol for example. It ignores
 
files that are not in the repo.
 
 
You can also limit searching to files matching a pattern (git will do the
 
pattern matching):
 
 
G foo_do_bar -- '*.c'
 
 
You could also grep files in particular tree (revision) but that would be
 
useless with vim ;)
 
 
Timo
 
 
'''Anonymous'''
 
, June 19, 2006 4:11
 
----
 
Addition:
 
 
func GitGrepWord()
 
normal "zyiw
 
call GitGrep('-w -e ', getreg('z'))
 
endf
 
 
nmap &lt;C-x&gt;G :call GitGrepWord()&lt;CR&gt;
 
   
  +
===Additions===
Press ^X G on a word.
 
  +
The following addition will run git grep on the word under the cursor when '''Ctrl+X G''' is pressed.
  +
<pre>
 
func GitGrepWord()
 
normal! "zyiw
 
call GitGrep('-w -e ', getreg('z'))
 
endf
 
nmap <C-x>G :call GitGrepWord()<CR>
  +
</pre>
   
 
==Comments==
'''Anonymous'''
 
, June 20, 2006 15:00
 
----
 
<!-- parsed by vimtips.py in 0.517740 seconds-->
 
[[Category:VersionControl]]
 

Latest revision as of 00:08, 30 September 2008

Tip 1262 Printable Monobook Previous Next

created June 18, 2006 · complexity basic · author Timo Hirvonen · version 7.0


git grep <pattern> searches for a pattern in a currently selected git branch. This adds :G <pattern> command to run the command from within Vim.

func GitGrep(...)
  let save = &grepprg
  set grepprg=git\ grep\ -n\ $*
  let s = 'grep'
  for i in a:000
    let s = s . ' ' . i
  endfor
  exe s
  let &grepprg = save
endfun
command -nargs=? G call GitGrep(<f-args>)

You can also limit searching to files matching a pattern (git will do the pattern matching):

:G <pattern> -- '*.c'

Additions[]

The following addition will run git grep on the word under the cursor when Ctrl+X G is pressed.

func GitGrepWord()
  normal! "zyiw
  call GitGrep('-w -e ', getreg('z'))
endf
nmap <C-x>G :call GitGrepWord()<CR>

Comments[]