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