Vim Tips Wiki

Repeat search using grep

Revision as of 03:32, March 6, 2009 by (Talk)

1,619pages on
this wiki
Tip 825 Printable Monobook Previous Next

created November 19, 2004 · complexity basic · author mosh · version 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 an 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
  " Optional mappings for easy navigation of results
  :map <c-n> :cnext<CR>
  :map <c-p> :cprev<CR>


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"
=> find -name "*.c" -o -name "*.h" | xargs grep "MyFunction"

Or at least, ignoring *~ and *.bak files?

Around Wikia's network

Random Wiki