Silver Searcher - How to ignore a file
Asked Answered
D

10

53

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?

Deil answered 10/4, 2014 at 21:26 Comment(0)
D
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.

Deil answered 10/4, 2014 at 22:29 Comment(1)
I found this answer combined with the answer from @ArturMałecki covered why --ignore didn't work for me.Supranational
R
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

Riot answered 20/4, 2015 at 6:29 Comment(10)
Great answer. For more details on .agignore see: github.com/ggreer/the_silver_searcher/wiki/Advanced-UsageSerrulate
.agignore will be deprecated in favor of .ignore: geoff.greer.fm/2016/09/26/ignoreExport
.agignore works with ag version 2.0.0, check source github.com/ggreer/the_silver_searcher/blob/…Pish
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.Leporide
.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.Underling
I just tried using .agignore (with ag 2.2.0) and it didn't work; renaming it to .ignore worked.Melisma
@Melisma I had the opposite behaviour with the same version of ag as you (2.2.0) on OS X. (c.f. github.com/ggreer/the_silver_searcher/issues/1097) Not sure what is going on!Flosser
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.Contingence
.ignore works in the root of the applicationExtremist
Interesting - I tried .agignore and it works for me with version 2.2.0 (unlike anol's experience). I am running Ubunty 22.04.4 which has package: silversearcher-ag 2.2.0+git20200805-1 amd64. [Personally I prefer .agignore being recognised, even with the choice of .ignore]Timeout
L
23

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

Lore answered 17/11, 2016 at 9:13 Comment(0)
M
17

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

This should help:

ag --ignore="*_test.rb" "SomeAwesomeClass"
Maturity answered 27/11, 2017 at 18:53 Comment(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 allLeman
D
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.

Deil answered 10/4, 2014 at 22:29 Comment(1)
I found this answer combined with the answer from @ArturMałecki covered why --ignore didn't work for me.Supranational
D
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.

Decarburize answered 27/7, 2019 at 1:51 Comment(0)
L
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

Lipo answered 8/8, 2019 at 20:59 Comment(1)
--path-to-ignore ~/.ignore did not work for me. --ignore=~/.ignore does.Becnel
V
1

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

ag --ignore TAGS;*.pdf;*.json "search_term"
Vein answered 27/8, 2015 at 10:31 Comment(0)
D
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

Discover answered 15/3, 2021 at 16:40 Comment(0)
S
0

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

Subclimax answered 2/7, 2014 at 17:14 Comment(1)
I can't reproduce getting globs to work with this. Could you post the actual line that worked for you?Contrapose
L
-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'
Lientery answered 10/4, 2014 at 22:22 Comment(1)
why do your single quotes look like backticks?Contrapose

© 2022 - 2024 — McMap. All rights reserved.