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