Suppose, we have the following files: ~/1.js, ~/1.min.js, ~/bad-dir/2.js.

All of them have the following contents:

function hello () {}

I am using helm-do-grep-1 to grep through these files and want exclude certain patterns in file names and directories from search:

(setq helm-grep-ignored-files (list "*.min.js" "/bad-dir/"))

But unfortunately when I run (helm-do-grep-1 (list "/home/user") t nil '("*.js")) and then type "hello", it outputs this:

1.js:1:function hello () {}
1.min.js:1:function hello () {}
2.js:1:function hello () {}

How to setup the exclusions?

1 Answer 1


I am afraid that is not possible at the moment, but you can modify helm-grep--prepare-cmd-line yourself and have it work. A simple way it to change helm-grep.el#L510-514 from

         (exclude           (unless (helm-grep-use-ack-p)
                              (if helm-grep-in-recurse
                                  (concat (or include ignored-files)
                                          " " ignored-dirs)


         (exclude           (unless (helm-grep-use-ack-p)
                              (if helm-grep-in-recurse
                                  (concat include " " ignored-files " " ignored-dirs)
                                (concat include " " ignored-files))))

By the way, to ignore bad-dir, you need to use helm-grep-ignored-directories instead of helm-grep-ignored-files.

Here is what you can add to your init file to achieve this:

(with-eval-after-load 'helm-grep
  (defun my-helm-grep--prepare-cmd-line (only-files &optional include zgrep)
    (let* ((default-directory (or helm-ff-default-directory
           (fnargs            (helm-grep-prepare-candidates
                               only-files default-directory))
           (ignored-files     (unless (helm-grep-use-ack-p)
                                 (lambda (x)
                                   (concat "--exclude="
                                           (shell-quote-argument x)))
                                 helm-grep-ignored-files " ")))
           (ignored-dirs      (unless (helm-grep-use-ack-p)
                                 ;; Need grep version >=2.5.4
                                 ;; of Gnuwin32 on windoze.
                                 (lambda (x)
                                   (concat "--exclude-dir="
                                           (shell-quote-argument x)))
                                 helm-grep-ignored-directories " ")))
           (exclude           (unless (helm-grep-use-ack-p)
                                (if helm-grep-in-recurse
                                    (concat include " " ignored-files " " ignored-dirs)
                                  (concat include " " ignored-files))))
           (types             (and (helm-grep-use-ack-p)
                                   ;; When %e format spec is not specified
                                   ;; in `helm-grep-default-command'
                                   ;; we need to pass an empty string
                                   ;; to types to avoid error.
                                   (or include "")))
           (smartcase         (if (helm-grep-use-ack-p)
                                (unless (let ((case-fold-search nil))
                                           "[[:upper:]]" helm-pattern))
            (concat helm-grep-default-command " %m")) ; `%m' like multi.
           (patterns (helm-mm-split-pattern helm-pattern t))
           (pipe-switches (mapconcat 'identity helm-grep-pipe-cmd-switches " "))
            (helm-aif (cdr patterns)
                (cl-loop with pipcom = (helm-grep--pipe-command-for-grep-command
                                        smartcase pipe-switches)
                         for p in it concat
                         (format " | %s %s" pipcom (shell-quote-argument p)))
       (delq nil
             (list (unless zgrep
                     (if types
                         (cons ?e types)
                       (cons ?e exclude)))
                   (cons ?c (or smartcase ""))
                   (cons ?p (shell-quote-argument (car patterns)))
                   (cons ?f fnargs)
                   (cons ?m pipes))))))
  (advice-add 'helm-grep--prepare-cmd-line :override #'my-helm-grep--prepare-cmd-line))
  • Can you show how I can modify grep command template to achieve the goal. I can changed the file. But I'll need to do it after every upgrade of Emacs. And have 3 Emacses: 2 at work and 1 at home.
    – user4035
    Feb 16, 2022 at 10:36
  • I would say that modifying the grep command template would be a rather hackish and not very extensible way to achieve this. I edited the answer with a snippet that overrides helm-grep--prepare-cmd-line with a version that supports both inclusions and exclusions.
    – d125q
    Feb 16, 2022 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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