15

There is an option --ignore which allows specifying files to ignore. At the moment I only managed to ignore multiple files by doing --ignore file1 --ignore file2.......

Trying to use --ignore "*assets*|*scripts*" does nothing. So is there a catch I'm not aware of?

T.Chmelevskij
  • 253
  • 1
  • 2
  • 7

3 Answers3

19

You could use brace expansion e.g.

ag pattern --ignore={'*assets*','*scripts*'}  path_to_search

or, as Glenn suggests here, process substitution:

ag pattern -p <(printf "*%s*\n" assets scripts) path_to_search
don_crissti
  • 79,330
  • 30
  • 216
  • 245
  • 1
    Completely forgot about expansion feature. Still wondering why man says `--ignore PATTERN`. Naturally would expect `PATTERN` to be regex... – T.Chmelevskij May 03 '17 at 22:15
  • 1
    @T.Chmelevskij - not really, when it comes to file names, PATTERN almost always means SHELL PATTERN... – don_crissti May 03 '17 at 22:31
3

format is --ignore pattern_to_exclude

➜  proj git:(develop) ✗ ag User -l  | wc
     82      82    2951
➜  proj git:(develop) ✗ ag User -l --ignore 'tests*' | wc
     65      65    2348

proof

➜  exp tree                                              
.
├── good.py
├── migrations.py
├── test2.py
├── test_another.py
└── tests.py

➜  for i in *.py; do echo "User" > $i; done 
➜  exp ag -l --ignore 'test*' --ignore 'migrations*' User
good.py

so only one file good.py has been returned, all others got filtered due to pattern

DmitrySemenov
  • 755
  • 7
  • 16
  • You are only ignoring filenames with the exact name of `tests` here. The question was how to ignore multiple patterns. – T.Chmelevskij Jun 11 '17 at 05:59
  • you can submit another --ignore, like **ag User -l --ignore tests --ignore migrations** – DmitrySemenov Jun 11 '17 at 06:50
  • Thanks for fixing it, but please read the whole question when answering. _At the moment I only managed to ignore multiple files by doing --ignore file1 --ignore file2......._ question was on how to shorthand that. Shell expansion is the best solution seen so far. So your own solution could be shortened to `ag -l --ignore={'test*','migrations*'} User` – T.Chmelevskij Jun 11 '17 at 07:38
  • for some reason that didn't work for me (ag 0.33 + zsh) that's the reason I have added additional answer what worked for me. I read your question before answering. – DmitrySemenov Jun 11 '17 at 19:43
  • Brace expansion is shell feature, not `ag`. Also I'm using `zsh` myself. Are you sure you don't have space somewhere in `--ignore={'test*','migrations*'}`? You can simply try `echo --ignore={'test*','migrations*'}` and it should print out those things. – T.Chmelevskij Jun 12 '17 at 13:37
  • I'll check one more time :) – DmitrySemenov Jun 12 '17 at 20:30
2

You could add

*assets*
*scripts*

to your .gitignore or .ignore file.

From the readme:

It ignores file patterns from your .gitignore and .hgignore.
If there are files in your source repo you don't want to search, 
just add their patterns to a .ignore file. 
laktak
  • 5,616
  • 20
  • 38
  • I am aware of `.ignore` files. But changing file every time just to exclude directory for one search is not really convenient. – T.Chmelevskij May 09 '17 at 09:42