Tuesday, February 21, 2012

Using notmuch to search emails from within Mutt

I am not a fan of GUI apps for all occasions. I general prefer to work in a text environment and use the keyboard rather than always mousing around and clicking to get things done. So when  my buddy, Will, showed me how to use notmuch with Mutt to search through my email I was ecstatic. Prior to this I would use command line tools to find emails and then navigate to folders to read emails. With the following setup I can now do my searching from within Mutt and quickly get to what I want.

The following instructions are how to set things up on Fedora 16. For other platforms please be sure to adjust what needs to be downloaded and/or installed from package repositories to suit your needs.

What You Need To Install

I'm going to to assume that you already have Mutt installed. In addition, you'll need to install the following packages and all of their dependencies:

  • perl-Mail-Box
  • perl-Email-Sender
  • perl-MailTools

The Script That Does The Work

You can download the notmuch-mutt script here. On my system I stored it at ~/Documents/notmuch-mutt.pl and refer to it as such in my .muttrc below.

Mutt Configuration

The big hook is to add the following to your .muttrc file. Here is what I added to mine:

macro index <F8> \
      "<enter-command>unset wait_key<enter><shell-escape>~/Documents/mutt-notmuch.pl --prompt search<enter><change-folder-readonly>~/.cache/mutt_results<enter>" \
      "search mail (using notmuch)"
macro index <F9> \
      "<enter-command>unset wait_key<enter><pipe-message>~/Documents/mutt-notmuch.pl thread<enter><change-folder-readonly>~/.cache/mutt_results<enter><enter-command>set wait_key<enter>" \
      "search and reconstruct owning thread (using notmuch)"

With this code you can use F8 to search for messages and F9 to reconstruct the thread for one of the  search result messages.

That's it! Now with this setup you can stay in a single terminal to search.