sponsor Vim development Vim logo Vim Book Ad

basic Tip #825: Expand #* search to grep-find

 tip karma   Rating 29/8, Viewed by 1300 

created:   November 19, 2004 17:13      complexity:   basic
author:   mosh at albany      as of Vim:   5.7

When #/* will not find a word under-cursor,
use the macro 'g/' given below, to expand the
same search with gnu-grep to dirs/files.

Search results are shown in a small window (quickfix mode),
use c-n/c-p to move between results.

Uses gnu-grep on PC/Unix to search vim \<regexp\> in files/trees.

Based on 'grep-find' which is a emacs commands.

" Put this in ~/_vimrc and use g/ to repeat current search as a grep-find.
" Usage
"   /xyz   .. not found in current file, so lets look for it in *.* with
"   g/     .. search for xyz in *.*, next lets look for it in the whole tree
"   :call Mosh_grep("../..")
"   :call Mosh_grep("/usr/include","strstr")
" GNU-grep -recursive,nocase,linenum,noerror,VimRegexp (in that order).
:set  grepprg=grep\ -rinsE

:map g/  :call Mosh_grep()<CR>

function! Mosh_grep(...)
    if a:0 == 0
        :exec "grep '".@/."' *.*"
    elseif a:0 == 1
        :exec "grep '".@/."' " a:1
    elseif a:0 == 2
        :exec "grep" a:2 " " a:1
    endif
    " Optional mappings for easy navigation of results
    :map <c-n>  :cnext<CR>
    :map <c-p>  :cprev<CR>
    :copen
endfunction

- Mohsin  
http://www.cs.albany.edu/~mosh

 rate this tip  Life Changing Helpful Unfulfilling 

<<Choose smaller font in vimdiff mode. | PHP manual in VIM help format >>

Additional Notes

[email protected], November 24, 2004 6:20
Good tip!
Wouldn't it be helpful to constrain the files to be grepped? E.g. only *.c and *.h files when editing C source?
=> find -name "*.c" | xargs grep "MyFunction"
Or at least, ignoring *~ and *.bak  files?
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
    stats
Sponsored by Web Concept Group Inc. SourceForge.net Logo