]> git.cworth.org Git - notmuch-wiki/blob - emacstips.mdwn
emacstips: how to change reply to all/sender key bindings
[notmuch-wiki] / emacstips.mdwn
1 [[!img notmuch-logo.png alt="Notmuch logo" class="left"]]
2 #Tips and Tricks for using notmuch with Emacs
3
4 [[!toc levels=2]]
5
6 The main Notmuch message reading client is **notmuch.el**, which is an
7 [emacs](http://www.gnu.org/software/emacs/) major mode, and is
8 included in the notmuch package.
9
10 ## Setup
11
12 To use the Notmuch emacs mode, first add the following line to your
13 `.emacs` rc file:
14
15         (require 'notmuch)
16
17 or you can load the package via autoload:
18
19         (autoload 'notmuch "notmuch" "notmuch mail" t)
20
21 Then, either run "emacs -f notmuch", or execute the command `M-x
22 notmuch` from within a running emacs.
23
24 ## Navigating & reading mails
25
26 When first starting notmuch in emacs, you will be presented with the
27 notmuch "hello" page.  From here you can do searches, see lists of
28 recent searches, saved searches, message tags, help information, etc.
29
30 Executing a search will open a new buffer in `notmuch-search-mode`
31 displaying the search results.  Each line in the search results
32 represents a message thread.  Hitting the '?' key will show help for
33 this mode.
34
35 In general, the 'q' will kill the current notmuch buffer and return
36 you to the previous buffer (sort of like a 'pop').
37
38 In search mode, navigating to a thread and hitting return will then
39 open a new buffer in `notmuch-show-mode`, which will show the actual
40 message contents of the thread.
41
42 ## Sending mail
43
44 In any notmuch mode, you can start a new message by hitting the 'm'
45 key.  To reply to a message or thread, just hit the 'r' key.
46
47 When composing new messages, you will be entered in emacs's
48 `message-mode`, which is a powerful mode for composing and sending
49 messages.  When in message mode, you can type `C-c ?` for help.
50
51 If you would like to use address autocompletion when composing
52 messages, see [address completion](#address_completion).
53
54 When you are ready to send a message, type `C-c C-c`. By default
55 message mode will use your sendmail command to send mail, so make sure
56 that works. One annoying standard configuration of message mode is
57 that it will hide the sent mail in your emacs frame stack, but it will
58 not close it. If you type several mails in an emacs session they will
59 accumulate and make switching between buffers more annoying. You can
60 avoid that behavior by adding `(setq message-kill-buffer-on-exit t)`
61 in your `.emacs` file (or doing `M-x
62 customize-variable<RET>message-kill-buffer-on-exit<RET>`) which will
63 really close the mail window after sending it.
64
65 ## Attaching files
66
67 Using the `M-x mml-attach-file` command, you can attach any file to be
68 sent with your mail. By default this command is bound to the menu item
69 *Attachments--Attach File* with the key binding `C-c C-a`. The
70 variable `mml-dnd-attach-options` (`M-x
71 customize-variable<RET>mml-dnd-attach-options<RET>`) can be set to
72 allow the prompting for various attachment options (such as
73 inline/attachment) if you want to do that.
74
75 For those who prefer a more graphical interface, you can also simply
76 drag and drop files from a file manager into a mail composition window
77 to have them attached. In Ubuntu this works without any modifications
78 if files are dragged from the file manager.
79
80 And for those who prefer working from command line, the following
81 script opens new emacs window with empty message and attaches files
82 mentioned as script arguments. (Note: The script expects that you have
83 `(server-start)` in your `.emacs` file.)
84
85     #!/bin/sh
86     attach_cmds=""
87     while [ "$1" ]; do
88         fullpath=$(readlink --canonicalize $1)
89         attach_cmds="$attach_cmds (mml-attach-file \"$fullpath\")"
90         shift
91     done
92     emacsclient -a '' -c -e "(progn (compose-mail) $attach_cmds)"
93
94
95 -----
96
97 # Advanced tips and tweaks
98
99 ## Use separate emacs lisp file for notmuch configuration
100
101 Instead of adding notmuch configuration code to `.emacs`, there
102 is an option to collect those to a separate file (which is only
103 loaded when `notmuch` is invoked). To do this, write, for example
104 a file called `~/.emacs.d/my-notmuch.el`:
105
106           ;; my-notmuch.el -- my notmuch mail configuration
107           ;;
108           
109           ;; add here stuff required to be configured *before*
110           ;; notmuch is loaded;
111
112           ; uncomment and modify in case some elisp files are not found in load-path
113           ; (add-to-list 'load-path "~/vc/ext/notmuch/emacs")
114
115           ;; load notmuch 
116           (require 'notmuch)
117
118           ;; add here stuff required to be configured *after*
119           ;; notmuch is loaded;
120                 
121           ;(setq user-mail-address (notmuch-user-primary-email)
122           ;      user-full-name (notmuch-user-name))
123
124           ; uncomment & modify if you want to use external smtp server to send mail
125           ; (setq smtpmail-smtp-server "smtp.server.tld"
126           ;       message-send-mail-function 'message-smtpmail-send-it)
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                 (define-key notmuch-show-mode-map "S"
141                   (lambda ()
142                     "mark message as spam"
143                     (interactive)
144                     (notmuch-show-add-tag "spam")
145                     (notmuch-show-remove-tag "inbox")))
146
147 You can do the same for threads in `notmuch-search-mode` by just
148 replacing "show" with "search" in the called functions.
149
150 The definition above makes use of a lambda function, but you could
151 also define a separate function first:
152
153                 (defun notmuch-show-tag-spam()
154                   "mark message as spam"
155                   (interactive)
156                   (notmuch-show-add-tag "spam")
157                   (notmuch-show-remove-tag "inbox")))
158                 (define-key notmuch-show-mode-map "S" 'notmuch-show-tag-spam)
159
160 Here's a more complicated example of how to add a toggle "deleted"
161 key:
162
163                 (define-key notmuch-show-mode-map "d"
164                   (lambda ()
165                     "toggle deleted tag for message"
166                     (interactive)
167                     (if (member "deleted" (notmuch-show-get-tags))
168                         (notmuch-show-remove-tag "deleted")
169                       (notmuch-show-add-tag "deleted"))))
170
171 ## Restore reply-to-all key binding to 'r'
172
173 Starting from notmuch 0.12 the 'r' key is bound to reply-to-sender instead of
174 reply-to-all. Here's how to swap the reply to sender/all bindings in show mode:
175
176                 (define-key notmuch-show-mode-map "r" 'notmuch-show-reply)
177                 (define-key notmuch-show-mode-map "R" 'notmuch-show-reply-sender)
178
179 And in search mode:
180
181                 (define-key notmuch-search-mode-map "r" 'notmuch-search-reply-to-thread)
182                 (define-key notmuch-search-mode-map "R" 'notmuch-search-reply-to-thread-sender)
183
184
185 ## How to do FCC/BCC...
186
187 The Emacs interface to notmuch will automatically add an `Fcc`
188 header to your outgoing mail so that any messages you send will also
189 be saved in your mail store. You can control where this copy of the
190 message is saved by setting the variables `message-directory` (which
191 defines a base directory) and `notmuch-fcc-dirs` which defines the
192 subdirectory relative to `message-directory` in which to save the
193 mail. Enter a directory (without the maildir `/cur` ending which
194 will be appended automatically). To customize both variables at the
195 same time, use the fancy command:
196
197                 M-x customize-apropos<RET>\(notmuch-fcc-dirs\)\|\(message-directory\)
198
199 This mechanism also allows you to select different folders to be
200 used for the outgoing mail depending on your selected `From`
201 address. Please see the documentation for the variable
202 `notmuch-fcc-dirs` in the customization window for how to arrange
203 this.
204
205 ## How to customize `notmuch-saved-searches`
206
207 When starting notmuch, a list of saved searches and message counts is
208 displayed, replacing the older `notmuch-folders` command. The set of
209 saved searches displayed can be modified directly from the notmuch
210 interface (using the `[save]` button next to a previous search) or by
211 customising the variable `notmuch-saved-searches`.
212
213 An example setting might be:
214
215                 (setq notmuch-saved-searches '(("inbox" . "tag:inbox")
216                                 ("unread" . "tag:inbox AND tag:unread")
217                                 ("notmuch" . "tag:inbox AND to:notmuchmail.org")))
218
219 Of course, you can have any number of saved searches, each configured
220 with any supported search terms (see "notmuch help search-terms").
221
222 Some users find it useful to add `and not tag:delete` to those
223 searches, as they use the `delete` tag to mark messages as
224 deleted. This causes messages that are marked as deleted to be removed
225 from the commonly used views of messages.  Use whatever seems most
226 useful to you.
227
228 ## Viewing HTML messages with an external viewer
229
230 The emacs client can display an HTML message inline using either the
231 `html2text` library or some text browser, like w3m or lynx. This is
232 controlled by the `mm-text-html-renderer` variable.
233
234 The first option is theorically better, because it can generate
235 strings formatted for emacs and do whatever you want, e.g., substitute
236 text inside &lt;b&gt; tags for bold text in the buffer. The library, however
237 is still in a very early development phase and cannot yet process
238 properly many elements, like tables and <style> directives, and even
239 the generated text is often poorly formatted.
240
241 Among the available browsers, w3m seems to do a better job converting
242 the html, and if you have the w3m emacs package, you can use it,
243 instead of the w3m-standalone, and thus preserve the text formatting.
244
245 But if the rendering fails for one reason or another, or if you really
246 need to see the graphical presentation of the HTML message, it can be
247 useful to display the message in an external viewer, such as a web
248 browser. Here's a little script that Keith Packard wrote, which he
249 calls `view-html`:
250
251                 #!/bin/sh
252                 dir=`mktemp -d`
253                 trap "rm -r $dir" 0
254                 cat "$@" > "$dir"/msg
255                 if munpack -C "$dir" -t < "$dir"/msg 2>&1 | grep 'Did not find'; then
256                     sed -n '/[Hh][Tt][Mm][Ll]/,$p' "$dir"/msg > $dir/part1.html
257                     rm "$dir"/msg
258                 fi
259                 for i in "$dir"/part*; do
260                     if grep -q -i -e '<html>' -e 'text/html' "$i"; then
261                         iceweasel "$i" &
262                         sleep 3
263                         exit 0
264                     fi
265                 done
266
267 Save that script somewhere in your `${PATH}`, make it executable,
268 and change the invocation of `iceweasel` to any other HTML viewer if
269 necessary. Then within the emacs client, press '|' to pipe the
270 current message, then type "view-html".
271
272 Keith mentions the following caveat, "Note that if iceweasel isn't
273 already running, it seems to shut down when the script exits. I
274 don't know why."
275
276 ## msmtp, message mode and multiple accounts
277
278 As an alternative to running a mail server such as sendmail or postfix
279 just to send email, it is possible to use
280 [msmtp](http://msmtp.sourceforge.net/). This small application will
281 look like `/usr/bin/sendmail` to a MUA such as emacs message mode, but
282 will just forward the email to an external SMTP server. It's fairly
283 easy to set up and it supports several accounts for using different
284 SMTP servers. The msmtp pages have several examples.
285
286 A typical scenario is that you want to use the company SMTP server
287 for email coming from your company email address, and your personal
288 server for personal email.  If msmtp is passed the envelope address
289 on the command line (the -f/--from option) it will automatically
290 pick the matching account.  The only trick here seems to be getting
291 emacs to actually pass the envelope from.  There are a number of
292 overlapping configuration variables that control this, and it's a
293 little confusion, but setting these three works for me:
294
295  - `mail-specify-envelope-from`: `t`
296
297  - `message-sendmail-envelope-from`: `header`
298
299  - `mail-envelope-from`: `header`
300
301 With that in place, you need a `.msmtprc` with the accounts configured
302 for the domains you want to send out using specific SMTP servers and
303 the rest will go to the default account.
304
305 If you have a hard time getting the above to work for you, as I did,
306 it's also possible to add a message-send-mail-hook in your .emacs to
307 send the from header explicitly as an argument to msmtp as described
308 [here](http://www.emacswiki.org/cgi-bin/wiki/GnusMSMTP#toc2) on the
309 emacswiki.
310
311
312 ## <span id="address_completion">Address completion when composing</span>
313
314 There are currently two solutions to this:
315
316 [bbdb](http://bbdb.sourceforge.net) is a contact database for emacs
317 that works quite nicely together with message mode, including
318 address autocompletion.
319
320 You can also use the notmuch database as a mail address book itself.
321 To do this you need a command line tool that outputs likely address
322 candidates based on a search string.  There are currently three
323 available:
324
325   * The python tool `notmuch_address.py` (`git clone
326     http://commonmeasure.org/~jkr/git/notmuch_addresses.git`) (slower, but
327     no compilation required so good for testing the setup)
328
329   * The vala-based
330     [addrlookup](http://github.com/spaetz/vala-notmuch) (faster, but
331     needs compiling).  The addrlookup binary needs to be compiled.
332     Grab
333     `http://github.com/spaetz/vala-notmuch/raw/static-sources/src/addrlookup.c`
334     and build it with:
335
336                     cc -o addrlookup addrlookup.c `pkg-config --cflags --libs gobject-2.0` -lnotmuch
337
338   * Shell/fgrep/perl combination [nottoomuch-addresses.sh](http://www.iki.fi/too/nottoomuch/nottoomuch-addresses/). 
339     This tools maintains it's own address "database" gathered from email
340     files notmuch knows and search from that "database" is done by fgrep(1).
341
342 You can perform tab-completion using any of these programs. Just add the following to your .emacs:
343
344     (require 'notmuch-address)
345     (setq notmuch-address-command "/path/to/address_fetching_program")
346     (notmuch-address-message-insinuate)
347
348
349 ## How to sign/encrypt messages with gpg
350
351 Messages can by signed using gpg by invoking `M-x
352 mml-secure-sign-pgpmime` (or `M-x
353 mml-secure-encrypt-pgpmime`). These functions are available via the
354 standard `message-mode` keybindings `C-c C-m s p` and `C-c C-m c
355 p`. To sign outgoing mail by default, use the `message-setup-hook`
356 in your `.emacs` file:
357
358                     ;; Sign messages by default.
359                     (add-hook 'message-setup-hook 'mml-secure-sign-pgpmime)
360
361 This inserts the required `<#part sign=pgpmime>` into the beginning
362 of the mail text body and will be converted into a pgp signature
363 when sending (so one can just manually delete that line if signing
364 is not required).
365
366 Alternatively, you may prefer to use `mml-secure-message-sign-pgpmime` instead
367 of `mml-secure-sign-pgpmime` to sign the whole message instead of just one
368 part.
369
370 ### Troubleshooting message-mode gpg support
371
372 - If you have trouble with expired subkeys, you may have encounted
373   emacs bug #7931.  This is fixed in git commit 301ea744c on
374   2011-02-02.  Note that if you have the Debian package easypg
375   installed, it will shadow the fixed version of easypg included with
376   emacs.