Wikia

Vim Tips Wiki

Changes: Temporarily replace grep with a custom program

Edit

Back to page

(Move categories to tip template)
(Remove html character entities)
 
Line 18: Line 18:
 
<pre>
 
<pre>
 
fu! Mycscope(func)
 
fu! Mycscope(func)
let tmp1=&amp;grepprg
+
let tmp1=&grepprg
let tmp2=&amp;grepformat
+
let tmp2=&grepformat
 
set grepformat=%f\ %*[a-zA-Z_0-9]\ %l\ %m
 
set grepformat=%f\ %*[a-zA-Z_0-9]\ %l\ %m
 
set grepprg=cscope\ -R\ -L\ -3
 
set grepprg=cscope\ -R\ -L\ -3
Line 26: Line 26:
 
exe "set grepformat=".escape(tmp2, ' ')
 
exe "set grepformat=".escape(tmp2, ' ')
 
endf
 
endf
command -nargs=* CScope :silent call Mycscope("&lt;args&gt;")
+
command -nargs=* CScope :silent call Mycscope("<args>")
 
</pre>
 
</pre>
   
Line 35: Line 35:
 
<pre>
 
<pre>
 
fu! Mylid(arg)
 
fu! Mylid(arg)
let tmp1=&amp;grepprg
+
let tmp1=&grepprg
 
set grepprg=lid\ -Rgrep\ -s\ $*
 
set grepprg=lid\ -Rgrep\ -s\ $*
 
exe "grep ".a:arg
 
exe "grep ".a:arg
 
exe "set grepprg=".escape(tmp1," ")
 
exe "set grepprg=".escape(tmp1," ")
 
endf
 
endf
command -nargs=* Lid :silent call Mylid("&lt;args&gt;")
+
command -nargs=* Lid :silent call Mylid("<args>")
 
</pre>
 
</pre>
   
Line 49: Line 49:
   
 
<pre>
 
<pre>
let &amp;grepprg=tmp1
+
let &grepprg=tmp1
let &amp;grepformat=tmp2
+
let &grepformat=tmp2
 
</pre>
 
</pre>
   

Latest revision as of 09:12, September 29, 2008

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, ' ')
endf
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," ")
endf
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.

CommentsEdit

The last two lines in your Mycscope can be simply

let &grepprg=tmp1
let &grepformat=tmp2

script#949 is based on this tip.


Around Wikia's network

Random Wiki