]> git.cworth.org Git - notmuch-wiki/blob - emacstips.mdwn
argh, untabify
[notmuch-wiki] / emacstips.mdwn
1 # Tips and Tricks for using notmuch with Emacs
2
3 One of the more popular notmuch message reading clients is
4 **notmuch.el**, an [emacs](http://www.gnu.org/software/emacs/) major
5 mode for interacting with notmuch.  It is included in the notmuch
6 package.  This page goes over some usage tips for using notmuch with
7 Emacs.
8
9 [[!toc levels=2]]
10
11 ## Setup
12
13 To use the Notmuch emacs mode, first add the following line to your
14 `.emacs` rc file:
15
16         (require 'notmuch)
17
18 or you can load the package via autoload:
19
20         (autoload 'notmuch "notmuch" "notmuch mail" t)
21
22 Then, either run "emacs -f notmuch", or execute the command `M-x
23 notmuch` from within a running emacs.
24
25 ## Navigating & reading mails
26
27 When first starting notmuch in emacs, you will be presented with the
28 notmuch "hello" page.  From here you can do searches, see lists of
29 recent searches, saved searches, message tags, help information, etc.
30
31 Executing a search will open a new buffer in `notmuch-search-mode`
32 displaying the search results.  Each line in the search results
33 represents a message thread.  Hitting the '?' key will show help for
34 this mode.
35
36 In general, the 'q' will kill the current notmuch buffer and return
37 you to the previous buffer (sort of like a 'pop').
38
39 In search mode, navigating to a thread and hitting return will then
40 open a new buffer in `notmuch-show-mode`, which will show the actual
41 message contents of the thread.
42
43 ## Sending mail
44
45 In any notmuch mode, you can start a new message by hitting the 'm'
46 key.  To reply to a message or thread, just hit the 'r' key.
47
48 When composing new messages, you will be entered in emacs's
49 `message-mode`, which is a powerful mode for composing and sending
50 messages.  When in message mode, you can type `C-c ?` for help.
51
52 If you would like to use address autocompletion when composing
53 messages, see [address completion](#address_completion).
54
55 When you are ready to send a message, type `C-c C-c`. By default
56 message mode will use your sendmail command to send mail, so make sure
57 that works. One annoying standard configuration of message mode is
58 that it will hide the sent mail in your emacs frame stack, but it will
59 not close it. If you type several mails in an emacs session they will
60 accumulate and make switching between buffers more annoying. You can
61 avoid that behavior by adding `(setq message-kill-buffer-on-exit t)`
62 in your `.emacs` file (or doing `M-x
63 customize-variable<RET>message-kill-buffer-on-exit<RET>`) which will
64 really close the mail window after sending it.
65
66 ## Attaching files
67
68 Using the `M-x mml-attach-file` command, you can attach any file to be
69 sent with your mail. By default this command is bound to the menu item
70 *Attachments--Attach File* with the key binding `C-c C-a`. The
71 variable `mml-dnd-attach-options` (`M-x
72 customize-variable<RET>mml-dnd-attach-options<RET>`) can be set to
73 allow the prompting for various attachment options (such as
74 inline/attachment) if you want to do that.
75
76 For those who prefer a more graphical interface, you can also simply
77 drag and drop files from a file manager into a mail composition window
78 to have them attached. In Ubuntu this works without any modifications
79 if files are dragged from the file manager.
80
81 And for those who prefer working from command line, the following
82 script opens new emacs window with empty message and attaches files
83 mentioned as script arguments. (Note: The script expects that you have
84 `(server-start)` in your `.emacs` file.)
85
86         #!/bin/sh
87         attach_cmds=""
88         while [ "$1" ]; do
89             fullpath=$(readlink --canonicalize "$1")
90             attach_cmds="$attach_cmds (mml-attach-file \"$fullpath\")"
91             shift
92         done
93         emacsclient -a '' -c -e "(progn (compose-mail) $attach_cmds)"
94
95
96 -----
97
98 # Advanced tips and tweaks
99
100 ## Use separate emacs lisp file for notmuch configuration
101
102 Instead of adding notmuch configuration code to `.emacs`, there
103 is an option to collect those to a separate file (which is only
104 loaded when `notmuch` is invoked). To do this, write, for example
105 a file called `~/.emacs.d/my-notmuch.el`:
106
107         ;;; my-notmuch.el -- my notmuch mail configuration
108         ;;;
109         
110         ;;; add here stuff required to be configured *before*
111         ;;; notmuch is loaded;
112
113         ;; uncomment and modify in case some elisp files are not found in load-path
114         ;; (add-to-list 'load-path "~/vc/ext/notmuch/emacs")
115
116         ;;; load notmuch 
117         (require 'notmuch)
118
119         ;;; add here stuff required to be configured *after*
120         ;;; notmuch is loaded;
121
122         ;; uncomment & modify if you want to use external smtp server to send mail
123         ;; (setq smtpmail-smtp-server "smtp.server.tld"
124         ;;       message-send-mail-function 'message-smtpmail-send-it)
125         ;; uncomment to debug smtp sending problems
126         ;; (setq smtpmail-debug-info t)
127
128 Then, add to `.emacs`:
129
130         (autoload 'notmuch "~/.emacs.d/my-notmuch" "notmuch mail" t)
131
132
133 ## Add a key binding to add/remove/toggle a tag
134
135 The `notmuch-{search,show}-{add,remove}-tag` functions are very useful
136 for making quick tag key bindings.  For instance, here's an example
137 of how to make a key binding to add the "spam" tag and remove the
138 "inbox" tag in notmuch-show-mode:
139
140 In notmuch versions up to 0.11.x
141
142         (define-key notmuch-show-mode-map "S"
143           (lambda ()
144             "mark message as spam"
145             (interactive)
146             (notmuch-show-add-tag "spam")
147             (notmuch-show-remove-tag "inbox")))
148
149 Starting from notmuch 0.12 the functions `notmuch-show-add-tag` and 
150 `notmuch-show-remove-tag` have changed to be more versatile and lost
151 noninteractive use. When upgrading to 0.12 the above needs to be 
152 changed to this:
153
154         (define-key notmuch-show-mode-map "S"
155           (lambda ()
156             "mark message as spam"
157             (interactive)
158             (notmuch-show-tag-message "+spam" "-inbox")))
159
160 You can do the same for threads in `notmuch-search-mode` by just
161 replacing "show" with "search" in the called functions.
162
163 Starting from notmuch 0.12 use `notmuch-search-tag-thread` instead:
164
165         (define-key notmuch-search-mode-map "S"
166           (lambda ()
167             "mark messages in thread as spam"
168             (interactive)
169             (notmuch-show-tag-thread "+spam" "-inbox")))
170
171 Starting from notmuch 0.13 use `notmuch-search-tag` -- it has a little
172 different usage syntax:
173
174         (define-key notmuch-search-mode-map "S"
175           (lambda ()
176             "mark messages in thread as spam"
177             (interactive)
178             (notmuch-search-tag '("+spam" "-inbox"))))
179
180 The definition above makes use of a lambda function, but you could
181 also define a separate function first:
182
183         (defun notmuch-show-tag-spam ()
184           "mark message as spam"
185           (interactive)
186           (notmuch-show-add-tag "spam")
187           (notmuch-show-remove-tag "inbox")))
188         (define-key notmuch-show-mode-map "S" 'notmuch-show-tag-spam)
189
190 (See above for analogy how to apply this for notmuch 0.12 and later)
191
192 Here's a more complicated example of how to add a toggle "deleted"
193 key:
194
195         (define-key notmuch-show-mode-map "d"
196           (lambda ()
197             "toggle deleted tag for message"
198             (interactive)
199             (if (member "deleted" (notmuch-show-get-tags))
200                 (notmuch-show-remove-tag "deleted")
201               (notmuch-show-add-tag "deleted"))))
202
203 And version for notmuch 0.12
204
205         (define-key notmuch-show-mode-map "d"
206           (lambda ()
207             "toggle deleted tag for message"
208             (interactive)
209             (notmuch-show-tag-message
210               (if (member "deleted" (notmuch-show-get-tags))
211                   "-deleted" "+deleted"))))
212
213 ## Adding many tagging keybindings
214
215 If you want to have have many tagging keybindings, you can save the typing
216 the few lines of  boilerplate for every binding (for versions before 0.12,
217 you will need to change notmuch-show-apply-tag-macro).
218
219     (eval-after-load 'notmuch-show
220       '(define-key notmuch-show-mode-map "`" 'notmuch-show-apply-tag-macro))
221
222     (setq notmuch-show-tag-macro-alist
223       (list
224        '("m" "+notmuch::patch" "+notmuch::moreinfo" "-notmuch::needs-review")
225        '("n" "+notmuch::patch" "+notmuch::needs-review" "-notmuch::pushed")
226        '("o" "+notmuch::patch" "+notmuch::obsolete"
227              "-notmuch::needs-review" "-notmuch::moreinfo")
228        '("p" "-notmuch::pushed" "-notmuch::needs-review"
229          "-notmuch::moreinfo" "+pending")
230        '("P" "-pending" "-notmuch::needs-review" "-notmuch::moreinfo" "+notmuch::pushed")
231        '("r" "-notmuch::patch" "+notmuch::review")
232        '("s" "+notmuch::patch" "-notmuch::obsolete" "-notmuch::needs-review" "-notmuch::moreinfo" "+notmuch::stale")
233        '("t" "+notmuch::patch" "-notmuch::needs-review" "+notmuch::trivial")
234        '("w" "+notmuch::patch" "+notmuch::wip" "-notmuch::needs-review")))
235
236     (defun notmuch-show-apply-tag-macro (key)
237       (interactive "k")
238       (let ((macro (assoc key notmuch-show-tag-macro-alist)))
239         (apply 'notmuch-show-tag-message (cdr macro))))
240
241 ## Restore reply-to-all key binding to 'r'
242
243 Starting from notmuch 0.12 the 'r' key is bound to reply-to-sender instead of
244 reply-to-all. Here's how to swap the reply to sender/all bindings in show mode:
245
246         (define-key notmuch-show-mode-map "r" 'notmuch-show-reply)
247         (define-key notmuch-show-mode-map "R" 'notmuch-show-reply-sender)
248
249 And in search mode:
250
251         (define-key notmuch-search-mode-map "r" 'notmuch-search-reply-to-thread)
252         (define-key notmuch-search-mode-map "R" 'notmuch-search-reply-to-thread-sender)
253
254
255 ## How to do FCC/BCC...
256
257 The Emacs interface to notmuch will automatically add an `Fcc`
258 header to your outgoing mail so that any messages you send will also
259 be saved in your mail store. You can control where this copy of the
260 message is saved by setting the variables `message-directory` (which
261 defines a base directory) and `notmuch-fcc-dirs` which defines the
262 subdirectory relative to `message-directory` in which to save the
263 mail. Enter a directory (without the maildir `/cur` ending which
264 will be appended automatically). To customize both variables at the
265 same time, use the fancy command:
266
267         M-x customize-apropos<RET>\(notmuch-fcc-dirs\)\|\(message-directory\)
268
269 This mechanism also allows you to select different folders to be
270 used for the outgoing mail depending on your selected `From`
271 address. Please see the documentation for the variable
272 `notmuch-fcc-dirs` in the customization window for how to arrange
273 this.
274
275 ## How to customize `notmuch-saved-searches`
276
277 When starting notmuch, a list of saved searches and message counts is
278 displayed, replacing the older `notmuch-folders` command. The set of
279 saved searches displayed can be modified directly from the notmuch
280 interface (using the `[save]` button next to a previous search) or by
281 customising the variable `notmuch-saved-searches`.
282
283 An example setting might be:
284
285         (setq notmuch-saved-searches '(("inbox" . "tag:inbox")
286                         ("unread" . "tag:inbox AND tag:unread")
287                         ("notmuch" . "tag:inbox AND to:notmuchmail.org")))
288
289 Of course, you can have any number of saved searches, each configured
290 with any supported search terms (see "notmuch help search-terms").
291
292 Some users find it useful to add `and not tag:delete` to those
293 searches, as they use the `delete` tag to mark messages as
294 deleted. This causes messages that are marked as deleted to be removed
295 from the commonly used views of messages.  Use whatever seems most
296 useful to you.
297
298 ## Viewing HTML messages with an external viewer
299
300 The emacs client can display an HTML message inline using either the
301 `html2text` library or some text browser, like w3m or lynx. This is
302 controlled by the `mm-text-html-renderer` variable.
303
304 The first option is theorically better, because it can generate
305 strings formatted for emacs and do whatever you want, e.g., substitute
306 text inside &lt;b&gt; tags for bold text in the buffer. The library, however
307 is still in a very early development phase and cannot yet process
308 properly many elements, like tables and <style> directives, and even
309 the generated text is often poorly formatted.
310
311 Among the available browsers, w3m seems to do a better job converting
312 the html, and if you have the w3m emacs package, you can use it,
313 instead of the w3m-standalone, and thus preserve the text formatting.
314
315 But if the rendering fails for one reason or another, or if you really
316 need to see the graphical presentation of the HTML message, it can be
317 useful to display the message in an external viewer, such as a web
318 browser. Here's a little script that Keith Packard wrote, which he
319 calls `view-html`:
320
321         #!/bin/sh
322         dir=`mktemp -d`
323         trap "rm -r $dir" 0
324         cat "$@" > "$dir"/msg
325         if munpack -C "$dir" -t < "$dir"/msg 2>&1 | grep 'Did not find'; then
326             sed -n '/[Hh][Tt][Mm][Ll]/,$p' "$dir"/msg > $dir/part1.html
327             rm "$dir"/msg
328         fi
329         for i in "$dir"/part*; do
330             if grep -q -i -e '<html>' -e 'text/html' "$i"; then
331                 iceweasel "$i" &
332                 sleep 3
333                 exit 0
334             fi
335         done
336
337 Save that script somewhere in your `${PATH}`, make it executable,
338 and change the invocation of `iceweasel` to any other HTML viewer if
339 necessary. Then within the emacs client, press '|' to pipe the
340 current message, then type "view-html".
341
342 Keith mentions the following caveat, "Note that if iceweasel isn't
343 already running, it seems to shut down when the script exits. I
344 don't know why."
345
346 ## msmtp, message mode and multiple accounts
347
348 As an alternative to running a mail server such as sendmail or postfix
349 just to send email, it is possible to use
350 [msmtp](http://msmtp.sourceforge.net/). This small application will
351 look like `/usr/bin/sendmail` to a MUA such as emacs message mode, but
352 will just forward the email to an external SMTP server. It's fairly
353 easy to set up and it supports several accounts for using different
354 SMTP servers. The msmtp pages have several examples.
355
356 A typical scenario is that you want to use the company SMTP server
357 for email coming from your company email address, and your personal
358 server for personal email.  If msmtp is passed the envelope address
359 on the command line (the -f/--from option) it will automatically
360 pick the matching account.  The only trick here seems to be getting
361 emacs to actually pass the envelope from.  There are a number of
362 overlapping configuration variables that control this, and it's a
363 little confusion, but setting these three works for me:
364
365  - `mail-specify-envelope-from`: `t`
366
367  - `message-sendmail-envelope-from`: `header`
368
369  - `mail-envelope-from`: `header`
370
371 With that in place, you need a `.msmtprc` with the accounts configured
372 for the domains you want to send out using specific SMTP servers and
373 the rest will go to the default account.
374
375 If you have a hard time getting the above to work for you, as I did,
376 it's also possible to add a message-send-mail-hook in your .emacs to
377 send the from header explicitly as an argument to msmtp as described
378 [here](http://www.emacswiki.org/cgi-bin/wiki/GnusMSMTP#toc2) on the
379 emacswiki.
380
381
382 ## <span id="address_completion">Address completion when composing</span>
383
384 There are currently three solutions to this:
385
386 ### bbdb
387
388 [bbdb](http://bbdb.sourceforge.net) is a contact database for emacs
389 that works quite nicely together with message mode, including
390 address autocompletion.
391
392 ### notmuch database as an address book
393
394 You can also use the notmuch database as a mail address book itself.
395 To do this you need a command line tool that outputs likely address
396 candidates based on a search string.  There are currently three
397 available:
398
399   * The python tool `notmuch_address.py` (`git clone
400     http://commonmeasure.org/~jkr/git/notmuch_addresses.git`) (slower, but
401     no compilation required so good for testing the setup)
402
403   * The vala-based
404     [addrlookup](http://github.com/spaetz/vala-notmuch) (faster, but
405     needs compiling).  The addrlookup binary needs to be compiled.
406     Grab
407     `http://github.com/spaetz/vala-notmuch/raw/static-sources/src/addrlookup.c`
408     and build it with:
409
410             cc -o addrlookup addrlookup.c `pkg-config --cflags --libs gobject-2.0` -lnotmuch
411
412   * Shell/fgrep/perl combination [nottoomuch-addresses.sh](http://www.iki.fi/too/nottoomuch/nottoomuch-addresses/). 
413     This tools maintains it's own address "database" gathered from email
414     files notmuch knows and search from that "database" is done by `fgrep(1)`.
415
416 You can perform tab-completion using any of these programs.
417 Just add the following to your .emacs:
418
419         (require 'notmuch-address)
420         (setq notmuch-address-command "/path/to/address_fetching_program")
421         (notmuch-address-message-insinuate)
422
423 ### Google Contacts
424
425 [GooBook](http://code.google.com/p/goobook/) is a command-line tool for
426 accessing Google Contacts. Install and set it up according to its documentation.
427
428 To use GooBook with notmuch, use this wrapper script and set it up like the
429 programs above.
430
431         #!/bin/sh
432         goobook query "$*" | sed 's/\(.*\)\t\(.*\)\t.*/\2 \<\1\>/' | sed '/^$/d'
433
434 You can add the sender of a message to Google Contacts by piping the message
435 (`notmuch-show-pipe-message`) to `goobook add`.
436
437 ## How to sign/encrypt messages with gpg
438
439 Messages can by signed using gpg by invoking 
440 `M-x mml-secure-sign-pgpmime` (or `M-x mml-secure-encrypt-pgpmime`).
441 These functions are available via the standard `message-mode` keybindings 
442 `C-c C-m s p` and `C-c C-m c p`. To sign outgoing mail by default, use the
443 `message-setup-hook` in your `.emacs` file:
444
445         ;; Sign messages by default.
446         (add-hook 'message-setup-hook 'mml-secure-sign-pgpmime)
447
448 This inserts the required `<#part sign=pgpmime>` into the beginning
449 of the mail text body and will be converted into a pgp signature
450 when sending (so one can just manually delete that line if signing
451 is not required).
452
453 Alternatively, you may prefer to use `mml-secure-message-sign-pgpmime` instead
454 of `mml-secure-sign-pgpmime` to sign the whole message instead of just one
455 part.
456
457 ### Troubleshooting message-mode gpg support
458
459 - If you have trouble with expired subkeys, you may have encountered
460   emacs bug #7931.  This is fixed in git commit 301ea744c on
461   2011-02-02.  Note that if you have the Debian package easypg
462   installed, it will shadow the fixed version of easypg included with
463   emacs.
464
465 ## Multiple identities using gnus-alias
466
467 [gnus-alias](http://www.emacswiki.org/emacs/GnusAlias) allows you to
468 define multiple identities when using `message-mode`. You can specify
469 the from address, organization, extra headers (including *Bcc*), extra
470 body text, and signature for each identity. Identities are chosen
471 based on a set of rules. When you are in message mode, you can switch
472 identities using gnus-alias.
473
474 ### Installation
475
476 - put `gnus-alias.el` on your load Emacs-Lisp load path (add new directory
477   to load path by writing `(add-to-list 'load-path "/some/load/path")` into
478   your `.emacs`.
479
480 - Add the following to your `.emacs`
481
482         (autoload 'gnus-alias-determine-identity "gnus-alias" "" t)
483         (add-hook 'message-setup-hook 'gnus-alias-determine-identity)
484
485 Looking into `gnus-alias.el` gives a bit more information...
486
487 ### Example Configuration 
488
489 Here is an example configuration.
490
491         ;; Define two identities, "home" and "work"
492         (setq gnus-alias-identity-alist
493               '(("home"
494                  nil ;; Does not refer to any other identity
495                  "John Doe <jdoe@example.net>" ;; Sender address
496                  nil ;; No organization header
497                  nil ;; No extra headers
498                  nil ;; No extra body text
499                  "~/.signature")
500                 ("work"
501                  nil
502                  "John Doe <john.doe@example.com>"
503                  "Example Corp."
504                  (("Bcc" . "john.doe@example.com"))
505                  nil
506                  "~/.signature.work")))
507         ;; Use "home" identity by default
508         (setq gnus-alias-default-identity "home")
509         ;; Define rules to match work identity
510         (setq gnus-alias-identity-rules
511               '(("work" ("any" "john.doe@\\(example\\.com\\|help\\.example.com\\)" both) "work"))
512         ;; Determine identity when message-mode loads
513         (add-hook 'message-setup-hook 'gnus-alias-determine-identity)
514
515 When `gnus-alias` has been loaded (using autoload, require, *M-x load-library*
516 or *M-x load-file* (load-file takes file path -- therefore it can be used
517 without any `.emacs` changes)) the following commands can be used to get(/set)
518 more information (some of these have "extensive documentation"):
519
520         M-x describe-variable RET gnus-alias-identity-alist
521         M-x describe-variable RET gnus-alias-identity-rules
522         M-x describe-variable RET gnus-alias-default-identity
523
524         M-x customize-group RET gnus-alias RET
525           or 
526         M-x gnus-alias-customize RET
527
528 The last two do the same thing.
529
530 See also the **Usage:** section in `gnus-alias.el`.