Arch manual pages

AFEW(1) afew AFEW(1)

afew - afew Documentation

afew is an initial tagging script for notmuch mail:


Its basic task is to provide automatic tagging each time new mail is registered with notmuch. In a classic setup, you might call it after notmuch new in an offlineimap post sync hook or in the notmuch post-new hook.

It can do basic thing such as adding tags based on email headers or maildir folders, handling killed threads and spam.

fyi: afew plays nicely with alot, a GUI for notmuch mail ;)


The steps to get up and running are:
  • install the afew package
  • create the config files
  • add a notmuch post-new hook that calls afew

The following commands will get you going on Debian/Ubuntu systems:

$ sudo aptitude install notmuch python-notmuch dbacl
$ git clone git://
$ cd afew
$ python install --prefix

Ensure that ~/.local/bin is in your path. One way is to add the following to your ~/.bashrc:

if [ -d ~/.local/bin ]; then

See installation for a more detailed guide.

Create the directories to hold the config files:

$ mkdir -p ~/.config/afew ~/.local/share/afew/categories

Make sure that ~/.notmuch-config reads:


Put a list of filters into ~/.config/afew/config:

# This is the default filter chain

And create a post-new hook for notmuch.

$ mkdir -p path/to/maildir/.notmuch/hooks
$ touch path/to/maildir/.notmuch/hooks/post-new

Then edit the post-new file to contain:

$HOME/.local/bin/afew --tag --new

You can:
  • add extra filters for more custom filtering
  • make use of the move_mode to move your email between folders
  • run afew against all your old mail by running afew --tag --all
  • start extending afew

afew works with python 2.7, 3.1 and 3.2, and requires notmuch and its python bindings. On Debian/Ubuntu systems you can install these by doing:

$ sudo aptitude install notmuch python-notmuch python-dev python-setuptools

And I'd like to suggest to install afew as your unprivileged user.

$ python install --prefix=~/.local
$ mkdir -p ~/.config/afew

If you do, make sure ~/.local/bin is in your path, say by putting the following in your ~/.bashrc:

if [ -d ~/.local/bin ]; then

If you want to do a system wide install you can leave off the --prefix option.

Ultimately afew is a command line tool. You have to specify an action, and whether to act on all messages, or only on new messages. The actions you can choose from are:
run the tag filters. See Initial tagging.
continuously monitor the mailbox for new files
move mail files between maildir folders

Basic tagging stuff requires no configuration, just run

$ afew --tag --new
# or to tag *all* messages
$ afew --tag --all

To do this automatically you can add the following hook into your ~/.offlineimaprc:

postsynchook = ionice -c 3 chrt --idle 0 /bin/sh -c "notmuch new && afew --tag --new"

There is a lot more to say about general filter configuration and the different filters provided by afew.

Adding --dry-run to any --tag or --sync-tags action prevents modification of the notmuch db. Add some -vv goodness to see some action.

To invoke afew in move mode, provide the --move-mails option on the command line. Move mode will respect --dry-run, so throw in --verbose and watch what effects a real run would have.

In move mode, afew will check all mails (or only recent ones) in the configured maildir folders, deciding whether they should be moved to another folder.

The decision is based on rules defined in your config file. A rule is bound to a source folder and specifies a target folder into which a mail will be moved that is matched by an associated query.

This way you will be able to transfer your sorting principles roughly to the classic folder based maildir structure understood by your traditional mail server. Tag your mails with notmuch, call afew --move-mails in an offlineimap presynchook and enjoy a clean inbox in your webinterface/GUI-client at work.

For information on how to configure rules for move mode, what you can do with it and what you can't, please refer to move_mode.

The full set of options is:

