Vim Tips Wiki

Temporarily replace grep with a custom program

Redirected from VimTip688

1,623pages on
this wiki
Add New Page
Add New Page Talk0
Tip 688 Printable Monobook Previous Next

created March 28, 2004 · complexity intermediate · author lpen · version 6.0

Sometimes you want to use quickfix for some things other than grep and make. You can always replace one of them and then return to what it was.

So, I have in my vimrc:

fu! Mycscope(func)
  let tmp1=&grepprg
  let tmp2=&grepformat
  set grepformat=%f\ %*[a-zA-Z_0-9]\ %l\ %m
  set grepprg=cscope\ -R\ -L\ -3
  exe "grep ".a:func
  exe "set grepprg=".escape(tmp1,' ')
  exe "set grepformat=".escape(tmp2, ' ')
command -nargs=* CScope :silent call Mycscope("<args>")

This will create the command CScope, that does a cscope's "find functions calling this function" with quickfix.

Another example: lid

fu! Mylid(arg)
  let tmp1=&grepprg
  set grepprg=lid\ -Rgrep\ -s\ $*
  exe "grep ".a:arg
  exe "set grepprg=".escape(tmp1," ")
command -nargs=* Lid :silent call Mylid("<args>")

Bug: Sometimes Vim is unable to come back from the command, and you have to hit CTRL+C. I have no idea why.


The last two lines in your Mycscope can be simply

let &grepprg=tmp1
let &grepformat=tmp2

script#949 is based on this tip.

Also on Fandom

Random Wiki