How to search for file extension?
dakom opened this issue · 4 comments
I'm new to Vim, sorry if this isn't the best place to ask!
I am trying to use this plugin to search in files by extension.
For example, to search for import "foo/Foo.css"
in a web app, this works:
:Ack -i css src/app
But this does not:
:Ack -i css **/*.tsx
Any tips?
On a related note, here's a followup question https://vi.stackexchange.com/questions/16002/how-to-keep-results-of-grep-open-while-saving
Note that ack.vim is based on the command-line program vim.
ack typically doesn't go off of file globs, but off of filetypes. If you want to just search PHP files, you'd use :Ack -i whatever --php
, and it would search all the PHP files. Note that filetype can be a more complex issue that just a single file glob.
From the command line, run ack --help-types
and it will list all the filetypes that ack knows about. Note that for PHP it says:
.php .phpt .php3 .php4 .php5 .phtml; First line matches /^#!.*\bphp/
I'm guessing that your .tsx files are for the JSX syntax, based on https://www.typescriptlang.org/docs/handbook/jsx.html
So what you can do is define your own filetype of JSX files like so
ack --type-add=jsx:ext:tsx --jsx -i css
Now, that --type-add
is a pain to put on every line, so you can put it in your .ackrc file. You can use ~/.ackrc
in your own home directory, which will make the type --jsx
available any time you use ack, or you can make a .ackrc
file in the root of the project you're working in, which will make --jsx
available only when you work on that project.
Once you have the --type-add
line in an .ackrc
file, you can just do this:
ack --jsx -i css
or from vim (I assume)
:Ack --jsx -i css
Does that all make sense?
Thanks! I'm using ag
so instead of --help-types
it's --list-file-types
and it turns out that typescript is builtin (--ts
which handles .ts
and .tsx
)
Much appreciated!
Excellent. I may have to add Typescript to ack3. Thanks.