Sams Teach Yourself Emacs in 24 Hours


Hour 12: Visible Editing Utilities

Previous HourNext Hour

Sections in this Hour:


Hour 12
Visible Editing Utilities

There are several ways to use visible means in Emacs. This includes changing the font, blinking the screen instead of ringing the bell, highlighting matching parentheses, and displaying part of a text in a special font or color to indicate syntax. All of these are described in this hour.

Changing the Font in Emacs

If the font or the font size in your Emacs is too small, too big, or simply too ugly for you or your monitor, this can make your interaction with Emacs harder. Fortunately it is quite easy for you to change these things. Unfortunately, this is done differently in UNIX and in Windows and differently in GNU Emacs and in XEmacs.

There are two ways to change the font:

Changing the Font for the Current Session in GNU Emacs in UNIX

To change the font for the current session in GNU Emacs, simply press Shift and the first mouse button. This brings up a menu with fonts as can be seen in Figure 12.1.

Figure 12.1
Choosing a font in GNU Emacs under UNIX.

This list of fonts is very limited, because the fonts are hard-coded into Emacs and must, therefore, be valid in every Emacs (that is, the lowest common denominator).

To get a list which is valid for your current X setup, you should copy the library called xf.el from the CD-ROM to your load path, and insert the following lines into your .emacs file:

(load "xf") 

Tip - The header of xf.el offers instructions on how to cache the font list that this function extracts. This limits the time taken by this library at startup. I don't, however, think this matters that much nowadays when computers are much more powerful than they were in 1994, when this library was developed. Furthermore, it has the drawback that the list is limited to one X setup (that is, one computer).

Changing the Font for the Current Session in XEmacs in UNIX

To choose a different font in XEmacs, you must select the Options menu. In this menu, you will find three items related to the font:

I believe you will seldom have to edit any of these options. In Figure 12.2, you can see the XEmacs font selection.

Figure 12.2
Choosing a font in XEmacs.

Changing the Font for the Current Session in GNU Emacs in Windows

Font selection in GNU Emacs under Windows is much like font selection in GNU Emacs under UNIX, with the exception that a Windows-like font window appears from which you can choose a monotype (fixed-width) font (see Figure 12.3).

Figure 12.3
Choosing a font in GNU Emacs under Windows.

Changing the Font Once and for All Under UNIX

In UNIX applications, defaults are configured in the file called $HOME/.Xdefaults or something similar (it depends on your UNIX system). This file contains a database of user preferences for the programs running in X Window. For a description of this file see the manual page for the program called xrdb.

Inserting the following line into this file changes the font in Emacs:


Caution - Emacs must be written with a capital E for this to work in XEmacs.

Setting the Default Font Under UNIX

This task shows you how to set the default font under UNIX to the font currently selected.

1. Depending on your Emacs, choose one of the previously described methods to set the font of your Emacs. For this method you must have already changed the font in this Emacs session (this seems to be a bug in Emacs).

2. Open the $HOME/.Xdefaults file, and go to an appropriate location in it. If you do not know which location is appropriate, go to the end of the file.

3. Press M-x and type sams-insert-font-setup. This inserts two lines similar to the following:


The first line sets your default font to the one you have loaded right now, whereas the second sets the default size of your Emacs to the size of your current frame. If you do not want the default size set, just remove the line.

4. Save the buffer. (Gee, that was a surprise, huh?)

5. If you want to test it out at once, go to a shell, type xrdb -merge $HOME/.Xdefaults . and start Emacs.

Tip - If you change the name Emacs in the two lines to something else, say book, you can start Emacs with a -name option, which makes Emacs read these options instead of the default you might have specified elsewhere. Thus if you start Emacs as

Emacs -name book

the options specified with book.font and book.geometry are read instead of those specified with Emacs.font and Emacs.geometry.

Changing the Font Once and for All Under Windows

First, enter the following code into a buffer for evaluation. If you want to keep it permanently, add it to your .emacs file.

(if (eq window-system 'w32)        ; emacs 20 & up 
    (defun insert-x-style-font() 
      "Insert a string in the X format which describes a font the  
user can select from the Windows font selector." 
      (insert (prin1-to-string (w32-select-font))))) 

Before you can use this function, you must evaluate it. Put the cursor after the last parenthesis, and type C-x C-e.

To use the function, put the cursor where you would like to place the name of the font you select, and call this function by name: insert-x-style-font. Create an empty buffer named foo with C-x b foo RET. Now execute M-x insert-x-style-font . The function inserts an X-style string description into the buffer, surrounded with quotes.

Any variable that the UNIX user can set with the Xdefaults file is one that the NT Emacs user can set in the Registry. Create a file in your home directory, called emacs.reg. You can call it what you want, but the .reg extension is associated with the registry, and you'll learn how to use that in a minute. In the file, enter the following:


With the cursor still after the equals sign, execute M-x insert-x-style-font. You will see the font string show up in the buffer, like so:

"EMACS.FONT"="-*-Courier New-normal-r-*-*-16-120-*-*-c-*-*-ansi-" 

This produces 12-point Courier New in your Emacs. To make it effective, close Emacs down entirely. Then execute emacs.reg, either from an MS-DOS window or by clicking it. After clicking OK in the dialog box, start Emacs up again. You should have 12-point Courier New in your Emacs frame.

As you read further in this hour, add variables to your emacs.reg file or edit existing ones, and execute it again. You will have to shut Emacs down and restart it to see your changes.

One thing you can't do to the Registry with a .reg file is delete a key. For that you have to use regedit.exe to delete the unwanted key. Alternatively, set the key to the default value in your emacs.reg.

Sams Teach Yourself Emacs in 24 Hours


Hour 12: Visible Editing Utilities

Previous HourNext Hour

Sections in this Hour: