11

I'd like to restrict find/grep to (say) only javascript files (mainly because that's what i do in "another editor").

Javascript files in my legacy Ruby-on-Rails project have one of these file extensions:-

.js, .js.erb, .coffee, .js.coffee, .js.coffee.erb, .jsx, .js.jsx, .js.jsx.erb (and maybe more)

If i could, i would restrict find to files with these file extensions, and pre-define this list so i don't have to type it each time.

I think i need to write a custom function, if there is a way to specify filetype, but I'm struggling to discover how to do this.. and maybe I've just picked up a weird workflow from my old editor?

EDIT: sorry the question wasn't clear.. i don't want to fix it so that emacs always searches in Javascript files.. when i want to search in Javascript files, i want to search only files with those extensions.. when i want to search in html files i want to only search in files with extensions .html, .html.erb, .html.haml, .haml, etc.

mustISignUp
  • 243
  • 1
  • 7

4 Answers4

11

If you use helm-ag command, you can specify option like -G\.js$ search_pattern, or if you use helm-do-ag, you can use C-u prefix for specifying extension.

This is screencast.

enter image description here

Thanks for using helm-ag.

syohex
  • 456
  • 2
  • 5
1

If using ag

If you are using ag, you can place an .agignore file in the project root with the files you would want to ignore. Here's an example:

# Extension ignores
*.log
*~
*#*#
*.txt
Sean Allred
  • 6,861
  • 16
  • 85
Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
1

You can use helm-ag-use-grep-ignore-list and specify ignore patterns in grep-find-ignored-files and grep-find-ignored-director.

Tu Do
  • 6,772
  • 20
  • 39
1

Although you have in mind a list of file types you want to search notice that the usual argument to find which selects files is -name which takes a globbing pattern. So something like -name *.ns.* should match all the Javascript files in your project.

Helm and it's ag integration probably have something similar.

These commands record history. Use C-p or the up arrow to bring back previous grep-find searches. You can use C-r .ns. to recall the last search you did across Javascript files, then simply edit the search pattern.