16

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?

4 Answers 4

20

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

6
  • You are only ignoring filenames with the exact name of tests here. The question was how to ignore multiple patterns. Jun 11, 2017 at 5:59
  • you can submit another --ignore, like ag User -l --ignore tests --ignore migrations Jun 11, 2017 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, 2017 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, 2017 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, 2017 at 13:37
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. 
1
  • I am aware of .ignore files. But changing file every time just to exclude directory for one search is not really convenient. May 9, 2017 at 9:42
1

Coming a bit late on this thread.

In ag, --ignore doesn't support regular expression. So if you want to ignore all files with say ".json" extension, you need to use --ignore='*.json' rather than that --ignore='.*.json'

If you want to ignore multiple file extensions, you can do the following: ag --ignore='*.yaml' --ignore='*.json'

The above will ignore files with yaml and json extensions.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .