Ack — Ignoring multiple directories without repeating the flag
Asked Answered
ack
A

4

39

Is it possible to ignore multiple directories in Ack, without repeating the flag?

e.g. I know the following works (i.e. setting multiple flags):

ack --ignore-dir=install --ignore-dir=php 'teststring'

I was hoping that I could separate directories with commas, like I can do with the extensions as follows:

ack --ignore-file=ext:css,scss,orig 'teststring'

However, the following comma separated ignore flag doesn't work:

ack --ignore-dir=install,php 'textstring'

Is it possible to use some short-hand equivalent, so I don't have to repeatedly type out the --ignore-dir flag?

Above answered 16/12, 2013 at 17:15 Comment(0)
A
56

It's actually similar to how you would specify the include patterns for grep:

ack <term> --ignore-dir={dir_a,dir_b}

However, This format does not work with a single directory. So

ack <term> --ignore-dir={log}

will not work.

Arlenarlena answered 13/2, 2014 at 1:49 Comment(3)
Note that the {} syntax causes a bash-expansion - it's not specific to ack. Try this at your command line: echo this is cool{1,2}Deuced
It's also important to point out that because brace expansion is a bash-specific feature, you won't be able to take advantage of it in your .ackrc file. Instead you will need to list out each --ignore-dir= option individually.Westbrook
I found that spacing between the dir names matters: ack <term> --ignore-dir={dir_a,dir_b} works but ack <term> --ignore-dir={dir_a, dir_b} doesn'tCoverlet
Y
24

Since you're using ack 2, you can put --ignore-dir=install and --ignore-dir=php in a .ackrc file in the root of your project. Then, every ack invocation in that tree will use those flags.

Yount answered 16/1, 2014 at 18:49 Comment(0)
V
18

So to ignore single directory use

ack <term> --ignore-dir=dir_a

and to ignore multiple directories use

ack <term> --ignore-dir={dir_a,dir_b}
Vile answered 16/2, 2015 at 11:35 Comment(1)
note: don't put space between multiple directories and commaSheila
T
0

One approach could be to select those directories to exclude with a regular expression in -G option complemented with the option --invert-file-match. Based in your question, something like the following:

ack -a -G 'install|php' --invert-file-match 'textstring' .
Treharne answered 16/12, 2013 at 22:35 Comment(2)
Interesting, but I don't think this is viable in Ack 2. I get the following message: Option '-a' is not valid in ack 2 This is because we now have -k/--known-types which makes it only select files of known types, rather than any text file (which is the behavior of ack 1.x). You may have options in a .ackrc, or in the ACKRC_OPTIONS environment variable. Try using the --dump flagAbove
@Birei's solution only works on ack 1.x. The -a and -G flags have been removed in ack 2 in favor of the -x flag.Yount

© 2022 - 2024 — McMap. All rights reserved.