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