]> git.cworth.org Git - notmuch-wiki/blob - howto.mdwn
howto: add table of contents
[notmuch-wiki] / howto.mdwn
1 [[!img notmuch-logo.png alt="Notmuch logo" class="left"]]
2 #How to...
3
4 Some tips about how to do some useful things with notmuch, and the
5 various "third party" notmuch utilities.
6
7 [[!toc levels=2]]
8
9 ## <span id="receive_mail">**Receive mail**</span>
10
11 Notmuch requires either [maildir
12 flag](http://cr.yp.to/proto/maildir.html) or a "mh"-style
13 maildirectory to operate on.  Basically any setup in which each mail
14 is in a file of its own will work.  Here are some generally
15 well-regarded mail retrieval tools:
16
17 * [offlineimap](https://github.com/nicolas33/offlineimap/) -
18   quite useful and widely tested, it also offers a handy hook that
19   will come in useful a bit later in our setup.  Also supports
20   "presynchook" and "postsynchook" command that will get run
21   whenever you sync.  Point _postsynchook_ to a script that gets run
22   on every sync and that will do the automatic updating and tagging
23   of your notmuch database.
24
25 * [mbsync](http://isync.sourceforge.net/)
26
27 * [getmail](http://pyropus.ca/software/getmail/)
28
29 * [fetchmail](http://fetchmail.berlios.de/)
30
31 See the [[initial_tagging]] page for more info on initial tagging of messages.
32
33 ## <span id="python">**Use notmuch from python**</span>
34
35 Notmuch includes python bindings to the notmuch shared
36 library. Extensive API documentation [is
37 available](http://notmuch.readthedocs.org/).
38
39 The bindings are very simple to use.  As an example, given you have
40 the python bindings installed (or simply set your PYTHONPATH
41 environment variable to point to the .../bindings/python directory),
42 this snippet will produce a list of mails matching the given
43 expression:
44
45         >>> import notmuch
46         >>> db = notmuch.Database()
47         >>> query = db.create_query('tag:inbox AND NOT tag:killed')
48         >>> list(query.search_messages()) # doctest:+ELLIPSIS
49         [...]
50
51 ## <span id="sync_maildir_flags">**Sync notmuch tags and maildir flags**</span>
52
53 notmuch **since version 0.5** syncs maildir flags and respective
54 tags in both directions. This part applies only to notmuch prior to
55 version 0.5 (but please consider updating to a more recent version
56 of notmuch; in addition to maildir tag syncing, it contains many
57 useful new features, and several important bug fixes):
58
59 Some IMAP users rely on maildir flags that convey the status "seen",
60 "replied", "trashed", in order to synchronize the status of their
61 mail across mail clients (a [maildir
62 flag](http://cr.yp.to/proto/maildir.html) is simply a defined part
63 of their filename).  Notmuch will by default happily ignore those
64 flags and will never modify them either, as there is no built-in
65 support for synchronizing your notmuch tags with your imap maildir
66 flags (seen, replied, trashed, etc). However there are currently 2
67 ways in which you can achieve synchronization.
68
69 [notmuchsync](http://spaetz.github.com/notmuchsync/) is a utility
70 (based on the cnotmuch python bindings) that allows synchronization
71 in either direction, as well as pruning of deleted files.
72
73 The second solution allows for fast maildir flag to notmuch tag
74 synchronization (and only in that direction) requires patching the
75 notmuch source code. The patch has been posted in this mail
76 *id:1267450136-31749-1-git-send-email-Sebastian@SSpaeth.de* and can
77 be viewed as a [source diff in this git
78 repository](http://github.com/spaetz/notmuch-all-feature/commit/df3b087cefb85e9d16fd17540e348bcb854dd7ee).
79
80 ## <span id="print_filenames">**Using notmuch with Mutt**</span>
81
82 Notmuch is a great mail indexing tool that can also be used *in conjunction*
83 with existing Mail User Agents (MUA) instead of replacing them. The advantage
84 of such mixed solutions is that users can benefit from notmuch features (such
85 as full-text search and thread reconstruction) without *having to* change
86 MUA.
87
88 A popular geek MUA is [the Mutt e-mail client](http://www.mutt.org);
89 integrating notmuch with Mutt is not seamless, but fairly straightforward.
90 There are two principal possibilities, each with its own ups and downs:
91
92 * Using mutt-notmuch, which will create a "virtual" maildir folder with
93   search results whenever a search is made. The upside is that you can
94   search all your folders simultanously; the downside is that your
95   modifications in the results listing do not carry over, also having
96   to switch folders comes with some more annoyances.
97
98   An
99   [**how to use Notmuch with Mutt**](http://upsilon.cc/~zack/blog/posts/2011/01/how_to_use_Notmuch_with_Mutt/)
100   has been written by Stefano Zacchiroli. Check out the
101   [howto](http://upsilon.cc/~zack/blog/posts/2011/01/how_to_use_Notmuch_with_Mutt/)
102   for more information.
103
104   <small>(Note by the howto author: I've linked the howto from this wiki rather
105   than splicing it in, in order to avoid duplication of information. If you
106   think it would be better to have it here, feel free to copy the text
107   here. The howto is
108   [available](http://git.upsilon.cc/cgi-bin/gitweb.cgi?p=zack-homepage.git;a=history;f=blog/posts/2011/01/how_to_use_Notmuch_with_Mutt.mdwn)
109   in markdown syntax from the Git repository of my homepage.)</small>
110
111 * Using a simple macro that will emulate the "limit" mutt functionality
112   using notmuch.
113
114   See the [**alternative notmuch integration**](http://log.or.cz/?p=228)
115   blog post for instructions and details,
116   or simply put these two macros to your muttrc:
117
118                          # 'L' performs a notmuch query, showing only the results
119                          macro index L "<enter-command>unset wait_key<enter><shell-escape>read -p 'notmuch query: ' x; echo \$x >~/.cache/mutt_terms<enter><limit>~i \"\`notmuch search --output=messages \$(cat ~/.cache/mutt_terms) | head -n 600 | perl -le '@a=<>;chomp@a;s/\^id:// for@a;$,=\"|\";print@a'\`\"<enter>" "show only messages matching a notmuch pattern"
120                          # 'a' shows all messages again (supersedes default <alias> binding)
121                          macro index a "<limit>all\n" "show all messages (undo limit)"
122
123   The upside (if you are used to working in the context of a single folder)
124   is that this really does use the limit functionality of mutt
125   and you are still in your original folder.
126   The downside is that this approach has scaling problems
127   and works well only for reasonably specific queries.
128
129
130 ## <span id="reapply_auto">**Automatically retagging the database (e.g., when upgrading versions)**</span>
131
132 Certain versions of notmuch include new automatic tags (for example, between
133 0.3 and 0.10, automatic tagging of signed and encrypted messages was added).
134 However, for users running with databases created in older versions of
135 notmuch, these tags are missing in pre-existing messages and need to be
136 added.  One way to do this is as follows:
137
138                          $ notmuch dump ~/out.nm
139                          $ mv ~/Mail/.notmuch ~/.notmuch.bak
140                          $ notmuch new
141                          $ notmuch tag -inbox -unread '*'
142                          $ notmuch restore --accumulate ~/out.nm
143
144 At this point, one should run a sanity check on the tags, and if everything
145 has merged correctly, the ~/.notmuch.bak directory is expendable, as is
146 ~/out.nm.
147
148 ## <span id="nbox">**Dealing with mbox and other formats**</span>
149
150 notmuch by itself is unable to handle non-maildir mail archives.  One tool
151 to solve this is called mb2md.  Assuming an mbox in ~/test.mbox and ones
152 mail archives to be in ~/Mail, an invocation would look like
153
154                          $ mb2md -s ~/test.mbox -d ~/Mail/mynewmaildirname
155
156 Note that specifying the paths for -s and -d is necessary.  This will create
157 a new maildir in ~/Mail/mynewmaildirname from the mbox at ~/test.mbox.
158
159 Often the formats are more convoluted, however.  Many lists provide an
160 almost-but-not-quite-mbox format that mailman produces, as can be seen, for
161 example, [here](http://lists.xapian.org/pipermail/xapian-devel/).  These
162 files can be converted with some degree of success to mbox using the script
163 found
164 [here](http://www.hermann-uwe.de/blog/converting-mailman-gzipd-text-archive-files-to-proper-mbox-files),
165 and from mbox to maildir as above.
166
167 However, many of these lists also have a gmane version, which, where it
168 exists, achieves far better results than dealing with the messy mailman
169 output.  Using the instructions from [Gmane's
170 site](http://gmane.org/export.php), we can download an mbox file, which we
171 can then convert to maildir using mb2md or other utility.
172
173 ## <span id="special_tags">**Take advantage of tags that are special to notmuch**</span>
174
175 See [[tags special to notmuch|special-tags]].