The Mail application in macOS allows creation of rules, such as Sender X and keyword Y in subject -> mark as read and move to archive. I am looking for something similar in mu4e.

Smart refiling has a similar feature:

you can select some messages in the headers view, then press r, and have them all marked for refiling to their particular folders.

What I'm looking for is that result without having to see the message or press r to refile it.

Does it exist?

  • See smart refiling.
    – jagrg
    Feb 19, 2020 at 0:33
  • @jagrg Smart refiling still requires user input, I'd like the messages to be refiled without even seeing them. I updated the question.
    – emonigma
    Feb 19, 2020 at 10:33

1 Answer 1


I don't think so. In any case, the function that does the refiling is called mu4e~proc-move, so all you need is the message's docid and target folder. But how do you know which messages are new during the indexing process? One way around this is to find messages flagged unread using the mu find command (see below).

(defvar mu4e-find-new-messages-command
  "mu find maildir:/YOUR_INBOX_FOLDER flag:unread --format=sexp 2>/dev/null")

(defun mu4e-refile-messages ()
  (let* ((cmd mu4e-find-new-messages-command)
         (res (concat "(list" (shell-command-to-string cmd) ")"))
         (msgs (car (read-from-string res))))
    (unless (equal '(list) msgs)
      (dolist (msg msgs)
        (when-let ((docid (mu4e-message-field msg :docid))
                   (maildir (funcall mu4e-refile-folder msg)))
          (mu4e~proc-move docid maildir))))))

(add-to-list 'mu4e-index-updated-hook 'mu4e-refile-messages)
  • Is the logic that I would create smart refiling rules as in the manual, and your function and hook applies them each time the mu4e index is updated?
    – emonigma
    Feb 20, 2020 at 14:19
  • 1
    That's the logic, yes.
    – jagrg
    Feb 20, 2020 at 16:28
  • I finally got around to try this, sorry for the delay. I can't get it to work. I have a rule with a single condition on mu4e-message-contact-field-matches msg :from "...", which I checked in the source code to be valid. This should refile after I run M-x mu4e and U, right?
    – emonigma
    Dec 9, 2021 at 20:16

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.