52

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?

10 Answers 10

44

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

9
22

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

17

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

This should help:

ag --ignore="*_test.rb" "SomeAwesomeClass"
1
  • 1
    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 4, 2021 at 2: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.

1
  • I found this answer combined with the answer from @ArturMałecki covered why --ignore didn't work for me.
    – ken hicks
    Jan 12, 2021 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.

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

1
  • --path-to-ignore ~/.ignore did not work for me. --ignore=~/.ignore does.
    – cevaris
    Aug 25, 2021 at 15:29
1

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

ag --ignore TAGS;*.pdf;*.json "search_term"
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

0

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

1
  • 1
    I can't reproduce getting globs to work with this. Could you post the actual line that worked for you? Jul 23, 2015 at 4:27
-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'
1
  • 5
    why do your single quotes look like backticks? Jul 23, 2015 at 4:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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