Wikia

Vim Tips Wiki

Changes: Git grep

Edit

Back to page

(Move categories to tip template)
(Remove html character entities)
 
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
'''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.
+
'''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>
 
<pre>
 
func GitGrep(...)
 
func GitGrep(...)
let save = &amp;grepprg
+
let save = &grepprg
 
set grepprg=git\ grep\ -n\ $*
 
set grepprg=git\ grep\ -n\ $*
 
let s = 'grep'
 
let s = 'grep'
Line 21: Line 21:
 
endfor
 
endfor
 
exe s
 
exe s
let &amp;grepprg = save
+
let &grepprg = save
 
endfun
 
endfun
command -nargs=? G call GitGrep(&lt;f-args&gt;)
+
command -nargs=? G call GitGrep(<f-args>)
 
</pre>
 
</pre>
   
 
You can also limit searching to files matching a pattern (git will do the pattern matching):
 
You can also limit searching to files matching a pattern (git will do the pattern matching):
 
 
<pre>
 
<pre>
:G &lt;pattern&gt; -- '*.c'
+
:G <pattern> -- '*.c'
 
</pre>
 
</pre>
   
 
===Additions===
 
===Additions===
 
The following addition will run git grep on the word under the cursor when '''Ctrl+X G''' is pressed.
 
The following addition will run git grep on the word under the cursor when '''Ctrl+X G''' is pressed.
 
 
<pre>
 
<pre>
 
func GitGrepWord()
 
func GitGrepWord()
Line 40: Line 38:
 
call GitGrep('-w -e ', getreg('z'))
 
call GitGrep('-w -e ', getreg('z'))
 
endf
 
endf
nmap &lt;C-x&gt;G :call GitGrepWord()&lt;CR&gt;
+
nmap <C-x>G :call GitGrepWord()<CR>
 
</pre>
 
</pre>
   
 
==Comments==
 
==Comments==
Thanks for cleaning this tip. I have just converted it to use our TipImported template, and have used <tt><nowiki><pre>...</pre></nowiki></tt> for the code (more robust than a space indent in the long term).
 
 
I removed the "Possibly needs its own tip" html comment on <tt>GitGrepWord()</tt> because I see no reason to split this quite small tip. Also, we are holding discussions in plain view (like this comment) to make it easier for people trying to clean up the imported tips (see the [[Vim_Tips_Wiki:Discussion_guidelines|discussion guidelines]] for why we don't use the talk page).
 
 
I think this tip should very briefly mention what git is (particularly since it's not a traditional tool).
 
 
FYI Ephemeral comments, such as this, will be deleted later, although we like to keep the <tt><nowiki>==Comments==</nowiki></tt> section to encourage people to comment here rather than on the talk page.
 
 
--[[User:JohnBeckett|JohnBeckett]] 00:34, 8 December 2007 (UTC)
 
 
----
 

Latest revision as of 00:08, September 30, 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'

AdditionsEdit

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>

CommentsEdit

Around Wikia's network

Random Wiki