0

If I understand correctly (from Defining your own types in ack --man), when I use --type-set in .ackrc (or at the command line) I can only use one "filtertype". In my case, I'd like to match files with a particular extension that also have a pattern present in the first line. I imagined it would look like this (perhaps without the --type-set in the middle:

--type-set
mytype:firstlinematch:my.regex.pattern...
--type-set
mytype:ext:myextension

If I do that, though, the linematch is ignored, because the type is replaced by the 2nd entry.*

I know multiple entries are allowed for --type-add, but that means this OR that condition must be true, rather than restricting to when both are true.

Is there a way to narrow using two filter types for a case like this?

  • (If I'm right about that, then a warning would help when a definition is clobbered later in .ackrc.)
Joshua Goldberg
  • 5,059
  • 2
  • 34
  • 39

1 Answers1

0

Using --type-add as the second definition is not restrictive. If one defines foo file types as follows:

ack --type-set foo:.txt --foo bar

This finds all txt files with bar somewhere in the file. If one adds to this file type set a first line match:

ack --type-set foo:.txt --type-add foo:firstlinematch:/pr.*t/ --foo bar

This redefines the file type as all txt files AND any file containing the regex /pr.*t/ in its first line. Accordingly, it finds all bars in that set of files. In other words, type-add is adds to the definition of files. The man file explicitly says:

"Type specifications can be repeated and are ORed together."

gregory
  • 10,969
  • 2
  • 30
  • 42