I'm using counsel-git-grep to search inside a project for a specific string, which works great. However, I would like to additionally specify a filetype pattern to reduce the result set. I tried queries like string -- '*.filetype' but none of them worked. I know I can customize the counsel-git-grep-cmd-default but this seems to be a very static approach.

So the question is, is there a way to define a filetype pattern inside a query string of counsel-git-grep?

  • FWIW, a slightly less static alternative to customising counsel-git-grep is invoking M-x counsel-git-grep-switch-cmd RET to do it for you (with history). – Basil Dec 2 '17 at 14:19
  • You can also modify the command for each invocation of counsel-git-grep by calling it with a prefix argument, i.e. C-u M-x counsel-git-grep RET. – Basil Dec 2 '17 at 14:30

I had the same requirement and have found this question on the internet search.

Because I didn't want to modify the git command every timeI wrote a small wrapper which adapts the git grep command to the current file extension:

(defun counsel-git-grep-current-mode ()
  "Like `counsel-git-grep', but limit to current file extension."
  (pcase-let ((`(_ . ,cmd) (counsel--git-grep-cmd-and-proj nil))
          (ext (file-name-extension (buffer-file-name))))
    (counsel-git-grep nil nil (format "%s -- '*.%s'" cmd (shell-quote-argument ext)))))

Source: counsel-git-grep-current-mode

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.