How can I use ack to search in files of a specific file type.
For example
ack -f .scss blah
acl -f .rb blah
Obviously the above does not work but how can I do this with ack?
How can I use ack to search in files of a specific file type.
For example
ack -f .scss blah
acl -f .rb blah
Obviously the above does not work but how can I do this with ack?
Use --type for this. To only search python files for example:
ack --type=python searchthis
List all the supported types with
ack --help=types # ack 2.x
ack --help-types # ack 3.x.x
If you want to create a custom type, add something like this to ~/.ackrc
:
--type-add=custom:ext:rb
--type-set
Then you can use --type=custom with ack to just search file with the .rb extension.
find . -name '*.scss' | ack -x 'blah'
-x Read the list of files to search from STDIN.
Short answer:
ack blah --css
Long answer:
You could add filetypes to your search to teach Ack to look only in those files. For example, lets associate css and sass extensions with a "stylesheets" short name:
ack --type-add=stylesheets:ext:css,sass
Now that you've teach Ack that you want to search on filetypes that you wanted to associate with the "stylesheets" short name, you can ask Ack to use that short name with:
ack --type-add=stylesheets:ext:css,sass --type=stylesheets blah
Or with the short way:
ack --type-add=stylesheets:ext:css,sass --stylesheets blah
Even in the short way, that's a lot to type for a search and that's the reason those kind of commands are better used on a configuration file like ~/.ackrc
, but I prefer to use them with an alias in my ~/.bashrc
.
But why does it works by default with --type=css
, or --css
? Because Ack have a default filenames groups that you can dump with ack --dump
.
Using Ack version: 2.24
© 2022 - 2024 — McMap. All rights reserved.