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?


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
  • 1
    Completely forgot about expansion feature. Still wondering why man says --ignore PATTERN. Naturally would expect PATTERN to be regex... May 3 '17 at 22:15
  • 1
    @T.Chmelevskij - not really, when it comes to file names, PATTERN almost always means SHELL PATTERN... May 3 '17 at 22:31

You could add


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. 
  • I am aware of .ignore files. But changing file every time just to exclude directory for one search is not really convenient. May 9 '17 at 9:42

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


➜  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

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

  • You are only ignoring filenames with the exact name of tests here. The question was how to ignore multiple patterns. Jun 11 '17 at 5:59
  • you can submit another --ignore, like ag User -l --ignore tests --ignore migrations Jun 11 '17 at 6: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 Jun 11 '17 at 7: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. 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. Jun 12 '17 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.