Editing Files


Emacs is a general-purpose, screen-oriented text editor that has been around for many years and exists, in various manifestations, on OpenVMS clusters, on most of the Sun workstation groups, and on various microcomputers, including the Apple Macintoshs, Amigas, and MS-DOS compatibles. Some of the more common versions of Emacs that you'll find at the Institute include UniPress Emacs, GNU Emacs, and MicroEmacs.

Each implementation of Emacs works basically the same but with some differences. The editor is easy to customize, but you may wish to resist the temptation for portability's sake.

Commands typically have names like "split-current-window," but the most common commands are bound to a sequence of keystrokes, which typically include the and keys. The set of bound commands can be modified and built-in commands extended using macros.

When working with Emacs, each file has an associated buffer so that when you switch documents your editing session is retained. Each window has an associated mode line containing the name of the file being edited in that window.

Setting Up Each Version

Before you can use Emacs, the symbol emacs must be defined in your LOGIN.COM file. To use Gnu Emacs, add the following line to your LOGIN.COM file:

This line will define the symbol EMACS and the logical names that Gnu Emacs uses. To use MicroEmacs, add the following line to your LOGIN.COM file:

The symbols for Unipress Emacs are defined in the system login file, so you do not have to change your LOGIN.COM file if you want to use Unipress Emacs. To edit a file with Emacs type EMACS followed by the file name. Gnu Emacs and Unipress Emacs are run as kept editors on VMS. This means that Emacs is run in a subprocess. The first time you run Emacs the subprocess is created. Each subsequent time you run Emacs, the same subprocess is run. This speeds the starting of Emacs after the first run.

Learning Emacs

GNU Emacs has printed manuals--with reference cards--available in the Institute's Science Software Support Office. It also has an online help facility and introductory tutorials. There are, however, a few functional differences among the various versions of Emacs. Quick reference cards are also available in Appendix A.

The online tutorials are a good way to learn Emacs. To use the tutorial:

If you prefer to use
MicroEmacs (which is smaller and faster than GNU Emacs), you can run a tutorial by typing:

This command starts the editor and loads a document that describes MicroEmacs and its functions.

Online help is also available by typing:


Key Bindings and Commands

commands have names like "next-page" and "previous-page." These commands provide all of the functions of Emacs, and even though you may think you're pressing - to see the next page of a file, that keystroke is actually mapped to the command "next-page." These keystroke bindings are stored in a table that can be modified, allowing a user to customize the editor.

MicroEmacs has fewer key bindings than the other versions due to its leaner nature. Keep in mind that any command can be executed in Emacs, regardless of whether it has a corresponding mapped keystroke. To invoke an unmapped command, press - . This will drop you to a line at the bottom of your terminal screen in which you can directly enter the command name. Once you type the command, press to execute the command.

If you want to see how your keys are initially bound, you can type the command examine-bindings. Look for - and - because these are the keys you need to rebind (see the next section)--you will certainly have to rebind the commands "search-forward" (GNU), "isearch-forward" (MicroEmacs), "save-buffer" (GNU), and "save-file" (MicroEmacs).

Flow Control Problems and Startup Files

One problem that you will encounter when using Emacs at the Institute is that VMS reserves the - and - keys for flow control (i.e., - suspends output from flowing to the terminal screen and - resumes it) so these keys cannot be used in Emacs key bindings. However, the default bindings for several of the most common commands include these keys. The solution is to rebind these commands in the startup file. The startup file is placed in your login directory and contains Emacs commands that are executed when you start the editor. The command format and name of the startup files are different for each of the versions of Emacs. The sample files shown in Figure 4.6 contain commands which unbind the - and - keys and rebind the save file and search commands to - and - respectively.

Gnu Emacs startup file (.EMACS)

(global-unset-key "\C-s") 
(global-unset-key "\C-q") 
(global-set-key "\C-xs" `save-buffer) 
(global-set-key "\M-s" `isearch-forward)

Micro Emacs startup file (EMACS.RC)

unbind-key ^S 
unbind-key ^Q 
bind-to-key save-file ^XS 
bind-to-key search-forward M-S

Unipress Emacs (.EMACS_PRO)

(remove-binding "\^S") 
(remove-binding "\^Q") 
(bind-to-key "write-current-file" "\^Xs") 
(bind-to-key "search-forward" "\033s")

Figure 4.6: Redefining Emacs Key Bindings

Basic Functions

Like any text editor, Emacs has a wide range of commands. In Emacs there are two ways to execute a command:

These control key sequences are either single control keys, or two-key sequences, usually beginning with - or . To execute a command in the command buffer, first press - , type the command name in the buffer, and press . To abort a partially entered command, either in the command buffer or in a two key sequence, press - .

A list of basic commands and their key mappings is shown in Table 4.4.

Table 4.4: Basic Emacs Command Sequences

Search and Replace

The "search-forward" command in Emacs is one that must be remapped to - . When you enter the keystroke to search, the cursor drops to a message line at the bottom of your screen. In the message line you should enter the text that Emacs should seek followed by . To find the next occurrence of a string, you would type - .

The search and replace function-- - --will do a global search, replacing all occurrences of the old text with the new text. You can have Emacs query you before making any changes by using the "query-replace" command, - (UniPress) or - (MicroEmacs). Each time an occurrence is found, you will be prompted to enter one of the following options:

Search functions work slightly different in GNU Emacs. The search function is "incremental-search" rather than "search-forward." After pressing - each character you enter is immediately added to the search string and is sought. You do not need to press at the end of the string, in fact, if you do press it, the search

Setting Up Each Version
Learning Emacs
Key Bindings and Commands
Flow Control Problems and Startup Files
Figure 4.6: - Redefining Emacs Key Bindings
Basic Functions
Table 4.4: - Basic Emacs Command Sequences
Search and Replace

Generated with CERN WebMaker