Notmuch logo

Tips and Tricks for using notmuch with Emacs

One of the more popular notmuch message reading clients is notmuch.el, an emacs major mode for interacting with notmuch. It is included in the notmuch package (notmuch-emacs in Debian). This page goes over some usage tips for using notmuch with Emacs.

  1. Tips and Tricks for using notmuch with Emacs
    1. Setup
    2. Navigating & reading mails
    3. Sending mail
    4. Attaching files
    5. Controlling external handlers for attachements
    6. Issues with Emacs 24
  2. Advanced tips and tweaks
    1. Overwriting the sender address
    2. Initial cursor position in notmuch 0.15 hello window
    3. Add a key binding to add/remove/toggle a tag
    4. Adding many tagging keybindings
    5. Restore reply-to-all key binding to 'r'
    6. How to do FCC/BCC...
    7. How to customize notmuch-saved-searches
    8. Viewing HTML messages with an external viewer
    9. msmtp, message mode and multiple accounts
    10. Address completion when composing
    11. How to sign/encrypt messages with gpg
    12. Reading and verifying encrypted and signed messages
    13. Multiple identities using gnus-alias
    14. Resending (or bouncing) messages
    15. notmuch-hello refresh status message
    16. Replacing tabs with spaces in subject and header
    17. Hiding unread messages in notmuch-show
    18. Changing the color of a saved search based on some other search
    19. Linking to notmuch messages and threads from the Circe IRC client
    20. Linking to notmuch messages from org-mode
    21. Viewing diffs in notmuch

Setup

Have a look at the Howto for prerequisites. Be sure you have done the general setup using the notmuch cli command!

