Version: 20150526
Updated: <2015-05-26 Tue>
Created: <2012-10-12 Mon>
Copyright: This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Gnus is a nice email client.
People think it is hard because they get lost in its features. It would be much easier if they learned the essential 5% features and ignore the remaining 95%.
So here is my guide on that 5%.
- Gnus is solid
- Less hardware resource required
- Mail search and filter is great
- Bundled with Emacs, no extra installation needed
- Usable when offline. Check the “offline” part in the section “Advanced tips”
- powerful combined with other plugins like yasnippet
Some conventions:
- “C” means “Ctrl” and “M” means “Alt”. For example, “M-x” means pressing “Alt” and “X” together.
- “M-x mycommand” means pressing “Alt” and “X”, input “mycommand”, and press “Enter” key.
- “RET” means “Enter” key.
- “Group” means “mail folder”.
- “Group buffer” means the list of folders.
- “Summary buffer” means the list of mails.
Now please check http://www.emacswiki.org/emacs/GnusGmail on basic setup. You can also use my settings at the end of this article.
After setup, run command “M-x gnus” in Emacs.
You will come to the “Group Buffer” window. The “Group Buffer” lists the mail folders. In Gnus, the folder is called “Group”. By default, the folders are invisible!
Please subscribe the groups to make it visible. For example, folder “Inbox” is named “nnimap+gmail:INBOX” in “Group Buffer” and it’s invisible at the beginning!. I admit it’s confusing.
Check section “Subscribe groups” for details.
After subscribing the group INBOX, the INBOX is still invisible if INBOX contains only read mails. That makes no sense for a modern email client!
There is a simple solution. `C-u 5 gnus-group-list-all-groups` will get desired result. I assigned hotkey “o” to it:
(defun my-gnus-group-list-subscribed-groups ()
"List all subscribed groups with or without un-read messages"
(interactive)
(gnus-group-list-all-groups 5)
)
(define-key gnus-group-mode-map
;; list all the subscribed groups even they contain zero un-read messages
(kbd "o") 'my-gnus-group-list-subscribed-groups)
In the “Group Buffer”, you can enter a folder by pressing “RET”. But I strongly suggest “C-u RET” because your true intention is to see ALL the mails, NOT the mails filtered by “smart” Gnus.
After entering the folder, you reach the “Summary Buffer” which is, as I said before, the list of your mails in one folder.
Now it’s simple, press “RET” to see the mail. Press “q” to quit “Summary Buffer”.
In summary, “Group Buffer” is the list of mail folders and “Summary Buffer” is the list of mails.
That’s all you need to know. If you are impatient, check the section My Gnus configuration (for Gmail).
Don’t memorize all the key bindings. Only pick up what you actually use.
“G G” or “M-x gnus-group-make-nnir-group” to search mails at server side in “Group Buffer”.
You can use “#” to mark the groups where to search mails(BTW, use “M-#” to unmark).
If you don’t mark the groups, the curent group under cursor is searched.
If you put the cursor above any group, all the groups will be searched.
If you want to search certain fields, press “C-u G G” or “C-u M-x gnus-group-make-nnir-group” instead. This is a very useful technique.
If you want to use more advanced syntax,
- Press “C-u G G” or “C-u M-x gnus-group-make-nnir-group”
- Input query statements, press Enter
- Input “imap”, press enter
”/ /” to limit the mails by subject at “Summary Buffer”. “Limiting” means filtering mails locally.
”/ a” to limit the mails by author at “Summary Buffer”.
”/ w” to cancel the current filter.
You can apply the limits sequentially and cancel them one by one using “/ w”
I love this “Limiting” thing. http://www.gnu.org/software/emacs/manual/html_mono/gnus.html#Limiting has more tricks.
See http://sachachua.com/blog/2008/05/emacs-gnus-searching-mail/ for details.
“A A” in “Group Buffer” to list all groups on ALL the connected server. It take a while. I suggest pressing “L” to avoid querying data from all the servers unless the first time to access server.
After getting the list of all subscribed/unsubscribed groups, press “u” to subscribe/unsubscribe specific group.
In order to see all the mails in “INBOX” folder/group, you need MANUALLY subscribe the group “INBOX”!
“l” to navigate back the default view of group buffer which is kind of confusing, as I mentioned before. You’d better never use this hotkey.
Pressing “o” is much better if you uses my emacs-lisp code to show all the subscribed groups.
“g” to refresh groups list.
“R” to reply with quoted text. “r” to reply without quoted text.
“S W” (press S then press W) to reply all (It’s named “wide reply” in Emacs) with quoted text. “S w” to reply all without quoted.
The hotkey is “a” or “m” in “Summary Buffer”.
BTW, you could “C-x m” anywhere in Emacs to write a new mail without bugging Gnus.
The hotkey is “C-c C-a”.
The attached file is some simple text embedded in mail body. You can copy and modified that text.
The hotkey is “o”. See ”Using Mime” in Emacs manual for details.
“C-c C-c”
hotkey “/ N” or command “M-x gnus-summary-insert-new-articles”
“C-u RET” on the selected group in “Group Buffer”, or “C-u M-g” in “Summary Buffer”.
That’s the most important part of this article! Gnus only displays unread mails.
Check http://stackoverflow.com/questions/4982831/i-dont-want-to-expire-mail-in-gnus for the details.
“C-c C-f” or “M-x gnus-summary-mail-forward” in “Summary Buffer”.
BTW, you can mark several mails you want to forward (hotkey is “#”) and then forward them all at once (Holger Schauer provided this tip).
After the forwarded email is created, you may copy the body of that email without sending it. The copied content could be inserted into any other email.
Press “c” either in summary buffer or group buffer. This is my most frequently used command! Gmail’s own key binding is hard to use.
It’s 100% usable if you install Cygwin at first.
Gnus from Cygwin version of Emacs works out of the box.
Native Emacs for Windows need a little bit setup:
- Right-click “My Computer” and go to Properties->Advanced->Environmental Variables
- set up user variables is good enough. You DON’T need be a Administrator to do the setup.
- set the variable “HOME” to the parent directory of your “.emacs.d”.
- set the variable “PATH” to the “C:/cygwin/bin” because Gnus use the executable “openssh.exe”. I supposed you install cygwin at C driver.
Exchange/Outlook users could visit http://blog.binchen.org/posts/how-to-get-email-from-exchange-server-without-outlook.html instead.
Hint, all you need is one portable program Davmail. That’s enough in 99% companies. But if your Adminstrator is evil, you may need EWSEditor for once.
Install BBDB through melpa. It is a database to store email address.
Then insert below code into ~/.emacs,
(add-hook 'message-mode-hook
'(lambda ()
(bbdb-initialize 'message)
(bbdb-initialize 'gnus)
(local-set-key "<TAB>" 'bbdb-complete-name)))
company-mode does the similar job.
I use both “bbdb-complete-name” and company-mode.
Here are the steps,
- Go to https://www.google.com/contacts/
- Click “More -> Export -> vCard Format -> Export”.
- Install https://github.com/redguardtoo/gmail2bbdb and run its command “gmail2bbdb-import-file”. The contacts will be output into ~/.bbdb
There are alternatives. But they are dependent on certain version of BBDB.
gmail2bbdb has no dependency. It always works out of the box.
I want to change “From” according to the computers I’m using automatically.
The code to be inserted into ~/.emacs:
;; (getenv "HOSTNAME") won't work because $HOSTNAME is not an env variable
;; (system-name) won't work because my /etc/hosts has some weird setup in office
(setq my-hostname (with-temp-buffer
(shell-command "hostname" t)
(goto-char (point-max))
(delete-char -1)
(buffer-string)))
(defun at-office ()
(interactive)
(and (string= my-hostname "my-sydney-workpc")
(not (or (string= my-hostname "homepc")
(string= my-hostname "eee")
))))
(setq user-full-name "My Name"
user-mail-address (if (at-office) "[email protected]" "[email protected]")
)
Key points:
- Command line program “hostname” is better than Emacs function `(system-name)`
- I works on several computers which does not belong to me, so I cannot change /etc/hosts which `(system-name)` try to access
- Please verify your email address at Gmail if you use google’s SMTP server
Popfile. A million times better than Gmail’s own filter.
You may think Google’s Inbox equals to Popfile. Trust me, it’s not. You only need Popfile!
Popfile is open sourced. The data is stored locally with my full control. So Popfile still works if I switch the mail server.
For example, after being trained by your Gmail data, Popfile becomes smarter. You can use the smarter Popfile to classify mails from your office’s Exchange Mail Server.
I’m 100% sure this solution will work because I’ve been using it for a very long time. I take pity on my colleagues who know only Outlooks.
Check http://blog.binchen.org/posts/use-popfile-at-linux.html for details.
Then insert below code into ~/.emacs,
(setq mm-text-html-renderer 'w3m)
Go to the “Summary Buffer”.
You need mark the mails by “M-x gnus-summary-tick-article-forward”.
The marked mails enter the disk cache. So they can be read offline.
You may remove it from the disk cache by “M-x gnus-summary-put-mark-as-read”.
You also need insert below code into ~/.emacs,
(setq gnus-use-cache t)
We set gnus-use-cache to true to use the cache to the full extent without considering saving the disk space.
BTW, the disk cache is actually the directory “~/News/cache/”. Back it up with Github’s private repository.
You only need copy the code containing “gnus-secondary-select-methods” from my setup.
Here is a sample setup,
(add-to-list 'gnus-secondary-select-methods
'(nnimap "gmail-second"
(nnimap-address "imap.gmail.com")
(nnimap-server-port 993)
(nnimap-stream ssl)
(nnir-search-engine imap)
(nnimap-authinfo-file "~/.authinfo-second.gpg")
; @see http://www.gnu.org/software/emacs/manual/html_node/gnus/Expiring-Mail.html
;; press 'E' to expire email
(nnmail-expiry-target "nnimap+gmail:[Gmail]/Trash")
(nnmail-expiry-wait 90)
))
“gnus-secondary-select-methods” is the list of your email accounts.
Gnus counts by individual mail. Gmail count by mail thread.
So Gnus can track all your sent mails. Since Gmail backs up sent mails in “[Gmail]/Sent Mail” folder, subscribe it in Gnus.
“M-x gnus-group-enter-server-mode” to go into server list.
Move the cursor to “offline” server then “M-x gnus-server-open-server”.
The content of ~/.gnus.el,
(require 'nnir)
;;@see http://www.emacswiki.org/emacs/GnusGmail#toc1
(setq gnus-select-method '(nntp "news.gmane.org")) ;; if you read news groups
;; ask encyption password once
(setq epa-file-cache-passphrase-for-symmetric-encryption t)
(setq smtpmail-auth-credentials "~/.authinfo.gpg")
;;@see http://gnus.org/manual/gnus_397.html
(add-to-list 'gnus-secondary-select-methods
'(nnimap "gmail"
(nnimap-address "imap.gmail.com")
(nnimap-server-port 993)
(nnimap-stream ssl)
(nnir-search-engine imap)
(nnimap-authinfo-file "~/.authinfo.gpg")
; @see http://www.gnu.org/software/emacs/manual/html_node/gnus/Expiring-Mail.html
;; press 'E' to expire email
(nnmail-expiry-target "nnimap+gmail:[Gmail]/Trash")
(nnmail-expiry-wait 90)))
(setq gnus-thread-sort-functions
'((not gnus-thread-sort-by-date)
(not gnus-thread-sort-by-number)))
; NO 'passive
(setq gnus-use-cache t)
;; BBDB: Address list
(add-to-list 'load-path "/where/you/place/bbdb/")
(require 'bbdb)
(bbdb-initialize 'message 'gnus 'sendmail)
(setq bbdb-file "~/.bbdb") ;; OPTIONAL, because I'm sharing my ~/.emacs.d
(add-hook 'gnus-startup-hook 'bbdb-insinuate-gnus)
(setq bbdb/mail-auto-create-p t
bbdb/news-auto-create-p t)
;; auto-complete emacs address using bbdb's own UI
(add-hook 'message-mode-hook
'(lambda ()
(flyspell-mode t)
(local-set-key "<TAB>" 'bbdb-complete-name)))
;; Fetch only part of the article if we can. I saw this in someone
;; else's .gnus
(setq gnus-read-active-file 'some)
;; Tree view for groups. I like the organisational feel this has.
(add-hook 'gnus-group-mode-hook 'gnus-topic-mode)
;; Threads! I hate reading un-threaded email -- especially mailing
;; lists. This helps a ton!
(setq gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject)
;; Also, I prefer to see only the top level message. If a message has
;; several replies or is part of a thread, only show the first
;; message. 'gnus-thread-ignore-subject' will ignore the subject and
;; look at 'In-Reply-To:' and 'References:' headers.
(setq gnus-thread-hide-subtree t)
(setq gnus-thread-ignore-subject t)
;; Personal Information
(setq user-full-name "My Name"
user-mail-address "[email protected]")
;; You need install the command line brower 'w3m' and Emacs plugin 'w3m'
(setq mm-text-html-renderer 'w3m)
(setq message-send-mail-function 'smtpmail-send-it
smtpmail-starttls-credentials '(("smtp.gmail.com" 587 nil nil))
smtpmail-auth-credentials '(("smtp.gmail.com" 587 "[email protected]" nil))
smtpmail-default-smtp-server "smtp.gmail.com"
smtpmail-smtp-server "smtp.gmail.com"
smtpmail-smtp-service 587
smtpmail-local-domain "homepc")
;; http://www.gnu.org/software/emacs/manual/html_node/gnus/_005b9_002e2_005d.html
(setq gnus-use-correct-string-widths nil)
The content of ~/.authinfo.gpg,
machine imap.gmail.com login [email protected] password my-secret-password port 993
machine smtp.gmail.com login [email protected] password my-secret-password port 587
Please note “.authinfo.gpg” is encrypted. You must use Emacs to open it. Emacs will do the encryption/descryption automatically. See http://emacswiki.org/emacs/EasyPG for details.