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