8

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.

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
1

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

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.

10

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.

  • 1
    How can we specify to search in .js and in .java files only? (that is, 2 different file extensions) – user3341592 Jul 2 '16 at 13:31
  • 2
    I never understood the names helm-ag and helm-do-ag. What do they really mean? How to recall what they do? – user3341592 Jul 2 '16 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.