Notmuch Cheatsheet

I've used notmuch with the mutt email client for years, but mostly as a tool to search my email. I organize my email by folders or directories, and archive based on year (e.g., year2020, etc). In the last few years, though, I've begun to accumulate email folders that I used to manage various projects, and recently, I've felt like these folders are muddying my system up. To fix this, I finally started to explore notmuch tags, and now I'm a fan. I assembled this cheat sheet to help me remember some of the more common commands.

Notmuch commands

We generally run these commands on the command line.

Purpose Command What it does When to use it
index new mail notmuch new scans maildir and updates database after syncing or moving mail
search messages notmuch search <query> lists matching threads / queries quick cli search
count matches notmuch count <query> returns number of matching messages checking tag counts
show full message notmuch show <query> displays complete thread content inspect mail outside mutt
lists tags in use notmuch search --output=tags '*' lists all tags currently in DB audit my tagging
add a tag notmuch tag +foo -- <query> adds tag foo to matching email bulk labeling
remove a tag notmuch tag -foo -- <query> removes tag foo to matching email cleanup
replace a tag notmuch tag +newtag -oldtag -- <query> swaps tag states relabel emails
search by folder notmuch search path:folder/** query by maildir path when using folders and tags
output filenames notmuch search --output=files <query> list raw file paths for scripting or moving emails
dump DB notmuch dump > backup.txt export all tags before major changes
restore DB notmuch restore < backup.txt restores tags recovery / migration
compact DB notmuch compact restores tags recovery / migration
rebuild from scratch (XDG) rm -r ~/.local/share/notmuch/ && notmuch new full reindex in case of DB corruption
rebuild from scratch (ex. older PATH) rm -r ~/Mail/.notmuch/ && notmuch new full reindex in case of DB corruption

I don't run all the commands above on the command line. For example, I update the Notmuch DB within Mutt when I sync my email. Here's an example from my .muttrc file:


# RETRIEVING EMAIL
macro index,pager  "clear ; /usr/bin/mbsync -a ; notmuch new" "Retrieve new email"
	

Useful search patterns

I generally run searches like those listed in the table below from within the Mutt email client. Search is bound to the F8 key. Here's the macro for that, which I likely got from the Notmuch website a long time ago:


# SEARCH EMAIL
macro index  \
"set my_old_pipe_decode=\$pipe_decode my_old_wait_key=\$wait_key nopipe_decode nowait_key\
notmuch-mutt -r --prompt search\
`echo ${XDG_CACHE_HOME:-$HOME/.cache}/notmuch/mutt/results`\
set pipe_decode=\$my_old_pipe_decode wait_key=\$my_old_wait_key" \
"notmuch: search mail"
	
Query Meaning
tag:inbox needs processing
tag:unread not yet read
from:alice mail from alice
subject:"budget review" exact phrase in subject
date:30d..now last 30 days
path:year2019/** mail in that folder
tag:waiting and date:14d..now recent mail w/ labeled as ‘waiting’
from:aol.com domain based senders
thread:<message-id> entire conversation