To use the Notmuch emacs mode, first add the following line to your .emacs rc file:

    (autoload 'notmuch "notmuch" "notmuch mail" t)

or if you always want to load notmuch when you start emacs:

    (require 'notmuch)

Then, either run "emacs -f notmuch", or execute the command M-x notmuch from within a running emacs.

Notmuch Emacs configuration file:

(Since Notmuch 0.18)

After notmuch is loaded notmuch-init-file (typically ~/.emacs.d/notmuch-config.el) is checked out. If such file exists it is loaded. Most emacs lisp based configuration not suitable via customization can be put there instead of ~/.emacs.

Navigating & reading mails

When first starting notmuch in emacs, you will be presented with the notmuch "hello" page. If it exits with an error after writing "Welcome to notmutch. You have" you need to do the basic notmuch setup first (see above). From here you can do searches, see lists of recent searches, saved searches, message tags, help information, etc.

Executing a search will open a new buffer in notmuch-search-mode displaying the search results. Each line in the search results represents a message thread. Hitting the '?' key will show help for this mode.

In general, the 'q' will kill the current notmuch buffer and return you to the previous buffer (sort of like a 'pop').

In search mode, navigating to a thread and hitting return will then open a new buffer in notmuch-show-mode, which will show the actual message contents of the thread.

Sending mail

In any notmuch mode, you can start a new message by hitting the 'm' key. To reply to a message or thread, just hit the 'r' key.

When composing new messages, you will be entered in emacs's message-mode, which is a powerful mode for composing and sending messages. When in message mode, you can type C-c ? for help.

If you would like to use address autocompletion when composing messages, see address completion.

When you are ready to send a message, type C-c C-c. By default message mode will use your sendmail command to send mail, so make sure that works. One annoying standard configuration of message mode is that it will hide the sent mail in your emacs frame stack, but it will not close it. If you type several mails in an emacs session they will accumulate and make switching between buffers more annoying. You can avoid that behavior by adding (setq message-kill-buffer-on-exit t) in your .emacs file (or doing M-x customize-variable<RET>message-kill-buffer-on-exit<RET>) which will really close the mail window after sending it.

Attaching files

Using the M-x mml-attach-file command, you can attach any file to be sent with your mail. By default this command is bound to the menu item Attachments--Attach File with the key binding C-c C-a. The variable mml-dnd-attach-options (M-x customize-variable<RET>mml-dnd-attach-options<RET>) can be set to allow the prompting for various attachment options (such as inline/attachment) if you want to do that.

For those who prefer a more graphical interface, you can also simply drag and drop files from a file manager into a mail composition window to have them attached. In Ubuntu this works without any modifications if files are dragged from the file manager.

And for those who prefer working from command line, the following script opens new emacs window with empty message and attaches files mentioned as script arguments. (Note: The script expects that you have (server-start) in your .emacs file.)

    #!/bin/sh
    attach_cmds=""
    while [ $# -gt 0 ]; do
        fullpath=$(readlink --canonicalize "$1")
        attach_cmds="$attach_cmds (mml-attach-file \"$fullpath\")"
        shift
    done
    emacsclient -a '' -c -e "(progn (compose-mail) $attach_cmds)"

Controlling external handlers for attachements

You can choose e.g. which pdf viewer to invoke from notmuch-show mode by adding a .mailcap file in your home directory. Here is an example:

application/pdf; /usr/bin/mupdf %s; test=test "$DISPLAY" != ""; description=Portable Document Format; nametemplate=%s.pdf
application/x-pdf; /usr/bin/mupdf %s; test=test "$DISPLAY" != ""; description=Portable Document Format; nametemplate=%s.pdf

Issues with Emacs 24

If notmuch-show-mode behaves badly for you in emacs 24.x try adding one of

    (setq gnus-inhibit-images nil)

or

    (require 'gnus-art)

to your .emacs file.


Advanced tips and tweaks

Overwriting the sender address

If you want to always use the same sender address, then the following defadvice can help you.

   (defadvice notmuch-mua-reply (around notmuch-fix-sender)
     (let ((sender "Max Monster <max.monster@example.com>"))
       ad-do-it))
   (ad-activate 'notmuch-mua-reply)

Initial cursor position in notmuch 0.15 hello window

In notmuch version 0.15 emacs client the handling of cursor position in notmuch hello window has been simplified to a version which suits best most cases.

Initially the cursor is positioned at the beginning of buffer.

Some users liked the "ancient" version where cursor was moved to the first Saved searches button.

Add the following code to your notmuch emacs configuration file in case you want this behaviour:

    (add-hook 'notmuch-hello-refresh-hook
              (lambda ()
                (if (and (eq (point) (point-min))
                         (search-forward "Saved searches:" nil t))
                    (progn
                      (forward-line)
                      (widget-forward 1))
                  (if (eq (widget-type (widget-at)) 'editable-field)
                      (beginning-of-line)))))

Add a key binding to add/remove/toggle a tag

The notmuch-{search,show,tree}-tag functions are very useful for making quick tag key bindings. The arguments to these functions have changed as notmuch has evolved but the following should work on all versions of notmuch from 0.13 on. These functions take a list of tag changes as argument. For example, an argument of (list "+spam" "-inbox") adds the tag spam and deletes the tag inbox. Note the argument must be a list even if there is only a single tag change e.g., use (list "+deleted") to add the deleted tag.

For instance, here's an example of how to make a key binding to add the "spam" tag and remove the "inbox" tag in notmuch-show-mode:

    (define-key notmuch-show-mode-map "S"
      (lambda ()
        "mark message as spam"
        (interactive)
        (notmuch-show-tag (list "+spam" "-inbox"))))

You can do the same for threads in notmuch-search-mode by just replacing "show" with "search" in the keymap and called functions, or for messages in notmuch-tree-mode by replacing "show" by "tree". If you want to tag a whole thread in notmuch-tree-mode use notmuch-tree-tag-thread instead of notmuch-tree-tag.

You may also want the function in search mode apply to the all threads in the selected region (if there is one). For notmuch prior to 0.17 this behaviour will occur automatically with the functions given above. To get this behaviour on 0.17+ do the following:

    (define-key notmuch-search-mode-map "S"
      (lambda (&optional beg end)
        "mark thread as spam"
        (interactive (notmuch-search-interactive-region))
        (notmuch-search-tag (list "+spam" "-inbox") beg end)))

The analogous functionality in notmuch-tree is currently missing.

The definitions above make use of a lambda function, but you could also define a separate function first:

    (defun notmuch-show-tag-spam ()
      "mark message as spam"
      (interactive)
      (notmuch-show-add-tag (list "+spam" "-inbox")))

    (define-key notmuch-show-mode-map "S" 'notmuch-show-tag-spam)

Here's a more complicated example of how to add a toggle "deleted" key:

    (define-key notmuch-show-mode-map "d"
      (lambda ()
        "toggle deleted tag for message"
        (interactive)
        (if (member "deleted" (notmuch-show-get-tags))
            (notmuch-show-tag (list "-deleted"))
          (notmuch-show-tag (list "+deleted")))))

Adding many tagging keybindings

If you want to have have many tagging keybindings, you can save the typing the few lines of boilerplate for every binding (for versions before 0.12, you will need to change notmuch-show-apply-tag-macro).

(eval-after-load 'notmuch-show
  '(define-key notmuch-show-mode-map "`" 'notmuch-show-apply-tag-macro))

(setq notmuch-show-tag-macro-alist
  (list
   '("m" "+notmuch::patch" "+notmuch::moreinfo" "-notmuch::needs-review")
   '("n" "+notmuch::patch" "+notmuch::needs-review" "-notmuch::pushed")
   '("o" "+notmuch::patch" "+notmuch::obsolete"
         "-notmuch::needs-review" "-notmuch::moreinfo")
   '("p" "-notmuch::pushed" "-notmuch::needs-review"
     "-notmuch::moreinfo" "+pending")
   '("P" "-pending" "-notmuch::needs-review" "-notmuch::moreinfo" "+notmuch::pushed")
   '("r" "-notmuch::patch" "+notmuch::review")
   '("s" "+notmuch::patch" "-notmuch::obsolete" "-notmuch::needs-review" "-notmuch::moreinfo" "+notmuch::stale")
   '("t" "+notmuch::patch" "-notmuch::needs-review" "+notmuch::trivial")
   '("w" "+notmuch::patch" "+notmuch::wip" "-notmuch::needs-review")))

(defun notmuch-show-apply-tag-macro (key)
  (interactive "k")
  (let ((macro (assoc key notmuch-show-tag-macro-alist)))
    (apply 'notmuch-show-tag-message (cdr macro))))

Restore reply-to-all key binding to 'r'

Starting from notmuch 0.12 the 'r' key is bound to reply-to-sender instead of reply-to-all. Here's how to swap the reply to sender/all bindings in show mode:

    (define-key notmuch-show-mode-map "r" 'notmuch-show-reply)
    (define-key notmuch-show-mode-map "R" 'notmuch-show-reply-sender)

And in search mode:

    (define-key notmuch-search-mode-map "r" 'notmuch-search-reply-to-thread)
    (define-key notmuch-search-mode-map "R" 'notmuch-search-reply-to-thread-sender)

How to do FCC/BCC...

The Emacs interface to notmuch will automatically add an Fcc header to your outgoing mail so that any messages you send will also be saved in your mail store. You can control where this copy of the message is saved by setting the variable notmuch-fcc-dirs which defines the subdirectory relative to the database.path setting from your notmuch configuration in which to save the mail. Enter a directory (without the maildir /cur ending which will be appended automatically). Additional information can be found as usual using:

   M-x describe-variable notmuch-fcc-dirs

An additional variable that can affect FCC settings in some cases is message-directory. Emacs message-mode uses this variable for postponed messages.

To customize both variables at the same time, use the fancy command:

    M-x customize-apropos<RET>\(notmuch-fcc-dirs\)\|\(message-directory\)

This mechanism also allows you to select different folders to be used for the outgoing mail depending on your selected From address. Please see the documentation for the variable notmuch-fcc-dirs in the customization window for how to arrange this.

How to customize notmuch-saved-searches

When starting notmuch, a list of saved searches and message counts is displayed, replacing the older notmuch-folders command. The set of saved searches displayed can be modified directly from the notmuch interface (using the [save] button next to a previous search) or by customising the variable notmuch-saved-searches.

An example setting for notmuch versions up to 0.17.x might be:

    (setq notmuch-saved-searches '(("inbox" . "tag:inbox")
                    ("unread" . "tag:inbox AND tag:unread")
                    ("notmuch" . "tag:inbox AND to:notmuchmail.org")))

Starting from notmuch 0.18 the variable changed. It is backwards compatible so the above will still work but the new style will be used if you use customize and there are some new features available. The above would become

    (setq notmuch-saved-searches '((:name "inbox" :query "tag:inbox")
                    (:name "unread" :query "tag:inbox AND tag:unread")
                    (:name "notmuch" :query "tag:inbox AND to:notmuchmail.org")))

The additional features are the possibility to set the search order for the search, and the possibility to specify a different query for displaying the count for the saved-search. For example

    (setq notmuch-saved-searches '((:name "inbox"
                                    :query "tag:inbox"
                                    :count-query "tag:inbox and tag:unread"
                                    :sort-order 'oldest-first)))

specifies a single saved search for inbox, but the number displayed by the search will be the number of unread messages in the inbox, and the sort order for this search will be oldest-first.

Of course, you can have any number of saved searches, each configured with any supported search terms (see "notmuch help search-terms"), and in the new style variable they can each have different count-queries and sort orders.

Some users find it useful to add and not tag:delete to those searches, as they use the delete tag to mark messages as deleted. This causes messages that are marked as deleted to be removed from the commonly used views of messages. Use whatever seems most useful to you.

Viewing HTML messages with an external viewer

The Emacs client can generally display HTML messages inline using one of the supported HTML renderers. This is controlled by the mm-text-html-renderer variable.

Sometimes it may be necessary to display the message, or a single MIME part, in an external browser. This can be done by (notmuch-show-view-part), bound to . v by default.

msmtp, message mode and multiple accounts

As an alternative to running a mail server such as sendmail or postfix just to send email, it is possible to use msmtp. This small application will look like /usr/bin/sendmail to a MUA such as emacs message mode, but will just forward the email to an external SMTP server. It's fairly easy to set up and it supports several accounts for using different SMTP servers. The msmtp pages have several examples.

A typical scenario is that you want to use the company SMTP server for email coming from your company email address, and your personal server for personal email. If msmtp is passed the envelope address on the command line (the -f/--from option) it will automatically pick the matching account. The only trick here seems to be getting emacs to actually pass the envelope from. There are a number of overlapping configuration variables that control this, and it's a little confusion, but setting these three works for me:

With that in place, you need a .msmtprc with the accounts configured for the domains you want to send out using specific SMTP servers and the rest will go to the default account.

Address completion when composing

There are currently three solutions to this:

bbdb

bbdb is a contact database for emacs that works quite nicely together with message mode, including address autocompletion.

notmuch database as an address book

You can also use the notmuch database as a mail address book itself. To do this you need a command line tool that outputs likely address candidates based on a search string. There are currently four available:

You can perform tab-completion using any of these programs. Just add the following to your notmuch init file:

    (require 'notmuch-address)
    (setq notmuch-address-command "/path/to/address_fetching_program")
    (notmuch-address-message-insinuate)

Google Contacts

GooBook is a command-line tool for accessing Google Contacts. Install and set it up according to its documentation.

To use GooBook with notmuch, use this wrapper script and set it up like the programs above.

    #!/bin/sh
    goobook query "$*" | sed 's/\(.*\)\t\(.*\)\t.*/\2 \<\1\>/' | sed '/^$/d'

You can add the sender of a message to Google Contacts by piping the message (notmuch-show-pipe-message) to goobook add.

Akonadi

    git clone https://github.com/mmehnert/akonadimailsearch

Install the development packages for kdepim on your system. Enter the cloned repository and create a build directory:

    mkdir build
    cd build
    cmake ..; make;

You will find the akonadimailsearch binary in the build/src directory. Copy it to ~/bin .

You can now add the following settings to your notmuch init file:

    (require 'notmuch-address)
    (setq notmuch-address-command "~/bin/akonadimailsearch")
    (notmuch-address-message-insinuate)

Completion selection with helm

An address query might return multiple possible matches from which you will have to select one. To ease this task, several different frameworks in emacs support completion selection. One of them is helm. The following snippet improves the out-of-the-box support for helm in notmuch as it enables the required-match option and also does not ignore the first returned address.

    (setq notmuch-address-selection-function
      (lambda (prompt collection initial-input)
        (completing-read prompt (cons initial-input collection) nil t nil 'notmuch-address-history)))

How to sign/encrypt messages with gpg

Messages can by signed using gpg by invoking M-x mml-secure-sign-pgpmime (or M-x mml-secure-encrypt-pgpmime). These functions are available via the standard message-mode keybindings C-c C-m s p and C-c C-m c p. To sign outgoing mail by default, use the message-setup-hook in your .emacs file:

    ;; Sign messages by default.
    (add-hook 'message-setup-hook 'mml-secure-sign-pgpmime)

This inserts the required <#part sign=pgpmime> into the beginning of the mail text body and will be converted into a pgp signature when sending (so one can just manually delete that line if signing is not required).

Alternatively, you may prefer to use mml-secure-message-sign-pgpmime instead of mml-secure-sign-pgpmime to sign the whole message instead of just one part.

Troubleshooting message-mode gpg support

Reading and verifying encrypted and signed messages

Encrypted and signed mime messages can be read and verified with:

    (notmuch-crypto-process-mime t)

Decrypting or verifying inline pgp messages can be done by selecting an the inline pgp area and and using:

    M-x epa-decrypt-region RET

Multiple identities using gnus-alias

gnus-alias allows you to define multiple identities when using message-mode. You can specify the from address, organization, extra headers (including Bcc), extra body text, and signature for each identity. Identities are chosen based on a set of rules. When you are in message mode, you can switch identities using gnus-alias.

Installation

Looking into gnus-alias.el gives a bit more information...

Example Configuration

Here is an example configuration.

    ;; Define two identities, "home" and "work"
    (setq gnus-alias-identity-alist
          '(("home"
             nil ;; Does not refer to any other identity
             "John Doe <jdoe@example.net>" ;; Sender address
             nil ;; No organization header
             nil ;; No extra headers
             nil ;; No extra body text
             "~/.signature")
            ("work"
             nil
             "John Doe <john.doe@example.com>"
             "Example Corp."
             (("Bcc" . "john.doe@example.com"))
             nil
             "~/.signature.work")))
    ;; Use "home" identity by default
    (setq gnus-alias-default-identity "home")
    ;; Define rules to match work identity
    (setq gnus-alias-identity-rules)
          '(("work" ("any" "john.doe@\\(example\\.com\\|help\\.example.com\\)" both) "work"))
    ;; Determine identity when message-mode loads
    (add-hook 'message-setup-hook 'gnus-alias-determine-identity)

When gnus-alias has been loaded (using autoload, require, M-x load-library or M-x load-file (load-file takes file path -- therefore it can be used without any .emacs changes)) the following commands can be used to get(/set) more information (some of these have "extensive documentation"):

    M-x describe-variable RET gnus-alias-identity-alist
    M-x describe-variable RET gnus-alias-identity-rules
    M-x describe-variable RET gnus-alias-default-identity

    M-x customize-group RET gnus-alias RET
      or
    M-x gnus-alias-customize RET

The last two do the same thing.

See also the Usage: section in gnus-alias.el.

Resending (or bouncing) messages

Add the following to your notmuch init file to be able to resend the current message in show mode.

    (define-key notmuch-show-mode-map "b"
      (lambda (&optional address)
        "Bounce the current message."
        (interactive "sBounce To: ")
        (notmuch-show-view-raw-message)
        (message-resend address)))

notmuch-hello refresh status message

Add the following to your notmuch init file to get a status message about the change in the number of messages in the mail store when refreshing the notmuch-hello buffer.

    (defvar notmuch-hello-refresh-count 0)

    (defun notmuch-hello-refresh-status-message ()
      (unless no-display
        (let* ((new-count
                (string-to-number
                 (car (process-lines notmuch-command "count"))))
               (diff-count (- new-count notmuch-hello-refresh-count)))
          (cond
           ((= notmuch-hello-refresh-count 0)
            (message "You have %s messages."
                     (notmuch-hello-nice-number new-count)))
           ((> diff-count 0)
            (message "You have %s more messages since last refresh."
                     (notmuch-hello-nice-number diff-count)))
           ((< diff-count 0)
            (message "You have %s fewer messages since last refresh."
                     (notmuch-hello-nice-number (- diff-count)))))
          (setq notmuch-hello-refresh-count new-count))))

    (add-hook 'notmuch-hello-refresh-hook 'notmuch-hello-refresh-status-message)

Replacing tabs with spaces in subject and header

Mailman mailing list software rewrites and rewraps long message subjects in a way that causes TABs to appear in the middle of the subject and header lines. Add this to your notmuch init file to replace tabs with spaces in subject lines:

    (defun notmuch-show-subject-tabs-to-spaces ()
      "Replace tabs with spaces in subject line."
      (goto-char (point-min))
      (when (re-search-forward "^Subject:" nil t)
        (while (re-search-forward "\t" (line-end-position) t)
          (replace-match " " nil nil))))

    (add-hook 'notmuch-show-markup-headers-hook 'notmuch-show-subject-tabs-to-spaces)

And in header lines (this will only work with the yet to be released notmuch version 0.15):

    (defun notmuch-show-header-tabs-to-spaces ()
      "Replace tabs with spaces in header line."
      (setq header-line-format
            (notmuch-show-strip-re
             (replace-regexp-in-string "\t" " " (notmuch-show-get-subject)))))

    (add-hook 'notmuch-show-hook 'notmuch-show-header-tabs-to-spaces)

Hiding unread messages in notmuch-show

I like to have an inbox saved search, but only show unread messages when they view a thread. This takes two steps:

  1. Apply this patch from Mark Walters to add the notmuch-show-filter-thread function.
  2. Add the following hook to your emacs configuration:

    (defun expand-only-unread-hook () (interactive)
      (let ((unread nil)
            (open (notmuch-show-get-message-ids-for-open-messages)))
        (notmuch-show-mapc (lambda ()
                             (when (member "unread" (notmuch-show-get-tags))
                               (setq unread t))))
        (when unread
          (let ((notmuch-show-hook (remove 'expand-only-unread-hook notmuch-show-hook)))
            (notmuch-show-filter-thread "tag:unread")))))
    
    
    (add-hook 'notmuch-show-hook 'expand-only-unread-hook)
    

Changing the color of a saved search based on some other search

I like to have a saved search for my inbox, but have it change color when there are thread with unread messages in the inbox. I accomplish this with the following code in my emacs config:

    (defun color-inbox-if-unread () (interactive)
      (save-excursion
        (goto-char (point-min))
        (let ((cnt (car (process-lines "notmuch" "count" "tag:inbox and tag:unread"))))
          (when (> (string-to-number cnt) 0)
            (save-excursion
              (when (search-forward "inbox" (point-max) t)
                (let* ((overlays (overlays-in (match-beginning 0) (match-end 0)))
                       (overlay (car overlays)))
                  (when overlay
                    (overlay-put overlay 'face '((:inherit bold) (:foreground "green")))))))))))
    (add-hook 'notmuch-hello-refresh-hook 'color-inbox-if-unread)

Linking to notmuch messages and threads from the Circe IRC client

Circe is an IRC client for emacs. To have clickable buttons for notmuch messages and threads, add the following to lui-buttons-list (using, e.g. M-x customize-variable)

("\\(?:id\\|mid\\|thread\\):[0-9A-Za-z][0-9A-Za-z.@-]*" 0 notmuch-show 0)

If you have notmuch-pick installed, it works fine for this as well.

Linking to notmuch messages from org-mode

Support for linking to notmuch messages is distributed with org-mode, but as a contrib file, so you might have to work a bit to load it.

In Debian and derivatives,

(add-to-list 'load-path "/usr/share/org-mode/lisp")

Then

(require 'org-notmuch)

In general it is nice to have a key for org-links (not just for notmuch). For example

(define-key global-map "\C-cl" 'org-store-link)

Viewing diffs in notmuch

The following code allows you to view an inline patch in diff-mode directly from notmuch. This means that normal diff-mode commands like refine, next hunk etc all work.

(defun my-notmuch-show-view-as-patch ()
  "View the the current message as a patch."
  (interactive)
  (let* ((id (notmuch-show-get-message-id))
         (subject (concat "Subject: " (notmuch-show-get-subject) "\n"))
         (diff-default-read-only t)
         (buf (get-buffer-create (concat "*notmuch-patch-" id "*")))
         (map (make-sparse-keymap)))
    (define-key map "q" 'notmuch-kill-this-buffer)
    (switch-to-buffer buf)
    (let ((inhibit-read-only t))
      (erase-buffer)
      (insert subject)
      (insert (notmuch-get-bodypart-internal id 1 nil)))
    (set-buffer-modified-p nil)
    (diff-mode)
    (lexical-let ((new-ro-bind (cons 'buffer-read-only map)))
                 (add-to-list 'minor-mode-overriding-map-alist new-ro-bind))
    (goto-char (point-min))))

and then this function needs to bound into the keymap with something like

(define-key 'notmuch-show-mode-map "D" 'my-notmuch-show-view-as-patch)