开发者

techniques in git grep and vim [closed]

Closed. This question needs to be more focused. It is not cu开发者_开发百科rrently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed last year.

Improve this question

I code in vim.

I use git; and love git grep.

Does anyone have a particularly nice set of techniques / scripts for using git grep inside of vim?


You do have this VIM extension (created by Timo Hirvonen back in 2006!)

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
  copen
  let &grepprg = save
endfun
command -nargs=? G call GitGrep(<f-args>)

As noted by dafnahaktana in the comments:

let s = 'grep' is replaced with let s = '!grep', to avoid automatically jumping to the first match (why would I want to?).
And also add the command copen after the command exe s, so that the matching list automatically opens.

Then:

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>


Check also fugitive.vim - A Git wrapper so awesome, it should be illegal
(that is its official tag-line ;) ), from Tim Pope (see the Git repo project):

It includes, amongst many other commands, one for a git grep:

call s:command("-bar -bang -nargs=? -complete=customlist,s:EditComplete Ggrep :execute s:Grep(<bang>0,<q-args>)")
call s:command("-bar -bang -nargs=* -complete=customlist,s:EditComplete Glog :execute s:Log('grep<bang>',<f-args>)")

function! s:Grep(bang,arg) abort
  let grepprg = &grepprg
  let grepformat = &grepformat
  let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd '
  let dir = getcwd()
  try
    execute cd.'`=s:repo().tree()`'
    let &grepprg = s:repo().git_command('--no-pager', 'grep', '-n')
    let &grepformat = '%f:%l:%m'
    exe 'grep! '.a:arg
    let list = getqflist()
    for entry in list
      if bufname(entry.bufnr) =~ ':'
        let entry.filename = s:repo().translate(bufname(entry.bufnr))
        unlet! entry.bufnr
      elseif a:arg =~# '\%(^\| \)--cached\>'
        let entry.filename = s:repo().translate(':0:'.bufname(entry.bufnr))
        unlet! entry.bufnr
      endif
    endfor
    call setqflist(list,'r')
    if !a:bang && !empty(list)
      return 'cfirst'
    else
      return ''
    endif
  finally
    let &grepprg = grepprg
    let &grepformat = grepformat
    execute cd.'`=dir`'
  endtry
endfunction


Still in the same set of Git encapsulation (including git grep), you have:

MinSCM : An abstract front-end for Mercurial/Git/Bazaar , from Takeshi NISHIDA:
See his Git repo

alt text http://bitbucket.org/ns9tks/vim-minscm/wiki/log.png

:MinSCMGrep[!]                  (Default mapping: \sg)

Searches for specified pattern and creates a |quickfix| list.

Used SCM commands ~

  • hg : grep -n
  • git : grep -n -e
  • bzr : (unavailable)


You also have this set of vim commands, from the Git project git grep vim, from tjennings.
(inspired from the ack.vim : Plugin for the Perl module / CLI script 'ack' from Miles Sterrett)

let g:gitgrepprg="git\\ grep\\ -n"

function! GitGrep(args)
    let grepprg_bak=&grepprg
    exec "set grepprg=" . g:gitgrepprg
    execute "silent! grep " . a:args
    botright copen
    let &grepprg=grepprg_bak
    exec "redraw!"
endfunction

function! GitGrepAdd(args)
    let grepprg_bak=&grepprg
    exec "set grepprg=" . g:gitgrepprg
    execute "silent! grepadd " . a:args
    botright copen
    let &grepprg=grepprg_bak
    exec "redraw!"
endfunction

function! LGitGrep(args)
    let grepprg_bak=&grepprg
    exec "set grepprg=" . g:gitgrepprg
    execute "silent! lgrep " . a:args
    botright lopen
    let &grepprg=grepprg_bak
    exec "redraw!"
endfunction

function! LGitGrepAdd(args)
    let grepprg_bak=&grepprg
    exec "set grepprg=" . g:gitgrepprg
    execute "silent! lgrepadd " . a:args
    botright lopen
    let &grepprg=grepprg_bak
    exec "redraw!"
endfunction

command! -nargs=* -complete=file GitGrep call GitGrep(<q-args>)
command! -nargs=* -complete=file GitGrepAdd call GitGrepAdd(<q-args>)
command! -nargs=* -complete=file LGitGrep call LGitGrep(<q-args>)
command! -nargs=* -complete=file LGitGrepAdd call LGitGrepAdd(<q-args>)


I found a gist that open results from git grep in a quickfix window to jump around:

<C-x><C-x> runs grep for the word under the cursor
:G <word> runs grep 
:Gi <word> runs grep as case-insensitive

Just add the content of the gist to your ~/.vimrc file

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