34

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.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230
  • I didn't find any related options for vimgrep. Now I use grep instead. – Morgan Cheng Dec 14 '09 at 07:22
  • You could use grep instead vimgrep. [Here](http://amix.dk/blog/post/175) explain how to use with script called MyGrep –  May 26 '11 at 12:50

5 Answers5

39

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
Edmond Burnett
  • 4,829
  • 3
  • 18
  • 17
  • 1
    This doesn't seem to have any effect. I've done `:set wildignore+=node_modules/**,.git/**` and yet my searches are still looking through node_modules and .git for results. `vim --version | grep wildignore` showed me that my vim was compiled with wig support, so I'm not sure what's going on here. – James M. Lay Apr 07 '19 at 04:54
  • Ah, nevermind. My macbook's shell apparently doesn't support the `**` expansion. In other shells you may have to do `shopt -s globstar` before you can use double-star expansions like this. – James M. Lay Apr 07 '19 at 05:02
  • I wish this worked, but alas it doesn't for me. Running Vim version 8 on Ubuntu. Added `set wildignore+=node_modules/**,.git/**` to vimrc and still `e **/foo ` still searches through the current directory's `node_modules`. – papiro Jul 26 '19 at 21:06
  • 2
    wildignore+=*/node_modules/** seems to work on my machine (windows) – sotto Jan 24 '20 at 21:16
  • works on archlinux :+1 – D.B.K Sep 26 '22 at 13:31
21

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
Jp_
  • 5,973
  • 4
  • 25
  • 36
12

You could try ack instead. It integrates nicely with vim and has lots of options for doing the sort of thing you want to do.

There are several ack-vim integrations on GitHub. For example: here.

LazyCat01
  • 957
  • 7
  • 23
Andy Stewart
  • 5,013
  • 2
  • 28
  • 38
2

For example in Ubuntu just

sudo apt-get install ack-grep

sudo ln -s /usr/bin/ack-grep /usr/bin/ack

then install http://www.vim.org/scripts/script.php?script_id=2572

and now add next line to your .vimrc

noremap <C-f> :copen<CR>:Ack --ignore-dir #first_ignore_dir# --ignore-dir #second_ignore_dir# -ai 
  • its open search frame by Ctr+F, have fun
IvanM
  • 2,913
  • 2
  • 30
  • 30
0

Seem to also ignore node_modules folder on root and if node_modules is in sub directories.

set wildignore+=*/node_modules/**
Jan
  • 302
  • 3
  • 5