$ afew --help
Usage: afew [options] [--] [query]
  -h, --help            show this help message and exit
    Please specify exactly one action.
    -t, --tag           run the tag filters
    -w, --watch         continuously monitor the mailbox for new files
    -m, --move-mails    move mail files between maildir folders
  Query modifiers:
    Please specify either --all or --new or a query string.
    -a, --all           operate on all messages
    -n, --new           operate on all new messages
  General options:
    -C NOTMUCH_CONFIG, --notmuch-config=NOTMUCH_CONFIG
                        path to the notmuch configuration file [default:
                        $NOTMUCH_CONFIG or ~/.notmuch-config]
    -e ENABLE_FILTERS, --enable-filters=ENABLE_FILTERS
                        filter classes to use, separated by ',' [default:
                        filters specified in afew's config]
    -d, --dry-run       don't change the db [default: False]
    -R REFERENCE_SET_SIZE, --reference-set-size=REFERENCE_SET_SIZE
                        size of the reference set [default: 1000]
    -T DAYS, --reference-set-timeframe=DAYS
                        do not use mails older than DAYS days [default: 30]
    -v, --verbose       be more verbose, can be given multiple times

Customization of tag filters takes place in afew's config file in ~/.config/afew/config.

afew tries to adapt to the new tag that notmuch sets on new email, but has mostly been developed and used against the new tag. To use that, make sure that ~/.notmuch-config contains:


You can modify filters, and define your own versions of the base Filter that allow you to tag messages in a similar way to the notmuch tag command, using the config file. The default config file is:


See the filters page for the details of those filters and the custom arguments they accept.

You can add filters based on the base filter as well. These can be customised by specifying settings beneath them. The standard settings, which apply to all filters, are:

text that will be displayed while running this filter if the verbosity is high enough.
the query to use against the messages, specified in standard notmuch format. Note that you don't need to specify the new tag - afew will add that when run with the --new flag.
the tags to add or remove for messages that match the query. Tags to add are preceded by a + and tags to remove are preceded by a -. Multiple tags are separated by semicolons.
if the message has one of these tags, don't add tags to it. Tags are separated by semicolons.

So to add the deer tag to any message to or from you could do:

query = ''
tags = +deer
message = Wild animals ahoy

You can also (in combination with the InboxFilter) have email skip the Inbox by removing the new tag before you get to the InboxFilter:

query = from''
tags = -new;+boss
message = Message from above

Showing some sample configs is the easiest way to understand. The notmuch initial tagging page shows a sample config:

# immediately archive all messages from "me"
notmuch tag -new -- tag:new and
# delete all messages from a spammer:
notmuch tag +deleted -- tag:new and
# tag all message from notmuch mailing list
notmuch tag +notmuch -- tag:new and
# finally, retag all "new" messages "inbox" and "unread"
notmuch tag +inbox +unread -new -- tag:new

The (roughly) equivalent set up in afew would be:

message = Delete all messages from spammer
query =
tags = +deleted;-new
message = Tag all messages from the notmuch mailing list
query =
tags = +notmuch

Not that the queries do not generally include tag:new because this is implied when afew is run with the --new flag.

The differences between them is that

  • the ArchiveSentMailsFilter will add the sent tag, as well as archiving the email. And it will not archive email that has been sent to one of your own addresses.
  • the InboxFilter does not add the unread tag. But most mail clients will manage the unread status directly in maildir.

Here are a few more example filters from github dotfiles:

query = ''
tags = +sicsa
message = sicsa
query = ' OR from:GT Silber OR'
tags = +soc;+foo
message = foosoc
query = 'folder:gmail/G+'
tags = +G+
message = gmail spam
# skip inbox
query = ' AND (subject:emacs OR subject:elisp OR "(defun" OR "(setq" OR PATCH)'
tags = -new
message = notmuch emacs stuff

The default filter set (if you don't specify anything in the config) is:


The standard filter configuration can be applied to these filters as well. Though note that most of the filters below set their own value for message, query and/or tags, and some ignore some of the standard settings.

The settings you can use are:
spam_tag = <tag>

  • Add <tag> to all mails recognized as spam.
  • The default is 'spam'.
  • You may use it to tag your spam as 'junk', 'scum' or whatever suits your mood. Note that only a single tag is supported here.

Email will be considered spam if the header X-Spam-Flag is present.

If the new message has been added to a thread that has already been tagged killed then add the killed tag to this message. This allows for ignoring all replies to a particular thread.

This filter looks for the List-Id header, and if it finds it, adds a tag lists and a tag named lists/<list-id>.

The settings you can use are:
sent_tag = <tag>

  • Add <tag> to all mails sent from one of your configured mail addresses.
  • The default is to add no tag, so you need to specify something.
  • You may e.g. use it to tag all mails sent by you as 'sent'. This may make special sense in conjunction with a mail client that is able to not only search for threads but individual mails as well.

    More accurately, it looks for emails that are from one of your addresses and not to any of your addresses.

to_transforms = <transformation rules>

  • Transform To/Cc/Bcc e-mail addresses to tags according to the specified rules. <transformation rules> is a space separated list consisting of 'user_part@domain_part:tags' style pairs. The colon separates the e-mail address to be transformed from tags it is to be transformed into. ':tags' is optional and if empty, 'user_part' is used as tag. 'tags' can be a single tag or semi-colon separated list of tags.
  • It can be used for example to easily tag posts sent to mailing lists which at this stage don't have List-Id field.

It extends SentMailsFilter with the following feature:
Emails filtered by this filter have the new tag removed, so will not have the inbox tag added by the InboxFilter.

This removes the new tag, and adds the inbox tag, to any message that isn't killed or spam. (The new tags are set in your notmuch config, and default to just new.)

This filter adds tags to a message if the named header matches the regular expression given. The tags can be set, or based on the match. The settings you can use are:
  • header = <header_name>
  • pattern = <regex_pattern>
  • tags = <tag_list>

If you surround a tag with {} then it will be replaced with the named match.

Some examples are:

header = X-Spam-Flag
pattern = YES
tags = +spam
header = List-Id
pattern = <(?P<list_id>.*)>
tags = +lists;+{list_id}
header = X-Redmine-Project
pattern = (?P<project>.*)
tags = +redmine;+{project}

SpamFilter and ListMailsFilter are implemented using HeaderMatchingFilter, and are only slightly more complicated than the above examples.

For each email, it looks at all folders it is in, and uses the path and filename as a tag, for the email. So if you have a procmail or sieve set up that puts emails in folders for you, this might be useful.
folder_explicit_list = <folder list>

  • Tag mails with tag in <folder list> only. <folder list> is a space separated list, not enclosed in quotes or any other way.
  • Empty list means all folders (of course blacklist still applies).
  • The default is empty list.
  • You may use it e.g. to set tags only for specific folders like 'Sent'.

folder_blacklist = <folder list>

  • Never tag mails with tag in <folder list>. <folder list> is a space separated list, not enclosed in quotes or any other way.
  • The default is to blacklist no folders.
  • You may use it e.g. to avoid mails being tagged as 'INBOX' when there is the more standard 'inbox' tag.

folder_transforms = <transformation rules>

  • Transform folder names according to the specified rules before tagging mails. <transformation rules> is a space separated list consisting of 'folder:tag' style pairs. The colon separates the name of the folder to be transformed from the tag it is to be transformed into.
  • The default is to transform to folder names.
  • You may use the rules e.g. to transform the name of your 'Junk' folder into your 'spam' tag or fix capitalization of your draft and sent folder:

folder transforms = Junk:spam Drafts:draft Sent:sent

folder_lowercases = true
Use lowercase tags for all folder names

maildir_separator = <sep>

  • Use <sep> to split your maildir hierarchy into individual tags.
  • The default is to split on '.'
  • If your maildir hierarchy is represented in the filesystem as collapsed dirs, <sep> is used to split it again before applying tags. If your maildir looks like this:


the mails in your afew folder will be tagged with 'devel' and 'afew'.

If instead your hierarchy is split by a more conventional '/' or any other divider


you need to configure that divider to have your mails properly tagged:

maildir_separator = /

To customize these filters, there are basically two different possibilities:

Let's say you like the SpamFilter, but it is way too polite

Create an filter object and customize it

[SpamFilter.0] # note the index
message = meh

The index is required if you want to create a new SpamFilter in addition to the default one. If you need just one customized SpamFilter, you can drop the index and customize the default instance.

Create a new type...

message = I hatez teh spam!

and create an object or two

message = Me hatez it too.

You can provide your own filter implementations too. You have to register your filters via entry points. See the afew for examples on how to register your filters. To add your filters, you just need to install your package in the context of the afew application.

Here is a full sample configuration for move mode:

folders = INBOX Junk
rename = False
max_age = 15
# rules
INBOX = 'tag:spam':Junk 'NOT tag:inbox':Archive
Junk = 'NOT tag:spam AND tag:inbox':INBOX 'NOT tag:spam':Archive

Below we explain what each bit of this means.

First you need to specify which folders should be checked for mails that are to be moved (as a whitespace separated list):

folders = INBOX Junk

Then you have to specify rules that define move actions of the form

<src> = ['<qry>':<dst>]+

Every mail in the <src> folder that matches a <qry> will be moved into the <dst> folder associated with that query. A message that matches multiple queries will be copied to multiple destinations.

You can bind as many rules to a maildir folder as you deem necessary. Just add them as elements of a (whitespace separated) list.

Please note, though, that you need to specify at least one rule for every folder given by the folders option and at least one folder to check in order to use the move mode.

INBOX = 'tag:spam':Junk

will bind one rule to the maildir folder INBOX that states that all mails in said folder that carry (potentially among others) the tag spam are to be moved into the folder Junk.

With <qry> being an arbitrary notmuch query, you have the power to construct arbitrarily flexible rules. You can check for the absence of tags and look out for combinations of attributes:

Junk = 'NOT tag:spam AND tag:inbox':INBOX 'NOT tag:spam':Archive

The above rules will move all mails in Junk that don't have the spam tag but do have an inbox tag into the directory INBOX. All other mails not tagged with spam will be moved into Archive.

You can limit the age of mails you want to move by setting the max_age option in the configuration section. By providing

max_age = 15

afew will only check mails at most 15 days old.

Set this option if you are using the mbsync IMAP syncing tool. mbsync adds a unique identifier to files' names when it syncs them. If the rename option is not set, moving files can cause UID conflicts and prevent mbsync from syncing with error messages such as "Maildir error: duplicate UID 1234" or "UID 567 is beyond highest assigned UID 89".

When the option is set, afew will rename files while moving them, removing the UID but preserving other mbsync information. This allows mbsync to assign a new UID to the file and avoid UID conflicts.

If you are using offlineimap, you can safely ignore this option.

rename = True

(1) Rules don't manipulate tags.

INBOX = 'NOT tag:inbox':Archive
Junk = 'NOT tag:spam':INBOX

The above combination of rules might prove tricky, since you might expect de-spammed mails to end up in INBOX. But since the Junk rule will not add an inbox tag, the next run in move mode might very well move the matching mails into Archive.

Then again, if you remove the spam tag and do not set an inbox tag, how would you come to expect the mail would end up in your INBOX folder after moving it? ;)

(2) There is no 1:1 mapping between folders and tags. And that's a feature. If you tag a mail with two tags and there is a rule for each of them, both rules will apply. Your mail will be copied into two destination folders, then removed from its original location.

You can put python files in ~/.config/afew/ and they will be imported by afew. If you use that python file to define a Filter class and use the register_filter decorator then you can refer to it in your filter configuration.

So an example small filter you could add might be:

from afew.Filter import Filter, register_filter
    'fabric': 'deployment',
    'oldname': 'new-name',
class RedmineFilter(Filter):
    message = 'Create tag based on redmine project'
    query = 'NOT tag:redmine'
    def handle_message(self, message):
        project = message.get_header('X-Redmine-Project')
        if project in PROJECT_MAPPING:
            project = PROJECT_MAPPING[project]
        self.add_tags(message, 'redmine', project)

We have defined the message and query class variables that are used by the parent class Filter. The message is printed when running with verbose flags. The query is used to select messages to run against - here we ensure we don't bother looking at messages we've already looked at.

The handle_message() method is the key one to implement. This will be called for each message that matches the query. The argument is a notmuch message object and the key methods used by the afew filters are get_header(), get_filename() and get_thread().

Of the methods inherited from the Filter class the key ones are add_tags() and remove_tags(), but read about the implementation or just read the source code to get your own ideas.

Once you've defined your filter, you can add it to your config like any other filter:


The design of the database manager was inspired by alots database manager alot.db.DBManager.

  • genindex
  • modindex
  • search

Justus Winter

2018, Justus Winter
December 29, 2018 0.1