]> git.cworth.org Git - obsolete/notmuch-wiki/blob - emacstips.mdwn
Show two-level TOC and move it under the page title
[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 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 often display an HTML message inline, but it
178 sometimes fails for one reason or another, (or is perhaps inadequate
179 if you really need to see the graphical presentation of the HTML
180 message).
181
182 In this case, it can be useful to display the message in an external
183 viewer, such as a web browser. Here's a little script that Keith
184 Packard wrote, which he calls `view-html`:
185
186                 #!/bin/sh
187                 dir=`mktemp -d`
188                 trap "rm -r $dir" 0
189                 cat "$@" > "$dir"/msg
190                 if munpack -C "$dir" -t < "$dir"/msg 2>&1 | grep 'Did not find'; then
191                     sed -n '/[Hh][Tt][Mm][Ll]/,$p' "$dir"/msg > $dir/part1.html
192                     rm "$dir"/msg
193                 fi
194                 for i in "$dir"/part*; do
195                     if grep -q -i -e '<html>' -e 'text/html' "$i"; then
196                         iceweasel "$i" &
197                         sleep 3
198                         exit 0
199                     fi
200                 done
201
202 Save that script somewhere in your `${PATH}`, make it executable,
203 and change the invocation of `iceweasel` to any other HTML viewer if
204 necessary. Then within the emacs client, press '|' to pipe the
205 current message, then type "view-html".
206
207 Keith mentions the following caveat, "Note that if iceweasel isn't
208 already running, it seems to shut down when the script exits. I
209 don't know why."
210
211 ## msmtp, message mode and multiple accounts
212
213 As an alternative to running a mail server such as sendmail or
214 postfix just to send email, it is possible to use
215 [msmtp](http://msmtp.sourceforge.net/).  This small application will
216 look like `/usr/bin/sendmail` to a MUA such as emacs message mode, but
217 will just forward the email to an external SMTP server.  It's fairly
218 easy to set up and it support several account for using different
219 SMTP servers.  The msmtp pages have several examples.
220
221 A typical scenario is that you want to use the company SMTP server
222 for email coming from your company email address, and your personal
223 server for personal email.  If msmtp is passed the envelope address
224 on the command line (the -f/--from option) it will automatically
225 pick the matching account.  The only trick here seems to be getting
226 emacs to actually pass the envelope from.  There are a number of
227 overlapping configuration variables that control this, and it's a
228 little confusion, but setting these three works for me:
229
230  - `mail-specify-envelope-from`: `t`
231
232  - `message-sendmail-envelope-from`: `header`
233
234  - `mail-envelope-from`: `header`
235
236 With that in place, you need a `.msmtprc` with the accounts configured
237 for the domains you want to send out using specific SMTP servers and
238 the rest will go to the default account.
239
240 ## <span id="address_completion">Address completion when composing</span>
241
242 There are currently two solutions to this:
243
244 [bbdb](http://bbdb.sourceforge.net) is a contact database for emacs
245 that works quite nicely together with message mode, including
246 address autocompletion.
247
248 You can also use the notmuch database as a mail address book itself.
249 To do this you need a command line tool that outputs likely address
250 candidates based on a search string.  There are currently two
251 available:
252
253   * The python tool `notmuch_address.py` (`git clone
254     http://jkr.acm.jhu.edu/git/notmuch_addresses.git`) (slower, but
255     no compilation required so good for testing the setup)
256
257   * The vala-based
258     [addrlookup](http://github.com/spaetz/vala-notmuch) (faster, but
259     needs compiling).  The addrlookup binary needs to be compiled.
260     Grab
261     `http://github.com/spaetz/vala-notmuch/raw/static-sources/src/addrlookup.c`
262     and build it with:
263
264                     cc -o addrlookup addrlookup.c `pkg-config --cflags --libs gobject-2.0` -lnotmuch
265
266 EUDC is integrated into emacs and is needed for tab completion of
267 email addresses. See [this
268 mail](http://mid.gmane.org/87fx3uflkx.fsf@jhu.edu)
269 (id:87fx3uflkx.fsf@jhu.edu) for more information.
270
271 ## How to sign/encrypt messages with gpg
272
273 Messages can by signed using gpg by invoking `M-x
274 mml-secure-sign-pgpmime` (or `M-x
275 mml-secure-encrypt-pgpmime`). These functions are available via the
276 standard `message-mode` keybindings `C-c C-m s p` and `C-c C-m c
277 p`. To sign outgoing mail by default, use the `message-setup-hook`
278 in your `.emacs` file:
279
280                     ;; Sign messages by default.
281                     (add-hook 'message-setup-hook 'mml-secure-sign-pgpmime)
282
283 This inserts the required `<#part sign=pgpmime>` into the beginning
284 of the mail text body and will be converted into a pgp signature
285 when sending (so one can just manually delete that line if signing
286 is not required).
287
288 Alternatively, you may prefer to use `mml-secure-message-sign-pgpmime` instead
289 of `mml-secure-sign-pgpmime` to sign the whole message instead of just one
290 part.