50

According to the docs, it should be

--ignore PATTERN

I have a file containing tags, named "tags". I have tried the following, each of them still searches through the tag file..

ag -Qt --ignore ".*tags" "asdf"

ag -Qt --ignore .*tags "asdf"

ag -Qt --ignore "tags" "asdf"

ag -Qt --ignore tags "asdf"

ag -Qt --ignore *tags

and none of them works.

If I use what's suggested here, then ag doesn't accept it at all

I tried to work around it by renaming it to temp.tags and using *.tags pattern to try and ignore it, but it still doesn't work.

Any ideas?

Jacob Wang
  • 4,411
  • 5
  • 29
  • 43

10 Answers10

42

Put the list of files to exclude in .agignore.

Note: as @DenilsonSáMaia mentioned, .agignore will be deprecated in favor of .ignore geoff.greer.fm/2016/09/26/ignore

Mukesh Soni
  • 1,119
  • 3
  • 13
  • 23
  • 3
    Great answer. For more details on .agignore see: https://github.com/ggreer/the_silver_searcher/wiki/Advanced-Usage – Janos Jun 14 '16 at 07:37
  • 2
    `.agignore` will be deprecated in favor of `.ignore`: http://geoff.greer.fm/2016/09/26/ignore/ – Denilson Sá Maia Jan 16 '17 at 16:23
  • 1
    `.agignore` works with ag version 2.0.0, check source https://github.com/ggreer/the_silver_searcher/blob/e5230abd98633e9bd408dfec9d26df30db09334b/src/options.c#L681 – michalzuber Aug 28 '17 at 13:37
  • you can add the .ignore file globally as ~/.ignore too (rather than at the project root level). Alternatively, you can add the file or folder to exclude to .gitignore instead. – arcseldon Jun 02 '19 at 14:05
  • 2
    `.agignore` is the correct file. It's 2021, and it hasn't deprecated yet.. likely never will. `.ignore` would be a terrible name anyway, as you wouldn't really know what it belongs to, and there'd be a likelihood of another app using it. – ffxsam Mar 16 '21 at 04:08
  • 1
    I just tried using `.agignore` (with ag 2.2.0) and it didn't work; renaming it to `.ignore` worked. – anol Jun 29 '21 at 21:36
  • @anol I had the opposite behaviour with the same version of `ag` as you (2.2.0) on OS X. (c.f. https://github.com/ggreer/the_silver_searcher/issues/1097) Not sure what is going on! – Att Righ Jul 15 '21 at 11:25
  • The thing I always miss when setting up on a new computer is that you must place the `.agignore` in the _home directory_ (i.e. `~/.agignore`) not in the root of your application. – gabe Oct 25 '22 at 17:20
  • .ignore works in the root of the application – Christian Stewart May 02 '23 at 22:30
21

Add just multiple --ignore, at least this works for me:

ag -Qt --ignore ".*tags" --ignore asdf

If you don't put quotes it's interpreted as directory if you put quotes as PATTERN

NexusStar
  • 373
  • 3
  • 8
16

I've found that --ignore doesn't take a regex.

This should help:

ag --ignore="*_test.rb" "SomeAwesomeClass"
Artur Małecki
  • 1,426
  • 16
  • 14
  • This is the behavior that I see as well. Your comment helped me the most. It seems to treat the string as a glob pattern if it has special characters and as a literal directory if it doesn't. Quotes don't seem to change it's behavior at all – Tariq C Nov 04 '21 at 02:07
11

After some research, it seems that it is a known issue documented here. Where if you do an --all-text (-t) search it'll override --ignore since it's searching for all texts. This issue is present for --unrestricted too.

Jacob Wang
  • 4,411
  • 5
  • 29
  • 43
  • I found this answer combined with the answer from @ArturMałecki covered why --ignore didn't work for me. – ken hicks Jan 12 '21 at 16:40
5

As of v2.2.0 (most likely earlier versions as well; I'm just going off the version I have) all these answers don't seem to work. What did work for me was:

ag searchterm --ignore=*.log --ignore=*.txt

Note the = after the ignore option.

youngrrrr
  • 3,044
  • 3
  • 25
  • 42
2

You can also create .ignore files to ignore things that are in your source repository. .ignore uses the same patterns as .gitignore and .hgignore. Using .ignore can drastically improve search speeds. .

If you want a global .ignore file, consider adding this alias:

alias ag='ag --ignore ~/.ignore' 

to your ~/.bash_profile (or similar) file. there is also

to temporarily disable vcs ignores you can run with --skip-vcs-ignores

random-forest-cat
  • 33,652
  • 11
  • 120
  • 99
1

For me, the following works (in ag version 0.18.1):

ag --ignore TAGS;*.pdf;*.json "search_term"
jco
  • 1,335
  • 3
  • 17
  • 29
1

I just placed .agignore file into my user root folder and it works.

By default, ag will ignore files matched by patterns in .gitignore, .hgignore, or .agignore. These files can be anywhere in the directories being searched. Ag also ignores files matched by the svn:ignore property in subversion repositories. Finally, ag looks in $HOME/.agignore for ignore patterns. Binary files are ignored by default as well.

https://manpages.ubuntu.com/manpages/trusty/man1/ag.1.html

Lajos
  • 2,549
  • 6
  • 31
  • 38
0

I tried the link you posted (using a glob instead of regex), but removed the '=' sign, and it worked.

fferen
  • 499
  • 3
  • 8
-1

Have you tried using single quotes? I know i've definitely been stumped by using double quotes and no quotes only to find that single quotes worked.

ag -Qt --ignore '*tags'
sorin
  • 161,544
  • 178
  • 535
  • 806
Bluecakes
  • 2,069
  • 17
  • 23