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