Labeling mail with mutt

Introduction

A very cool gmail feature is the ability to add label to emails (also called "tags" in other contexts), and then view only mails with a given label.

There is a semi-standard email header called "X-Label" which can be used to store labels. mutt supports searching it, filtering views according to the value, and showing it in the index view; but it doesn't allow you to change it (although there are patches).

Luckily, it has a really useful macro processing, so it's not so difficult to implement.

How to make it work

First, download the editlabel script, which will be necessary. Put it inside some directory like ~/bin and make it executable with chmod +x ~/bin/editlabel.

Then backup your ~/.muttrc file, in case you mess up, and edit to add the following lines somewhere:

# labels
macro index y "<enter-command>set editor=\"~/bin/editlabel append\"\n\
<edit><sync-mailbox><next-undeleted>\
<enter-command>set editor=vim\n" "Append label"
macro pager y "<enter-command>set editor=\"~/bin/editlabel append\"\n\
<edit><sync-mailbox><next-undeleted>\
<enter-command>set editor=vim\n" "Append label"
macro index Y "<enter-command>set editor=\"~/bin/editlabel menu\"\n\
<edit><sync-mailbox><next-undeleted>\
<enter-command>set editor=vim\n" "Edit labels"
macro pager Y "<enter-command>set editor=\"~/bin/editlabel menu\"\n\
<edit><sync-mailbox><next-undeleted>\
<enter-command>set editor=vim\n" "Edit labels"
macro index \Cy "<limit>~y " "Limit view to label"

It assumes that you use vim as the editor; if you don't, replace it with the command of your choice. Also, if you placed the editlabel script somewhere else, change it with the proper location.
Do NOT touch anything else there.

Then, look for a line that begins with set index_format and change it for something like:

set index_format="%4C %Z %{%b %d} %-15.15L %?M?(#%03M)&(%4l)? %?y?(%.20Y) ?%s"

The key is %?y?(%.20Y), it makes mutt display the labels right before the subject. If you're keen, you can modify your existing index_format to add it as you please. If the line doesn't exist, add it.

Finally, edit ~/.labels and add the labels you want, one per line (empty lines are allowed). Something like:

personal
urgent
work
pending
You can add more labels later if you want.

How to use it

Now everything is set up, let's see how you use it.
It might be a good idea to play on an useless mailbox first, just in case.

Adding a label

To add a label to an email, select it in the index or pager, and hit y. It will ask you for a label, type the name (it MUST be on the list you wrote earlier) and press enter. Voila! You should see the label assigned to it. You can add many labels to the same email.

Removing a label

To remove a label from an email, select it in the index or pager, and hit Y (note it's caps). Then, a small menu will appear, type r and press enter. Then type the name of the label you want to remove, and press enter again.

Clearing all labels

If you want to clear all labels from an email, select it in the index or pager, and hit Y (note it's caps). Then, a small menu will appear, type c and press enter.

Filtering views

If you only want to see mails with a given label, press Ctrl+y, which will make a prompt like Limit to messages matching: ~y appear. Type the name of the label (make sure you don't remove the "~y " at the beginning) and press enter.
To restore the view to all emails, press l. and then enter.

Other actions

Other actions are available through the same menu you've already seen: select the email, hit Y and then if you select s it will show you the actual labels; with l you will see the available labels (ie. the ones in ~/.labels. The a action is for adding a label, just like the first one we saw.

Steve Kemp has implemented a nice label indexer that scans all your mailboxes and creates a folder per tag, so you can easily view all mails in all your folders with a given label.


Alberto Bertogli (albertito@blitiri.com.ar) - Last updated 10/Nov/2012