]> git.cworth.org Git - notmuch-wiki/blob - howto.mdwn
Debian squeeze users should start with up to date versions from backports.
[notmuch-wiki] / howto.mdwn
1 # How to...
2
3 Some tips about how to do some useful things with notmuch, and the
4 various "third party" notmuch utilities.
5
6 [[!toc levels=2]]
7
8 ## <span id="debian_packages">**Debian squeeze packages outdated**</span>
9
10 The notmuch package(s) in Debian squeeze are very old (version 0.3).
11 It's better to start off with an up to date version from
12 [backports](http://backports-master.debian.org/).
13
14 ## <span id="receive_mail">**Receive mail**</span>
15
16 Notmuch does not fetch mail for you.  For that, you need to use an
17 external mail syncing utility.  Some recommended utilities are listed
18 below.
19
20 Notmuch requires that every individual message be in it's own file.
21 The well-supported [maildir](http://cr.yp.to/proto/maildir.html) or
22 "mh"-style storage formats are compatible with notmuch.  Basically any
23 setup in which each mail is in a file of its own will work.  The older
24 mbox mail store formats is not supported, but fortunately it is very
25 easy to [[convert mbox to maildir|howto/#mbox]] .  The following
26 utilities support these formats:
27
28 * [offlineimap](https://github.com/nicolas33/offlineimap/) -
29   quite useful and widely tested, it also offers a handy hook that
30   will come in useful a bit later in our setup.  Also supports
31   "presynchook" and "postsynchook" command that will get run
32   whenever you sync.  Point _postsynchook_ to a script that gets run
33   on every sync and that will do the automatic updating and tagging
34   of your notmuch database.
35
36 * [mbsync](http://isync.sourceforge.net/)
37
38 * [getmail](http://pyropus.ca/software/getmail/)
39
40 * [fetchmail](http://fetchmail.berlios.de/)
41
42 See the [[initial_tagging]] page for more info on initial tagging of
43 messages.
44
45 ## <span id="sync_maildir_flags">**Sync notmuch tags and maildir flags**</span>
46
47 notmuch has the ability to synchronize maildir flags and respective tags in both
48 directions. For more information on maildir flags see the [maildir
49 page](http://cr.yp.to/proto/maildir.html), and for the respective tags see your
50 notmuch configuration file. This feature is enabled by default, but if you don't
51 need it, it is simple to disable it with the 'notmuch config' command:
52
53         $ notmuch config set maildir.synchronize_flags false
54
55 The maildir flags may, in turn, be synchronized with IMAP flags by another tool,
56 such as offlineimap.
57
58 For safety reasons, and because
59 [[notmuch does not support delete operations|deleting]], notmuch does
60 not sync the "trashed" flag.  For discussion on this topic please
61 refer to the mailing list.
62
63 ## <span id="python">**Use notmuch from python**</span>
64
65 Notmuch includes python bindings to the notmuch shared library. Please
66 refer to the nice and extensive
67 [notmuch python API documentation](http://notmuch.readthedocs.org/).
68
69 The bindings are very simple to use.  As an example, given you have
70 the python bindings installed (or simply set your PYTHONPATH
71 environment variable to point to the .../bindings/python directory),
72 this snippet will produce a list of mails matching the given
73 expression:
74
75         >>> import notmuch
76         >>> db = notmuch.Database()
77         >>> query = db.create_query('tag:inbox AND NOT tag:killed')
78         >>> list(query.search_messages()) # doctest:+ELLIPSIS
79         [...]
80
81 ## <span id="print_filenames">**Using notmuch with Mutt**</span>
82
83 Notmuch is a great mail indexing tool that can also be used *in conjunction*
84 with existing Mail User Agents (MUA) instead of replacing them. The advantage
85 of such mixed solutions is that users can benefit from notmuch features (such
86 as full-text search and thread reconstruction) without *having to* change
87 MUA.
88
89 A popular geek MUA is [the Mutt e-mail client](http://www.mutt.org);
90 integrating notmuch with Mutt is not seamless, but fairly straightforward.
91 There are two principal possibilities, each with its own ups and downs:
92
93 * Using mutt-notmuch, which will create a "virtual" maildir folder with
94   search results whenever a search is made. The upside is that you can
95   search all your folders simultanously; the downside is that your
96   modifications in the results listing do not carry over, also having
97   to switch folders comes with some more annoyances.
98
99   An
100   [**how to use Notmuch with Mutt**](http://upsilon.cc/~zack/blog/posts/2011/01/how_to_use_Notmuch_with_Mutt/)
101   has been written by Stefano Zacchiroli. Check out the
102   [howto](http://upsilon.cc/~zack/blog/posts/2011/01/how_to_use_Notmuch_with_Mutt/)
103   for more information.
104
105   <small>(Note by the howto author: I've linked the howto from this wiki rather
106   than splicing it in, in order to avoid duplication of information. If you
107   think it would be better to have it here, feel free to copy the text
108   here. The howto is
109   [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)
110   in markdown syntax from the Git repository of my homepage.)</small>
111
112 * Using a simple macro that will emulate the "limit" mutt functionality
113   using notmuch.
114
115   See the [**alternative notmuch integration**](http://log.or.cz/?p=228)
116   blog post for instructions and details,
117   or simply put these two macros to your muttrc:
118
119         # 'L' performs a notmuch query, showing only the results
120         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"
121         # 'a' shows all messages again (supersedes default <alias> binding)
122         macro index a "<limit>all\n" "show all messages (undo limit)"
123
124   The upside (if you are used to working in the context of a single folder)
125   is that this really does use the limit functionality of mutt
126   and you are still in your original folder.
127   The downside is that this approach has scaling problems
128   and works well only for reasonably specific queries.
129
130
131 ## <span id="reapply_auto">**Automatically retagging the database (e.g., when upgrading versions)**</span>
132
133 Certain versions of notmuch include new automatic tags (for example, between
134 0.3 and 0.10, automatic tagging of signed and encrypted messages was added).
135 However, for users running with databases created in older versions of
136 notmuch, these tags are missing in pre-existing messages and need to be
137 added.  One way to do this is as follows:
138
139         $ notmuch dump ~/out.nm
140         $ mv ~/Mail/.notmuch ~/.notmuch.bak
141         $ notmuch new
142         $ notmuch tag -inbox -unread '*'
143         $ notmuch restore --accumulate ~/out.nm
144
145 At this point, one should run a sanity check on the tags, and if everything
146 has merged correctly, the ~/.notmuch.bak directory is expendable, as is
147 ~/out.nm.
148
149 ## <span id="mbox">**Dealing with mbox and other formats**</span>
150
151 notmuch by itself is unable to handle non-maildir mail archives.  One tool
152 to solve this is called mb2md.  Assuming an mbox in ~/test.mbox and ones
153 mail archives to be in ~/Mail, an invocation would look like
154
155         $ mb2md -s ~/test.mbox -d ~/Mail/mynewmaildirname
156
157 Note that specifying the paths for -s and -d is necessary.  This will create
158 a new maildir in ~/Mail/mynewmaildirname from the mbox at ~/test.mbox.
159
160 Often the formats are more convoluted, however.  Many lists provide an
161 almost-but-not-quite-mbox format that mailman produces, as can be seen, for
162 example, [here](http://lists.xapian.org/pipermail/xapian-devel/).  These
163 files can be converted with some degree of success to mbox using the script
164 found
165 [here](http://www.hermann-uwe.de/blog/converting-mailman-gzipd-text-archive-files-to-proper-mbox-files),
166 and from mbox to maildir as above.
167
168 However, many of these lists also have a gmane version, which, where it
169 exists, achieves far better results than dealing with the messy mailman
170 output.  Using the instructions from [Gmane's
171 site](http://gmane.org/export.php), we can download an mbox file, which we
172 can then convert to maildir using mb2md or other utility.
173
174 ## <span id="special_tags">**Take advantage of tags that are special to notmuch**</span>
175
176 See [[tags special to notmuch|special-tags]].