mileszs / ack.vim

Vim plugin for the Perl module / CLI script 'ack'
Other
3.08k stars 396 forks source link

How to search for file extension? #243

Closed dakom closed 6 years ago

dakom commented 6 years ago

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

petdance commented 6 years ago

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?

dakom commented 6 years ago

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!

petdance commented 6 years ago

Excellent. I may have to add Typescript to ack3. Thanks.

petdance commented 6 years ago

@dakom The other good thing about using types instead of extensions is that you can exclude files, too. So you can do ag whatever --nots when you want it to NOT search Typescript. You can stack them, too, so you can say ag whatever --nots --nohtml.