Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude file patterns in vimgrep?

Tags:

vim

vimgrep

In vim, I do search with vimgrep frequently. I have mapping like below:

map <leader>s :execute "noautocmd vimgrep /\\<" . expand("<cword>") . "\\>/gj **/*.*" <Bar> 
cw<CR> 5

The problem is that there are some temporary subfolders (like obj, objd) that I don't want to search for. How can I exclude subfolders matching given patterns. For example, subfolders with prefix "objd" should not be included in searching.

like image 234
Morgan Cheng Avatar asked Dec 14 '09 04:12

Morgan Cheng


2 Answers

As of Vim 7.3.570, you can use wildignore to exclude patterns with vimgrep.

For example, to ignore the objd subfolder:

:set wildignore+=objd/**

Additional exclusions can be added by separating patterns with a comma:

:set wildignore+=objd/**,obj/**,*.tmp,test.c

See Vim's help documentation for a few more details.

:help wildignore
like image 156
Edmond Burnett Avatar answered Oct 02 '22 04:10

Edmond Burnett


As showed in http://vimcasts.org/blog/2013/03/combining-vimgrep-with-git-ls-files/ you could instead of exclude files, include the files you want to search. So you can search in the files tracked by Git with

:noautocmd vimgrep /{pattern}/gj `git ls-files`

In this way you are not searching the files stated in the .gitignore.


I use it so much I created a command for that, so I just need to

:Sch {pattern}

and I did it by adding the following line to my .vimrc

command -nargs=1 Sch noautocmd vimgrep /<args>/gj `git ls-files` | cw
like image 18
Jp_ Avatar answered Sep 30 '22 04:09

Jp_