Sams Teach Yourself Emacs in 24 Hours


Hour 21: Advanced Gnus

Previous HourNext Hour

Sections in this Hour:


Hour 21
Advanced Gnus

Are you ready to see why I constantly hail Gnus as being the most powerful mail and news reader? In this hour you will see some of the really cool things one can do with Gnus. These include

This hour also helps you customize some of the minor things that can be annoying if they don't work the way you want them to. Hey, this is Emacs--almost everything can be customized!

Customizing the Group Buffer

Start with the Group buffer. In this section you will see how you can organize your mail groups into topics, how you can customize the group line to show only the information you are interested in, and how you may make Gnus sort your incoming mail into a set of groups.


After you learn how to subscribe to groups and you find out how easy it is to make Gnus find the messages in a group that are of special interest to you, you will suddenly find that you are subscribed to 20 different newsgroups, and perhaps as many mailing lists.

The problem then is to find the group you want to read messages from. Fortunately, there's a solution in Gnus to this problem-- topic. Topic is like a directory structure where each topic can contain a number of groups and subtopics. Furthermore you can fold topics so their content is hidden. This can be seen in Figure 21.1. (Note that this is from my personal setup, and I've changed the group lines; you will shortly learn how to do that.)

Figure 21.1
Keeping the groups in topics.

All the topic commands are available from the Topics menu item, but before you can do anything with topics, you need to enable topics, which you do by pressing t.

Now you can add new topics by pressing T n. Gnus queries you in the minibuffer for the name of the topic, and inserts it as the last element under the topic, which point currently is within. If this is not the correct location for the topic, you can move it to another location by killing it with C-k and pasting it in at another location with C-y. If you paste a topic at the end of another topic, it will be located at the same level as the other topic. If your intention was that it should have been a subtopic of this other topic, simply press the Tab key, which pushes the topic one level down. This can be seen in Figures 21.2 and 21.3.

Figure 21.2
Using the Tab key--before.

Figure 21.3
Pressing the Tab key brings DEF to the same level as ABC. Pressing Tab once more brings it to a subitem to ABC.

There are a few different ways to move groups around, but the easiest way is simply to kill them with C-k and then paste them in again at another location using C-y. You can kill several groups after each other. The order in which they are pasted in is in the reverse order they have been killed--that is, the last one killed is the first one pasted in.

You can collapse and expand topics by pressing the Enter key when point is on top of them, or by pressing the second mouse button upon them.

Once you have set up topics, you always want to have them enabled, right? You can achieve this by inserting the following into your .Gnus file:

(add-hook 'gnus-group-mode-hook 'gnus-topic-mode)

Additional Servers

Gnus has a few additional servers, called doc-server and virtual-server. There is no need for you to think about these features as servers in general, but you will see them listed as servers in different places in Gnus, so it is convenient for you to know.


The doc-server is used to tell Gnus to read a file as a group. This is convenient if you have an old mailbox that you just want to peek into. To create a doc-group, type G-f. Gnus now asks you for the name of the file and creates a group from this file.

If you want to copy any of the messages from this file to any of your existing groups, then you can press B c. Gnus then asks you for the group to copy this message to. You can also mark a number of messages by pressing # and copy them all in one go.

If, on the other hand, you want the messages handled as if they have just arrived, press B r. This might be of interest if you have set Gnus up to sort your incoming mail into several different groups (this is discussed in the section "Sorting Incoming Mail Using Gnus").

When you are finished with the group, kill it using C-k.

The Virtual Server

Sometimes different newsgroups exist that more or less cover the same topic, or it might at least seem so from your point of view. Examples of this include

The existence of two groups might have the following disadvantages to you:

The solution to this problem is to merge the two groups into one group. This is done using a virtual group.

Creating a Virtual Group Containing comp.emacs and

This task shows you how to create a virtual group with the messages from comp.emacs and Follow these steps:

1. Subscribe to the two groups as usual.

2. Create a virtual group by pressing G V. Gnus now asks for the name of the group. Call it Emacs. Now a virtual group exists, but it does not contain any messages, because no groups are attached to it.

3. Move point to each of the two Emacs groups, and for each of them press G v. Gnus now asks you which virtual group the current group should be attached to. Type the name you gave it previously.

4. Now a virtual group exists. It contains all the messages from both groups, and you will never see that it is not one group, but in fact two. The two original groups are, however, also still visible. To remove them, you must unsubscribe them, but not kill them. That is press u when point is on the original groups.

The same method may also be used to join your outgoing and incoming mail groups together to one, where you can see both the letters you sent and the letters which you received.

There is one caveat with virtual groups, which is that when you want to post a new message to one of the groups that the virtual group contains, you have to go to the original groups and post the message from there. This, however, doesn't apply to followups to the messages in the groups.

Customizing the Group Line

The group lines (those showing a group in the Group buffer), the topic lines, and the summary lines (those showing the messages in the Summary buffer) can all be configured. This way you can make Gnus include just the information you care about. If you, for example, do not want to see the select method (for example, nnfolder:), you can configure the group line not to show this. Likewise you might want to know how many messages a given group contains--that is, not only the number of unread messages (which is the default), but the total number of messages.

The group line is configured using the variable gnus-group-line-format. This variable must have a string assigned to it that shows the information you want. In this string you can include placeholders, each of which are expanded to show information about the group in question. The placeholders are specified with a percent character ( %), and a character specifying which placeholder it is. The default value of the group line can be seen in Figure 21.4.

All the specifics available for this variable can be seen in the description of the variable. That is, press C-h v. (describe-variable) and type gnus-group-line-format. If you do not want to see the select method, but only the name of the group, you can insert the following into your .Gnus file:

(setq Gnus-group-line-format "%M%S%p%P%5y: %(%G%)%l\n")

Figure 21.4
The default value of the group-line- format.variable.

Sorting Incoming Mail Using Gnus

Whenever Gnus finds new mail, it moves it to a location that is private for Gnus. During this mail transferring, Gnus can also sort the mail into several different groups. This is useful if you are subscribed to a number of mailing lists and do not care to use the time to set up an external program to sort mail for you (such as procmail).

The configuration of mail sorting is done with the variable nnmail-split-methods. The following is a configuration where mail is split in three different groups:

(setq nnmail-split-methods '( 
  ("debian-devel" "^To:.*[email protected]") 
  ("postcard" "^Subject:.*Postcard:") 
  ("Miscellaneous" "") 

The previous code is a Lisp expression, but you should be able to figure out how to use it without knowing anything about Lisp. The three lines in the middle are the ones that say which mail should go where. Each line must start and end with a parenthesis. Between the parentheses, two strings limited by quotation marks are listed. The first string names the mail folder that mail should be put in if the regular expression, given as the second string, matches.

The regular expression of the last line should always be the empty string to ensure that there always is at least one regular expression that matches. The letter is always placed in the group associated with the first regular expression that matches the letter. Thus each letter is always located in only one group!

Gnus can in fact do more powerful mail sorting than the previous example indicates, but in case you need more power, I suggest that you use a tool, such as procmail, developed specifically for this task.

Note - If you want to use procmail, it might be a good idea to use The Dotfile Generator to configure the .procmailrc file. This file specifies how procmail should sort the mail. Information about The Dotfile Generator is available from its home page, located at

Just in case you're wondering, I'm the author of The Dotfile Generator and the procmail module for it.

Sams Teach Yourself Emacs in 24 Hours


Hour 21: Advanced Gnus

Previous HourNext Hour

Sections in this Hour: