]> git.cworth.org Git - notmuch-wiki/blob - notmuch-mutt.mdwn
added mutt-kz, procmail, notmuch_delivery and notmuch_abook howto to mutt wikipage
[notmuch-wiki] / notmuch-mutt.mdwn
1 [[!img notmuch-logo.png alt="Notmuch logo" class="left"]]
2
3 Notmuch is a great mail indexing tool that can also be used *in conjunction*
4 with existing Mail User Agents (MUA) instead of replacing them. The advantage of
5 such mixed solutions is that users can benefit from notmuch features (such as
6 full-text search and thread reconstruction) without *having to* change MUA.
7
8 A popular geek MUA is [the Mutt e-mail client](http://www.mutt.org); integrating
9 notmuch with Mutt is not seamless, but fairly straightforward. There are two
10 principal possibilities, each with its own ups and downs:
11
12 # Using Notmuch with mutt-kz
13
14 ## Install:
15
16         git clone https://github.com/karelzak/mutt-kz.git
17         cd mutt-kz
18         ./configure && make && make install
19
20 ## Configuration:
21
22         # notmuch
23         set nm_default_uri="notmuch:///PATH/TO/MY/Maildir" # path to the maildir
24         set virtual_spoolfile=yes                          # enable virtual folders
25         set sendmail="/PATH/TO/bin/nm_sendmail"            # enables parsing of outgoing mail
26         virtual-mailboxes \
27             "INBOX"     "notmuch://?query=tag:INBOX and NOT tag:archive"\
28             "Unread"    "notmuch://?query=tag:unread"\
29             "Starred"   "notmuch://?query=tag:*"\
30             "Sent"      "notmuch://?query=tag:sent"        # sets up queries for virtual folders
31         # notmuch bindings
32         macro index \\ "<vfolder-from-query>"              # looks up a hand mane query
33         macro index A "<modify-labels>+archive -unread -inbox\n"        # tag as Archived
34         macro index I "<modify-labels>-inbox -unread\n"                 # removed from inbox
35         macro index S "<modify-labels-then-hide>-inbox -unread +junk\n" # tag as Junk mail
36         macro index + "<modify-labels>+*\n<sync-mailbox>"               # tag as starred
37         macro index - "<modify-labels>-*\n<sync-mailbox>"               # tag as unstarred
38         # sidebar
39         set sidebar_width   = 20
40         set sidebar_visible = yes               # set to "no" to disable sidebar view at startup
41         color sidebar_new yellow default
42         # sidebar bindings
43         bind index <left> sidebar-prev          # got to previous folder in sidebar
44         bind index <right> sidebar-next         # got to next folder in sidebar
45         bind index <space> sidebar-open         # open selected folder from sidebar
46         # sidebar toggle
47         macro index ,@) "<enter-command> set sidebar_visible=no; macro index ~ ,@( 'Toggle sidebar'<Enter>"
48         macro index ,@( "<enter-command> set sidebar_visible=yes; macro index ~ ,@) 'Toggle sidebar'<Enter>"
49         macro index ~ ,@( 'Toggle sidebar'      # toggle the sidebar
50
51 * Using:
52
53 when you open `mutt` you get the INBOX opened. There you can crawl through your
54 mails, and tag them as appropriate, either manually using the " ` " command, or using
55 the bindings defined in configuration (such as A/I/S/+/-).
56
57 * Mail tagging on sending
58
59 You may have noticed in `mutt-kz`'s configuration that I set the `sendmail` variable
60 of mutt to a `nm_sendmail` script. This is for tagging outgoing mail each time I send
61 a mail. Here is the content of the script (which may be used directly in mutt's 
62 variable, I did not try). 
63
64 Source of nm_sendmail:
65
66         #!/bin/bash
67         tee >(notmuch-deliver -t sent -f Sent) | sendmail $*
68
69 ## Mail filtering/tagging
70
71 For mail tagging on arrival, I prefer to use a simple procmail delivery along with 
72 notmuch-delivery (which can be compiled in the `contrib/` directory of notmuch's sources).
73
74 Of course, you could use formail or maildrop, instead of procmail, but it is flexible
75 enough for my needs, and here is an example of configuration that can be useful:
76
77         PATH=/bin:/usr/bin:/usr/local/bin
78
79         # ensure each mail is unique
80         :0 Wh: msgid.lock
81         | formail -D 8192 msgid.cache
82
83         # update addressbook with current mail
84         :0 Wh
85         | /usr/local/bin/notmuch_abook update
86
87         NOINBOX="-r inbox"
88         TAGS=""
89
90         # manage dynamic tagging, using the ' + ' token in mail addresses
91         # e.g.: user+TAG@fqdn.tld will generate the tag TAG
92         :0:notmuch.lock
93         * ^TO\/user\+[a-z0-9]+@fqdn\.tld
94         * MATCH ?? ^user\+\/[a-z0-9]+
95         {
96         TAGS="-t ${MATCH}"
97         }
98
99         # match all mails from mailing-lists, don't let them go to inbox, but tag them with ml
100         :0:notmuch.lock
101         * ^List-[Ii][dD]:.*
102         {
103         TAGS="${TAGS} -t ml -r inbox"
104         }
105
106         # tag all mails coming from mutt-kz mailing list
107         :0:notmuch.lock
108         * .*mutt-kz\.lists\.fedoraproject\.org.*
109         | notmuch-deliver $TAGS -t mutt -t notmuch
110
111         # tag all mails coming from notmuch mailing list
112         :0:notmuch.lock
113         * .*notmuch\.notmuchmail\.org.*
114         | notmuch-deliver $TAGS -t notmuch
115
116         # Mark all spams as junk mail
117         :0:notmuch.lock
118         * ^X-Spam-Status: Yes
119         | notmuch-deliver -t junk
120
121         :0:notmuch.lock
122         * ^Subject: .*SPAM.*
123         | notmuch-deliver -t junk
124
125         ### All unmatched mails
126         :0:notmuch.lock
127         * .*
128         | notmuch-deliver -v $TAGS 
129
130 ## Addressbook management and vim
131
132 There are some emacs tips over [here](http://notmuchmail.org/emacstips/#index15h2) that
133 explains how to configure emacs with an addressbook, but the few solutions were not fast enough
134 for me. And I never could test the vala-based code. So I updated the notmuch_addresses code to
135 use a cache to be able to make better matches and still be lightning fast.
136
137 And finally, I needed to have a way to autocomplete my recipients' addresses 
138 automagically from within `vim`. So that's why I created the `notmuch_abook` code.
139 You can either install it as a plugin in vim, using vundle:
140
141         Vundle 'guyzmo/notmuch-abook'
142
143 Or if you're not using vim (which is objectively the best editor ever), you may
144 want to use it as standalone:
145
146         % pip install notmuch_abook
147
148 To configure the address book, append to `~/.notmuch-config` the following:
149
150         % cat > ~/.notmuch-config << EOF
151         
152         [addressbook]
153         path=/home/YOURUSERNAME/.notmuch-abook.db
154         backend=sqlite3
155
156         EOF
157
158 where YOURUSERNAME is your home directory. Then, you can sync the addressbook cache
159 by running. It will create the database file specified in configuration, and go through
160 all notmuch's indexed mails to get all addresses headers and cache them. It takes around
161 20 seconds on my 10000 mails index:
162
163         % notmuch_abook create
164
165 And you can query for addresses using the lookup command, which will match the beginning
166 of each word in the name and address, as follows:
167
168         % notmuch_abook lookup Foo
169         Foobar Bar <bar@bar.com>
170         Bar Foobar <bar@bar.com>
171         Bar Bar <foobar@bar.com>
172         Bar Bar <bar@foobar.com>
173         Bar Bar <bar@bar.foo>
174
175 If you're using vim, when you edit a mail having a filetype set to mail, you will be
176 able to open a completion menu when you do <C-x><C-u> at every address header.
177
178 # Using Notmuch with bare Mutt, the old fashioned way
179
180 * Using
181   [notmuch-mutt](http://git.notmuchmail.org/git/notmuch/tree/HEAD:/contrib/notmuch-mutt),
182   which will create a "virtual" maildir folder with search results whenever a
183   search is made. The upside is that you can search all your folders
184   simultanously; the downside is that your modifications in the results listing
185   do not carry over, also having to switch folders comes with some more
186   annoyances.
187
188   A
189   [how to use Notmuch with Mutt](http://upsilon.cc/~zack/blog/posts/2011/01/how_to_use_Notmuch_with_Mutt/)
190   has been written by Stefano Zacchiroli.
191
192   <small>(Note by the howto author: I've linked the howto from this wiki rather
193   than splicing it in, in order to avoid duplication of information. If you
194   think it would be better to have it here, feel free to copy the text
195   here. The howto is
196   [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)
197   in markdown syntax from the Git repository of my homepage.)</small>
198
199 * Using a simple macro that will emulate the "limit" mutt functionality
200   using notmuch.
201
202   See the [alternative notmuch integration](http://log.or.cz/?p=228)
203   blog post for instructions and details,
204   or simply put these two macros to your muttrc:
205
206         # 'L' performs a notmuch query, showing only the results
207         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"
208         # 'a' shows all messages again (supersedes default <alias> binding)
209         macro index a "<limit>all\n" "show all messages (undo limit)"
210
211   The upside (if you are used to working in the context of a single folder) is
212   that this really does use the limit functionality of mutt and you are still in
213   your original folder.  The downside is that this approach has scaling problems
214   and works well only for reasonably specific queries.
215