MyGrep: Simplistic grep in Vim
When programming it is useful to search for an occurrence of a word, and grep is one hell-of-a application for that. Grep is integrated in Vim, but the integration is lacking. I have tried to use following:
When using vimgrep it isn't possible to pipe and exclude .svn matches, and vimgrep is slow compared to GNU grep. grep.vim looks like a good script, but the Rgrep command does not work (I get a weird temp error). Anyway, I sat down and cooked my own little function. It can:
UsageThe signature is: MyGrep <options> <pattern> <dir> Where options is optional, while pattern and dir are required. Standard options are -rsinI, you can do grep --help to see what they do. ExampleThis will perform a recursive search for cow in ~/Desktop/: :MyGrep -r "cow" ~/Desktop/*
ImplementationYou can dump this function in your .vimrc or in a mygrep.vim file and place it under your VIM plugin directory:
function! MyGrep(...)
if a:0 < 2
echo "Usage: MyGrep <options> <pattern> <dir>"
echo 'Example: MyGrep -r "cow" ~/Desktop/*'
return
endif
if a:0 == 2
let options = '-rsinI'
let pattern = a:1
let dir = a:2
else
let options = a:1 . 'snI'
let pattern = a:2
let dir = a:3
endif
let exclude = 'grep -v "/.svn"'
let cmd = 'grep '.options.' '.pattern.' '.dir. '| '.exclude
let cmd_output = system(cmd)
if cmd_output == ""
echomsg "Pattern " . pattern . " not found"
return
endif
let tmpfile = tempname()
exe "redir! > " . tmpfile
silent echon '[grep search for "'.pattern.'" with options "'.options.'"]'."\n"
silent echon cmd_output
redir END
let old_efm = &efm
set efm=%f:%\\s%#%l:%m
execute "silent! cgetfile " . tmpfile
let &efm = old_efm
botright copen
call delete(tmpfile)
endfunction
command! -nargs=* -complete=file MyGrep call MyGrep(<f-args>)
Code
·
VIM Editor
•
9. Sep 2006
|
|