Barry's Emacs
User's Guide
Extensions Reference
MLisp Programmer's Guide
MLisp Reference
Barry's Emacs User Guide
This document describes the use and customisation of Barry's Emacs display editor:
- Introduction provides an introduction to Barry's Emacs.
- Basic Editing describes the basic Emacs commands that allow you to enter text, make corrections and save text in a file.
- Editing Specific File Types describes commands available in Emacs for editing specific file types.
- Editing Specific File Types describes Emacs' advanced editing facilities.
- Correcting Mistakes and Emacs Problems describes how to correct editing mistakes and recover from Emacs problems.
- Emacs Simple Customisation describes how to make simple Emacs customisations without a detailed knowledge of the Mock Lisp programming language.
- Package And Subsystem Index gives an alphabetically ordered list of all the packages, subsystems and libraries distributed with Barry's Emacs.
- Default Key Bindings gives an alphabetically ordered list of the default Emacs key bindings.
- The conventions section describes the meanings of the different typefaces used in this document.
There is also other documentation supplied with Barry's Emacs.
The Barry's Emacs software and documentation is Copyright © 1980-2020 Barry Scott.
This document is intended for all users of Barry's Emacs.
Other Emacs documentation
The Barry's Emacs Programmer's Guide describes how to extend Emacs using the Mock Lisp programming language.
The Barry's Emacs Mock Lisp Reference Manual contains an alphabetically ordered list of all Emacs functions and variables available in the Mock Lisp programming language.
The Barry's Emacs Extensions Reference Manual documents the Emacs extensions (packages, subsystems and libraries) that are distributed with Barry's Emacs.
Conventions
Throughout this text, key sequences are described as causing specific actions to occur (functions to run, for example). These key sequences are those set up when Emacs is started (they are the default key bindings).
Key sequences are displayed with each key separated by a dash. When using these sequences, do not include the dashes. The dashes are displayed for clarity only.
Many Emacs key sequences use Control keys. These are indicated by a caret character (^) appearing in the key sequence. For example, to type the ^O key sequence, press and hold down the Ctrl key and simlutaneously press the O key. The special sequence ^^ is used in some Emacs key sequences. To type ^^, press and hold down the Ctrl key, then simultaneously type the ^ key.
Many other Emacs key sequences use Escape keys. These are indicated with an ESC- in the key sequence. For example, to type the ESC-e key sequence, press the key labelled ESC, then press the e key. Note that ESC- key sequences are case-sensitive; this means that ESC-e is not the same as ESC-E.
Different typefaces used in this book are used to distinguish between normal text and special symbols. These are described as follows:
Return | The following special characters are displayed as shown: Return, Delete, Escape, Tab, Backspace, Linefeed and Space. |
This is an example |
All examples and system displays are shown like this. |
execute-mlisp-file | All keystroke sequences, function names, command names and variable names are displayed in this typeface. |
Introduction
Emacs provides facilities that go beyond simple text insertion and deletion: automatic indentation of programs; viewing multiple files at once, as well as expressions and comments in several different programming languages.
Emacs has extensive help, both in the form of help files and within Emacs itself. You can also use it to find out what any command does, or to find all the commands that pertain to a specific topic.
You can change the definitions of Emacs commands in small ways. For example, if you use a programming language in which comments start with /* and end with */, you can tell the Emacs comment manipulation commands to use those strings. Another sort of customisation is rearrangement of the command set.
Most of the customisation of Emacs is done via the emacs_user:emacsinit.ml initialisation file.
The remaining customisations of Emacs are done via the Edit, Preferences... menu under Windows. And under Unix via the $HOME/.Xdefaults file.
How to start Barry's Emacs
Under Windows you can start Emacs using all the usual Windows methods for starting an application.
- Using the bemacs command from a shell, CMD, 4NT etc.
- From the desktop shortcut.
- From the Barry's Emacs folder found in the Programs menu of the Start menu.
- From the Start Menu
- By dropping a file on to the desktop shortcut.
- By opening a document that is associcated with Barry's Emacs.
The bemacs command
The bemacs command sends your command line to the Barry's Emacs application. If Barry's Emacs is not running yet the bemacs command will start one running. If Barry's Emacs is already running the command line is sent to the the running Barry's Emacs.
The normal bemacs command line allows you to specify a list of filenames of the files you wish to edit.
bemacs file bemacs file1 file2 file3
You can use either "/" or "-" to start a switch. For example /line and -line are treated as equivalent.
By using the following command line switches you can change the default behaviour of the bemacs command.
- /name=name
- Send this command line to the Barry's Emacs named name. If /name is ommitted the name of the Barry's Emacs used is "main". Using the /name switch you can address as many seperate BArry's Emacs's as you need. /name must be the first parameter on the command line.
- /line=number file
- Start editing the file named file at line number number.
- +number file
- Start editing the file named file at line number number. This switch is support to allow the bemacs command to be substituted in place of the vi unix editor.
- /readonly file
- Start editing the file named file and set the buffer to be readonly - to prevent any modifications being made.
- /noreadonly file
- Start editing the file named file and set the buffer to be read-write - to allow modifications being made.
- /cd
- Barry's Emacs will change its current directory to be the same as current directoy used by the bemacs command.
- /wait
- Emacs will enter a recursive edit and the bmacs command will not exit until the recursive edit exits.
- /nowindow-restore
- Prevent Emacs from restoring its window if it is minimised.
The MLisp Programmers Guide has furthur information about Command Lines. Including how to customise command line handling.
examples
Edit the file emacsinit.ml at line number 106:
bemacs /line=106 emacsinit.ml
Edit the files emacs.h, emacs.cpp, use a Barry's Emacs called "Dev" and make Barry's Emacs change directory:
cd \emacs\src bemacs /name=Dev /cd emacs.h emacs.cpp
Edit the files emacs.h at line 19, emacs.cpp at line 254, use a Barry's Emacs called "Fred" and make Barry's Emacs change directory:
cd \emacs\src bemacs -name=Fred -cd -line:19 emacs.h -line=254 emacs.cpp
Use emacs to update a file as part of a CMD script. Notice the need to use start /wait to make the CMD shell wait for the bemacs command to complete.
echo log message for work done >log.tmp start /wait bemacs /wait log.tmp echo Log message type log.tmp
Default Key Bindings
The tables below are a list of the Emacs default key bindings.
The default binding are provided by calling the function default-emacs-init from your emacsinit.ml file. The default emacsinit.ml shipped with emacs calls default-emacs-init.
This is the same information as displayed by the describe-bindings function.
Binding | Function | Binding | Function |
---|---|---|---|
up | previous-line | ctrl-up | scroll-one-line-up |
down | next-line | ctrl-down | scroll-one-line-down |
right | forward-character | ctrl-right | forward-word |
left | backward-character | ctrl-left | backward-word |
home | PC-home-key | ctrl-home | beginning-of-file |
insert | Toggle replace-mode | shift-insert | UI-edit-paste |
ctrl-insert | UI-edit-copy | ||
delete | delete-next-character | shift-delete | PC-edit-cut |
ctrl-delete | delete-next-word | ||
end | end-of-line | ctrl-end | end-of-file |
page-up | previous-page | ctrl-page-up | beginning-of-window |
page-down | next-page | ctrl-page-down | end-of-window |
Binding | Function | Binding | Function |
---|---|---|---|
f1 | help |
Binding | Function | Binding | Function |
---|---|---|---|
mouse-wheel-pos | Scroll the buffer down 3 lines at a time | ctrl-mouse-wheel-pos | Scroll the buffer up 3 lines at a time |
mouse-wheel-neg | Scroll the buffer up 3 lines at a time | ctrl-mouse-wheel-neg | Scroll the buffer up 1 page at a time |
Binding | Function | Binding | Function |
---|---|---|---|
^@ | set-mark | ^A | Select the whole buffer |
^B | switch-to-buffer | ^C | UI-edit-copy |
^D | delete-next-character | ^E | end-of-line |
^F | incremental-search | ^G | interrupt-key |
^H | delete-previous-character | ^I | tab-key |
^J | newline-and-indent | ^K | kill-to-end-of-line |
^L | see ctrl-l table | ^M | newline |
^N | next-line | ^O | visit-file |
^P | previous-line | ^Q | quote-character |
^R | reverse-incremental-search | ^S | write-current-file |
^T | transpose-characters | ^U | argument-prefix |
^V | UI-edit-paste | ^W | dabbrev |
^X | see ctrl-x table | ^Y | yank-from-killbuffer |
^Z | new-undo | ESC | See Esc table |
^_ | help-key | BKSP | delete-previous-character |
What happens when ^X is typed changes if text is highlighted. If text is highlighted ^X will cut the text into the paste buffer, using UI-edit-cut. Otherwise ^X acts as prefix for the commands in the following table.
Binding | Function | Binding | Function |
---|---|---|---|
^X-^@ | unset-mark | ^X-^B | list-buffers |
^X-^C | exit-emacs | ^X-^D | describe-word-in-buffer |
^X-^E | compile-it | ^X-^F | write-file-exit |
^X-^I | insert-file | ^X-^M | write-modified-files |
^X-^N | next-error | ^X-^O | use-old-buffer |
^X-^R | read-file | ^X-^S | write-current-file |
^X-^U | new-undo | ^X-^V | visit-file |
^X-^W | write-named-file | ^X-^X | exchange-dot-and-mark |
^X-^Z | shrink-window | ^X-! | execute-monitor-command |
^X-( | start-remembering | ^X-) | stop-remembering |
^X-1 | delete-other-windows | ^X-2 | split-current-window |
^X-3 | split-current-window-vertically | ^X-= | what-cursor-position |
^X-? | apropos-variable | ^X-b | switch-to-buffer |
^X-d | delete-window | ^X-e | execute-keyboard-macro |
^X-n | next-window | ^X-p | previous-window |
^X-z | enlarge-window |
Binding | Function | Binding | Function |
---|---|---|---|
^L-^B | buffer-dired | ^L-^D | list-databases |
^L-^L | redraw-display | ^L-^P | list-processes |
Binding | Function | Binding | Function |
---|---|---|---|
ESC-^C | exit-emacs | ESC-^D | debug |
ESC-^I | tab-delete | ESC-^V | page-next-window |
ESC-^W | delete-region-to-buffer | ESC-^Y | yank-buffer |
ESC-ESC | minibuf | ESC-! | line-to-top-of-window |
ESC-( | backward-paragraph | ESC-) | forward-paragraph |
ESC-, | beginning-of-window | ESC-- | meta-minus |
ESC-. | end-of-window | ESC-0..ESC-9 | meta-digit |
ESC-< | beginning-of-file | ESC-> | end-of-file |
ESC-? | apropos | ESC-@ | column-to-left-of-window |
ESC-C | case-capitalize | ESC-^ | case-word-invert |
ESC-a | backward-sentence | ESC-b | backward-word |
ESC-d | delete-next-word | ESC-e | forward-sentence |
ESC-f | forward-word | ESC-g | goto-line |
ESC-h | delete-previous-word | ESC-i | case-invert |
ESC-j | indent-C-procedure | ESC-l | case-lower |
ESC-q | query-replace-string | ESC-r | replace-string |
ESC-u | case-upper | ESC-v | previous-page |
ESC-x | execute-extended-command | ESC-z | scroll-one-line-down |
Basic Editing
This chapter describes basic Emacs functions that allow you to enter text, make corrections, and save the text in a file.
- The Organisation of the Screen describes the organisation of the Emacs screen.
- Self-Documentation Commands describes Emacs' self-documentation features.
- Basic Editing Commands describes the basic editing commands.
- Command Prefix Arguments shows how to prefix Emacs commands with numerical arguments.
- Files describes Emacs basic file handling commands.
The Organisation of the Screen
![]() |
The Emacs screen is shown in Figure 1. Moving the mouse pointer over the figure will display the name of the parts of the screen in the browser status line.
Emacs divides the screen into several areas, each of which contains its own kind of information. The biggest area is the one in which you usually see the text you are editing. The cursor usually appears in the middle of the text, showing the position of dot, the location at which editing takes place. The cursor is a vertical line by default. In Figure 1 the cursor can be seen at end of the word "me" and before "electronically".
The line between the modeline and the status bar on the screen is called the Mini-buffer. This area is used to display messages and error text when a command is incorrectly used. It is also used to collect arguments for commands that require them when they are executed interactively. Normal editing commands can be used in the Mini-buffer.
Windows
The main area of the screen may be divided up into a number of windows, as shown in Figure 2. A window is associated with a buffer which will contain the contents of a file.
![]() |
Commands exist to change the association of windows to buffers and to change the number of windows displayed on the screen.
A window must be at least two lines deep and at least two columns wide. You can therefore display a lot of windows on the screen at any one time. The variable split-height-threshold controls the size of windows that may be divided, and therefore controls the minimum number of windows on the screen.
Mode Lines
The line at the bottom of each window is known as the window's Mode Line. It is the line that usually starts with Buffer and is displayed with a blue background. Its purpose is to tell you information that may affect the meaning of your commands aside from the text itself.
Some Emacs commands read and process other commands. They are known as subsystems and may initiate recursive editing levels. If you are inside a subsystem or a recursive editing level, the mode line tells you its name. Square brackets around the mode line indicate the depth of recursive editing levels.
Subsystems and recursive editing levels are advanced features, and you will not initially be using them. When you are not inside a subsystem or recursive editing level, we say that you are at the top level; this implies that the characters you type are interpreted as ordinary Emacs commands. When you are at top level, the mode line tells you what file you are editing, where you are in it, and what editing modes are enabled which may affect the meaning of your commands.
The format of the default mode line is shown in Figure 3. The figure will display the names of the parts of mode line in the browser status line. Clicking on parts of the figure will take you to the description of that part of the mode line.
![]() |
The components of the mode line are:
major
The name of the major mode you are in. At any time, Emacs is in one and only one of its possible major modes. The major modes available include Normal (in which buffers are created), Text, Lisp, Pascal, and others.
minor
A list of some of the minor modes that are enabled. Journal means that journalling is enabled for this buffer. Checkpoint means that check pointing is enabled for this buffer and generally for all buffers. Overstrike means that character replace mode is switched on so that characters that you type replace the characters that exist in the buffer already. Abbrev means that abbreviation mode is on and that abbreviations are automatically expanded. Readonly means that you cannot alter the contents of the buffer.
bfr
The name of the buffer currently associated with the window. Each buffer has its own name and may hold a file being edited; this is how Emacs holds several files at once. At any time you can edit only one buffer, the current buffer, though you may have many files open within Emacs.
File
The name of the file that you are editing. It is the last file that was visited in the associated buffer. If the buffer does not contain the contents of a file, then [No File] is displayed. Buffers of this type are called scratch buffers.
*
Indicates that there are unsaved changes in the buffer. If the buffer has not changed since it was read in or saved, no asterisk is displayed.
pos
Tells you whether there is additional text above the top of the screen, or below the bottom. It is Top if you are looking at the beginning of the file, Bottom if you are looking at the end of the file, or nn% where nn is the percentage of the file above dot.
subsys
The name of the currently running subsystem. By default, this is blank.
[
Square brackets in the mode line indicate the current recursive edit depth. When the recursion depth is zero, square brackets are not displayed. When the recursion depth is less than five, recursion-depth number of square brackets are included in the mode line. When the recursion depth is greater than five, the depth is indicated by [n[ and ]n], where n is the recursion depth.
<
A marker indicating that the buffer is horizontally scrolled left by the indicated number of characters. When no left-scrolling is being used, the indicator is not included in the mode line. Otherwise, <n< is displayed, where n is the number of characters by which the screen is scrolled to the left.
Self-Documentation Commands
Emacs provides extensive self-documentation features which are invoked by a single character, called the Help character. At any time while using Emacs, you can type the Help character to ask for help. The Help character is actually typed as ^_ (control-shift-underscore).
If you type the Help character while you are using a subsystem (such as INFO), Emacs may print a list of the commands available in that subsystem. However, it is up to the subsystem to provide this facility.
The Emacs Help facility offers several options for asking about the commands that exist and what the commands do. It prompts with Doc (? for help): at the bottom of the screen, and you type a character to say what kind of help you want. You could type Help or ? at this point to find out what options are available. The available options are listed in Table 1.
Description | Option |
---|---|
Show command containing a string (apropos) | a |
Describe all key bindings | b |
Describe a command | d |
Describe a key binding | k |
Describe a variable | v |
Find a function on the keyboard | w |
The most basic Help options are Help v and Help d. You can use them to ask what a particular command or variable does. Help d is for asking about functions (or extended commands); type the name of the function and a Return. Thus, Help d lisp-mode tells you about ESC-x lisp-mode. Help v tells you about variables in the same way. d stands for describe, since Help d actually uses the function describe-command to do the work.
A more complicated question to ask is, What are the commands or variables for manipulating something?. For this, you use the Help a command. This command prints a list of all the functions and variables that have the word you specified anywhere in their name. The a stands for "apropos", since Help a actually uses the functions apropos and apropos-variable to do the sub string matching.
Help a also tells you what keys are bound to the commands, if any. For example, it would say that you can invoke write-modified-files by typing ^X-^M.
Since the apropos functions look only for functions and variables whose names contain the string you specify, you must use ingenuity in choosing strings. If you are looking for commands for deleting backwards, and Help a delete-backwards does not reveal any, do not give up. Try delete, or backwards, or just back. Be persistent.
Table 2 contains a set of apropos strings that cover many classes of Emacs commands, since there are strong conventions for naming the standard Emacs commands. By giving you a feel for the Emacs naming conventions, this set should help you to develop a technique
character | line | word | sentence | paragraph |
region | buffer | screen | window | bound |
binding | file | beginning | end | case |
mode | forward | backward | next | previous |
up | down | search | kill | delete |
mark | fill | indent | UI | syntax |
Basic Editing Keyboard Commands
This section describes the basic text editing features of Emacs. It shows how to insert and erase text and how to move the cursor around the screen.
inserting text
To insert printing characters into the text you are editing, just type them.
To delete text you have just inserted, you can use the Backspace key. Backspace deletes the character before the cursor. The cursor and all characters after it move backwards. Therefore, if you type a printing character and then type Backspace, they cancel out.
To end a line and start typing a new line, press the Return key. Return operates by inserting a line separator, so if you type Return in the middle of a line, you break the line in two.
If the wrap-long-lines variable is set, and you add too many characters to one line without breaking it with a Return, the line grows to occupy two or more lines on the screen, with a >= at the extreme right margin of all but the last line. The >= means that the following screen line is not really a distinct line in the file, but just the continuation of a line too long to fit on the screen.
If the wrap-long-lines variable is not set, a diamond is displayed at the end of each line that is longer than the screen.
Direct insertion works for printing and layout characters, but other characters act as editing commands and do not insert themselves. If you need to insert a control character you must quote it by using the ^q (quote-character) command first.
moving the cursor
To do more than insert characters, you have to know how to move the cursor. Here are a few of the commands for doing this:
Move to the beginning of the current line. | ^A or Home (beginning-of-line) |
Move to the end of the current line. | ^E or End (end-of-line) |
Move forward one character. | ^F or Right-arrow (forward-character) |
Move backward one character. | ^B or Left-arrow (backward-character) |
Move down one line, vertically. If you start in the middle of one line, you end up in the middle of the next line. |
^N or Down-arrow (next-line) |
Move up one line, vertically. | ^P or Up-arrow (previous-line) |
Clear and redraw the display. | ^L (redraw-display) |
Transpose the last two characters. | ^T (transpose-characters) |
Move to the top of the file. | ESC-< (beginning-of-file) |
Move to the end of the file. | ESC-> (end-of-file) |
erasing text
Delete the character before the cursor. | Backspace (delete-previous-character) |
Delete the character after the cursor. | ^D or Delete (delete-next-character) |
Delete to the end of the current line. | ^K (kill-to-end-of-line) |
You already know about the Backspace command, which deletes the character before the cursor. Another command, ^D, deletes the character after the cursor, causing the rest of the text on the line to shift left. If ^D is typed at the end of a line, that line and the next line are joined together.
Basic Editing Mouse Commands
The default actions of the mouse follow the normal windows conventions. The following sections give details of the actions.
moving the cursor
The cursor can be positioned by clicking once with the left mouse button. If you click in the mode line of a window that window is selected and the cursor is left where you where last editing. If you click inside the windows text area the cursor is positioned between the nearest two characters.
selecting text
To select a block of text with the mouse use the left mouse button.
Start selecting text by pressing the mouse button then dragging it over the text to be selected. Release the mouse button when the right text is selected.
A single click will cancel any selection and position the cursor.
A single click and drag will select text a character at a time.
A double click selects one word.
A double click and drag will select text a word at a time.
A triple click selects one line.
A triple click and drag will select text a line at a time.
dragging the selection
The mouse can be used to drag the selected text to a new location.
Press the mouse button inside the selection and drag the mouse to the new position for the text. If you release the mouse button inside the selection it will not be moved.
arranging windows
If there are more then one window on the screen the mouse can be used to resize the windows.
Press the left mouse button in the mode line of a window and drag the mode line to resize the window.
If you drag the mode line so that the window is less then one line high the window is deleted.
Command Prefix Arguments
Any Emacs command can be given a numeric argument. Some commands interpret the argument as a repetition count. For example, giving an argument of 10 to the ^F command (move forward one character) moves forward 10 characters. With these commands, specifying no argument is equivalent to providing an argument of 1.
Some commands care only about whether there is an argument, and not about its value; for example, the command ^X-^D (describe-word-in-buffer) attempts to tell you about the word in which dot is located. If you provide a prefix argument, it allows you to alter the definition for the word using a recursive edit.
The fundamental way of specifying an argument is to use the ^U (argument-prefix) command followed by the digits of the argument. Negative arguments are allowed -- they often tell a command to move or act backwards. A negative argument is entered with ^U followed by a minus sign and the digits of the argument value.
^U followed by a character that is neither a digit nor a minus sign has the special meaning of multiply by four. It multiplies the argument for the next command by four. Two such ^Us multiply it by sixteen. Thus, ^U-^U-^F moves forward sixteen characters. This is a good way to move forward quickly, since it moves about a quarter of a line on most terminals. Other useful combinations are ^U-^N and ^U-^U-^N which move down useful fractions of a screen.
There are other terminal-dependent ways of specifying arguments. They have the same effect as typing ^U, but may be easier to type. If your terminal has a numeric keypad which sends something recognisably different from the ordinary digits, it is possible to customise Emacs to allow use of the numeric keypad for specifying arguments.
autoarg mode
Autoarg-mode allows you to specify prefix arguments without using ^U. Instead, you type just the digits of the argument. Typing digits followed by an ordinary inserting character causes the digits themselves to be inserted, but digits followed by a command character serve as an argument to the command and are not inserted. A minus sign can also be part of an argument, but only at the beginning. If you type a minus sign following some digits, both the digits and the minus sign are inserted.
To use Autoarg Mode, type the command ESC-x auto-arg-mode.
Auto-argument digits echo at the bottom of the screen; the first non-digit character causes the accumulated digits to be inserted or used as an argument. To insert some digits and nothing else, you must follow the digits with a Space, then delete the space character. ^G cancels the digits.
Files
To keep any text permanently you must store it in a file. To tell Emacs to edit text in a file, choose a filename and use the ^X-^V command. This visits the specified file so that its contents appear on the screen in a new buffer and window. You can make changes, and then save the file by typing ^X-^s. This makes the changes permanent and creates a new version of the file. Note that, until you save changes to the file, the changes are only inside your Emacs; the file itself is not really changed.
If the file does not exist and you want to create it, just visit it as if it did exist. When you save your text with ^X-^S, Emacs will create the file.
See Advanced File Handling for more information on handling files.
Editing Specific File Types
This chapter describes the commands available in Emacs for editing specific types of file, such as text memos or program source code:
- Major Modes summarises the Emacs major modes used to edit specific file types.
- Editing Natural Language Text describes Emacs commands for editing natural language text.
- Editing Programs on describes special features for editing programs.
Major Modes
Emacs has many different major modes, each of which customises Emacs for editing text of a particular file type. The major modes are mutually exclusive -- that is, only one major mode can be active at a time. When at top level, Emacs indicates in the mode line which major mode is active for each buffer displayed on the screen.
When Emacs creates a buffer, it is in what is called Normal Mode , which means that the character commands are defined so as to be convenient for general use. More precisely, in Normal Mode every Emacs binding is set to its default state. For editing any specific type of text, such as Lisp code or English text, you should switch to the appropriate major mode. This tells Emacs to change the meanings of a few commands to become more specifically adapted to the language being edited. Most commands remain unchanged.
Selecting a new major mode is done with an ESC-x command. Each major mode is the name of the function that selects that mode. Thus, you can enter Lisp Mode by typing ESC-x lisp-mode. Emacs can be made to enter the correct major mode for a file simply based on the file's type, so you do not have to worry about selecting a mode. This is achieved by using the auto-execute function.
The ESC-x auto-execute command causes the nominated command to be executed every time a file is manipulated which matches the specified file pattern. The file pattern is specified as a wildcard pattern.
The "*" and "?" characters are treated specially, all other characters stand for themselves. A "*" match zero or more characters and a "?" matches a single character.
Here are some example patterns:
Pattern | Explanation | Examples |
---|---|---|
*.cxx | Matches all files whoes names end in .cxx. | emacs.cxx
search.cxx .cxx |
*?.cxx | Matches all files whoes names has atleast one characters before ending with .cxx. | emacs.cxx
search.cxx a.cxx |
readme* | Matches all files that start with readme. | readme.txt
readme.1st |
The command is executed whenever the file name matches the pattern. If you wish to use Lisp Mode when visiting files with a type of .ML, use the command ESC-x auto-execute lisp-mode *.ml.
Syntax tables define how Emacs commands (such as forward-word) react to the characters in a buffer. Many major modes alter syntax tables so that characters are treated in the most suitable way for the particular mode. For example, most programming language major modes specify that only blank lines separate paragraphs. This is so that the paragraph commands remain useful when writing program source code rather than natural language text.
To find out which keys a particular major mode redefines, use the command ESC-x describe-bindings while the required mode is in effect. This command displays all the key bindings for the current buffer. The specific bindings for the current major mode will be displayed under the heading Local bindings, along with any other local bindings you have defined yourself.
Editing Text
Emacs enables you to easily manipulate words, sentences, or paragraphs of text. In addition, there are commands to fill text and convert character cases.
word commands
Emacs has commands for moving over or operating on words. By convention, they are all Escape prefixed commands. All of these commands may be used in any major mode.
Move forward over a word. | ESC-f or Ctrl-Right |
Move backward over a word. | ESC-b or Ctrl-Left |
Delete up to the end of a word. | ESC-d or Ctrl-Delete |
Delete back to the beginning of a word. | ESC-h |
Notice how these commands form a group that parallels the character-based commands ^F, ^B, ^D and Delete.
The commands ESC-f and ESC-b move forward and backward over words. They are analogous to ^F and ^B, which move over single characters. Like their Control- analogues, ESC-f and ESC-b move over several words if they are given a prefix argument. ESC-f with a negative argument moves backwards like ESC-b, and ESC-b with a negative argument moves forwards. Forward motion stops exactly after the last letter of the word, while backward motion stops exactly before the first letter.
It is easy to delete a word at a time. ESC-d deletes the word after dot. To be precise, it deletes everything from dot to the place that ESC-f would move dot. Therefore, if dot is in the middle of a word, only the part of the word after dot is deleted. If some punctuation comes after dot and before the next word, it is deleted along with the word. If you wish to delete only the next word but not the punctuation, use ESC-f to go to the end, then delete the word backwards with ESC-h. ESC-d takes arguments just like ESC-f.
ESC-h deletes the word before dot. It deletes everything from dot back to where ESC-b would move dot. If dot is after the space in FOO, BAR, then FOO, is deleted. If you wish to delete just FOO, use ESC-b then ESC-d instead of an ESC-h.
The definition of a word as used by the word commands is completely controlled by the current syntax table. Any character can, for example, be declared as a word-delimiter.
sentence and paragraph commands
The Emacs commands for manipulating sentences and paragraphs are mostly Escape prefixed commands so as to resemble the word-handling commands.
Move back to the beginning of a sentence. | ESC-a |
Move forward to the end of a sentence. | ESC-e |
Move back to the beginning of the previous paragraph. | ESC-( |
Move forward to the end of the next paragraph. | ESC-) |
The commands ESC-a and ESC-e move to the beginning and end of the current sentence respectively. They were choosen to resemble ^A and ^E, which move to the beginning and end of the current line. Emacs considers a sentence to end wherever there is a ., ? or ! character followed by white-space or an end-of-line. Neither ESC-a nor ESC-e move past the white-space which delimits the sentence.
The variable sentence-delimiters is a regular expression search-string composed of the various strings that define a sentence boundary. The default value of this variable is:
[.!?][\040\t\n][\040\t\n]*
ESC-( moves to the beginning of the current or previous paragraph, while ESC-) moves to the end of the current or next paragraph. Blank lines and text formatter command lines are not considered part of any paragraph. Also, an indented line starts a new paragraph.
The variable paragraph-delimiters is a regular expression search string composed of the various strings that define a paragraph boundary. The default value of this variable is:
\f\n\|[\040\t]*\n
Indentation commands for code
Tab | Indents appropriately in a mode-dependent fashion. |
Linefeed | Performs the same action as Return followed by Tab. |
Use the Tab command to request indentation. Its precise effect depends on the major mode. In Text and Normal Modes, it indents to the next tab-stop.
When you type normal text, if you use the Linefeed key to terminate a line, the indentation of the current line will be duplicated at the start of the next line. So to type in several lines of text all starting at a specified indent, use the Linefeed key to terminate each line.
text filling and text mode
Emacs has a major mode called Text Mode which provides functions for setting margins, filling, justifying and centering text. Use the ESC-x text-mode command to enter Text Mode.
The Text Mode functions can be used without needing to switch to Text Mode -- they can be used from any major mode. This is usually more convenient than having to change the major mode to Text Mode when you need to format some text. Text Mode is included in Emacs for backwards compatibility only.
If you enable the Text Mode functions explicitly using ESC-x text-mode, the Text Mode functions are bound to keys. If you do not switch to Text Mode to use the functions, you must either provide your own bindings for the functions, or execute the functions as extended commands using ESC-x.
The Text Mode functions and key bindings are:
Set the left margin. | ESC-1 |
Set the right margin. | ESC-^H |
Set the margins to the values indicated by text in the current paragraph. The right margin is defined as the right-most column in which text appears for paragraphs with a ragged-right margin. | (set-margins) |
Set the paragraph indent offset. | ESC-i |
Fill or justifies the current paragraph between the currently set margins. | ESC-j |
Fill or justifies the region between the currently set margins. | ESC-J |
Toggle the current justify mode to either a ragged or straight right margin. | ESC-m |
Centre the current line between the currently set margins. | ESC-c |
The fill-and-justify functions will fill the current paragraph between the current margin settings. If you edit text in the middle of a paragraph, the paragraph may no longer be correctly filled -- just type ESC-j to fill the paragraph. The effect of ESC-j can be undone with ^X-^U (undo). See Undoing Changes to the Buffer for more information on undoing changes.
Paragraphs can be indented using ESC-i to set an indent offset. A positive value for the indent offset causes the first line of the paragraph to be indented by the specified number of characters. For example, with an indent offset of 5, paragraphs will appear as:
Here is the beginning of the paragraph. Here is some more text, and here is yet more.
A negative indent offset enables you to create hanging paragraphs; with an indent of -5 and a left margin of 5, text will be filled as follows:
1. text....text ....text more text..... text text more text..... text text more text..... text text 2. text....text ....text more text..... text text more text..... text text Here is the beginning of the paragraph Here is some more text, and here is yet more.
The Text Mode functions access several buffer-specific variables. You can use either use the Text Mode bindings listed above to set the values of these variables, or you can set them directly using ESC-x set. If you want to change their default values, use ESC-x set-default.
The Text Mode variables are:
left-margin
Contains the left margin setting for the current buffer. The default setting for the left margin is 1.
right-margin
Contains the right margin setting for the current buffer. The default setting for the right margin is 1000 -- if you use the Text Mode fill functions from any mode other than Text Mode, be sure to set right-margin to a reasonable value.
index-offset
Contains the index offset for the current buffer.
buffer-display-margins
Controls whether the current margin settings are displayed in the buffer's mode line. This variable is set to 1 by default, so margins are displayed. If you do not want margins displayed, set the variable to 0.
autowrap-mode-flag
Controls whether text is automatically wrapped as you type it. This is set to 1 by default, so that when you type text beyond the right margin, a line separator is automatically inserted, and you can continue typing without worrying about breaking the line yourself. This feature is known as auto-filling.
buffer-justify-flag
Controls the appearance of text at the right margin. If set to 1 (the default), text will be be filled with a straight right margin; if buffer-display-margins is set to 1, the right margin indicator in the mode line will be a J. If buffer-justify-flag is set to 0, text will be filled with a ragged right margin; if buffer-display-margins is set to 1, the right margin indicator in the mode line will be an R.
case conversion commands
Emacs has commands for converting either a single word or any arbitrary range of text to be capitalized, upper-case, lower-case or to invert the case.
Convert the current word or region to lower-case. | ESC-l (case-lower) |
Convert the current word or region to upper-case. | ESC-u (case-upper) |
Invert the case of the current word or region. | ESC-i (case-invert) |
Capitalize the current word or region. | ESC-C (case-capitalize) |
If a case conversion command is given in the middle of a word, it applies to the whole word.
To convert the case of part of a word set the region around the part to be converted.
Editing Programs
Special features for editing programs include automatic indentation, comment alignment, parenthesis matching, and the ability to move over balanced, parenthesised expressions. Many of these features are generalised so that they can work for any programming language.
For some languages, there is a special major mode which customises Emacs to be better-suited to editing programs written in that language.
The languages supported by major modes include:
C | C Mode or Electric C Mode |
C++ | C Mode or Electric C Mode |
MLisp | Lisp Mode |
Lisp | Lisp Mode |
PASCAL | PASCAL Mode |
Bliss | Bliss Mode |
The TAGS package can be used to remember all the labels or functions in a multi-file program, and to find any one of them quickly.
compiling your program
The command ^X-^E (compile-it) is used to compile a program within Emacs. Normally, ^X-^E uses the make command to compile a program. make knows how to build your program based on a description file that you give it.
The first thing ^X-^E does is to write out all modified files. This is because it is likely that other buffers contain parts of the same program that you are about to compile.
Then ^X-^E invokes the make command. All output from make goes into the empty buffer Error log, which will be displayed in a window on the screen. Messages that the compilation generates are displayed in the Error Log buffer as they are generated.
When the compilation is complete, the function parse-error-messages-in-region is applied to the entire buffer. This command uses the function specified in the variable error-message-parser to find all the compiler error messages. For each error message generated, the source file in which the error was detected is visited and the line on which the compilation detected an error is marked. This process generates a list of marks in various buffers for viewing with the ^X-^N (next-error) command. When all errors have been visited, ^X-^N says so.
In summary, a typical use of the compilation facility is as follows:
- Prepare some source files;
- Type ^X-^E to compile the program;
- Use ^X-^N until Emacs says there are no more errors, then go back to step 2;
- When no more errors are detected, the program is ready to run.
You can also compile programs with the ESC-x shell command. In shell, you are put into shell window in Emacs where every line that you type and end with a Return is executed as a DCL command. The output from the commands is displayed in the shell window. Several key sequences are re-bound in shell to be more useful. ^R is bound to insert the last command sent to DCL into the buffer at dot. So, if you make a mistake, you can easily edit the command and try it again. The last 20 commands are remembered and can be recalled with successive ^R commands. The shell command is described more fully in the Barry's Emacs Extensions Reference Manual.
Indentation commands for code
Indent the current line. | Tab |
Equivalent to Return followed by Tab. | Linefeed |
Delete all spaces and tabs around dot. | ESC-x delete-white-space |
Most programming languages have some indentation convention. For Lisp, lines are indented according to their nesting in parentheses. For assembler code, almost all lines start with a single tab, but some have one or more spaces as well.
Whatever the language, to indent a line, use the Tab command. Each major mode defines this command to perform the sort of indentation appropriate for the particular language. In Lisp Mode, Tab aligns the line according to its depth in parentheses. This only happens if the tab is the first character on a line.
The command Linefeed does a Return, then does a Tab on the next line. Thus, Linefeed at the end of the line makes a following blank line and supplies it with the appropriate amount of indentation. Linefeed in the middle of a line breaks the line and supplies the indentation in front of the new line.
To delete just the indentation of a line, go to the beginning of the line and use ESC-x delete-white-space, which deletes all spaces and tabs around the cursor.
To insert an indented line before the current line, use ^A-^O-Tab. To make an indented line after the current one, use ^E-Linefeed.
automatic display of matching parentheses
The Emacs parenthesis matching feature shows automatically how parentheses balance as you enter text. When this feature is enabled, after a closing parenthesis or other close-bracket character is inserted, the cursor automatically moves for an instant to the open parenthesis which balances the newly-inserted character. The cursor stays at the open parenthesis for a second before returning to the close parenthesis.
The parenthesis-matching feature is enabled by using Fundamental Mode rather than the default Normal Mode, and may be enabled by language modes as required. It is also available on its own as the MLisp package FLASHER. You can load the FLASHER package yourself to have parenthesis-matching in a specific buffer. Several language major modes also load FLASHER to provide this feature when you edit program source code.
It is worth emphasising that the location of dot (the place where your text will be inserted) is not affected by the parenthesis-matching feature. Dot stays after the close parenthesis, where it ought to be -- only the cursor on the screen moves away and back. You can type ahead freely as if the parenthesis display feature was not enabled. In fact, if you type fast enough, you will not see the cursor move -- you must pause after typing a close parenthesis to let the cursor move to the open parenthesis.
If you type in an un-matched parenthesis, Emacs rings the bell and tells you there is an error.
Major Modes for Programming Languages
Lisp's simple syntax makes it easy for an editor to understand. As a result, Emacs can do more for Lisp, and with less work, than for any other language.
Paragraphs are defined to start only with blank lines so that the paragraph commands can be useful. Auto-fill Mode indents the new lines which it creates. Comments start with a semicolon ";". For example:
; This is a comment
The command ` (expand-mlisp-word) helps you to insert Emacs function names; it gives you name completion, which saves a lot of typing.
Lisp Mode commands include:
Move back one s-expression. | ESC-( |
Move forward one s-expression. | ESC-) |
Compile and execute the first MLisp expression in the current buffer. | ESC-c |
Re-indent the current MLisp function. | ESC-j |
Re-indent the current MLisp line. | ESC-i |
Insert a Lisp comment. | ; |
ESC-c can be used to take the single MLisp expression in the current buffer and compile it in Emacs as MLisp. The compiled MLisp is then executed. Thus, if you write some MLisp, the easiest way to test and debug it is to run it using ESC-c.
To re-indent the current function (as defined by lines staring with (defun) use the ESC-j command. This command uses the same indenting algorithm as the Linefeed and Tab commands.
Lisp Mode works in exactly the right way only for the MLisp dialect of Lisp. For other Lisps, the syntax tables may have to be modified.
C Mode is a major programming mode for writing C programs. It provides optional automatic expansion of C keywords, formatting them according to Software Engineering standards.
When you type a C keyword in C Mode and complete it with a non-word character, C Mode automatically recognises the word, and performs the expansion. If a word is not recognised, it is not expanded.
If you do not require automatic expansion, set the variable c-auto-expansion to zero. The function expand-c-keyword can then be used directly after each keyword to force the expansion to take place.
C Mode works by reading templates from the Emacs language template library accessed through the language database search-list.
This search-list contains the provided template library database, and also the database sys$login:language-template. The database language-template is searched first, so your customised templates are used before the system's templates.
To cause the expansion for module and function headers, enter the appropriate word and use the expand-c-keyword function.
If you try to expand a C word that C Mode does not understand, an error message is issued. All C keywords, braces, comments and tertiary operators are recognised by C Mode.
PASCAL Mode is a simple major mode for writing PASCAL programs. PASCAL Mode works by automatically upper-casing all PASCAL reserved words as they are entered. Table 3 shows which
PASCAL Keywords Recognised by PASCAL Mode
AND | ARRAY | BEGIN | BOOLEAN | CAND | CASE |
CHR | CONST | COR | DIV | DO | DOWNTO |
ELSE | END | EXIT | EXPORTS | FALSE | FILE |
FOR | FORWARD | FROM | FUNCTION | GET | GOTO |
IF | IMPORTS | IN | INPUT | INTEGER | LABEL |
LONG | MOD | MODULE | NEW | NIL | NOT |
OF | OR | ORD | OTHERWISE | OUTPUT | PACKED |
PRIVATE | PROCEDURE | PROGRAM | PUT | READ | READLN |
REAL | RECORD | REPEAT | RESET | REWRITE | SET |
SET | STRING | TEXT | THEN | TO | TRUE |
TYPE | UNTIL | VAR | WHILE | WITH | WRITE |
WRITELN |
Two types of PASCAL comment are recognised: (* with *) and { with }.
The function Pascal-Skeleton can be used to create a function or procedure template and fills in the appropriate parameters and types.
Bliss Mode
Bliss Mode is a major programming mode for writing any dialect of Bliss code. It provides optional automatic expansion of Bliss keywords, formatting them according to Software Engineering standards.
When you type a Bliss keyword in Bliss Mode and complete it with a non-word character, Bliss Mode automatically recognises the word, and performs the expansion. If a word is not recognised by Bliss Mode, it will not be expanded. Some words in Bliss may be grouped together to form strings of keywords (for example, EXTERNAL ROUTINE). To solve the problem of whether or not to expand a word, Bliss Mode looks at the word separator. If a word may form a string of words, Bliss Mode assumes that there are no more words to be input if the separator is a Return. So, to define a GLOBAL BIND ROUTINE, type GLOBAL BIND ROUTINE Return.
If you do not require automatic expansion, set the variable bliss-auto-expansion to zero. The function expand-bliss-keyword can then be used directly after each keyword to be expanded to force the expansion to take place.
Bliss Mode works by reading templates from the Emacs language template library accessed through the language database search-list.
This search-list contains the provided template library database, and also the database sys$login:language-template. The database language-template is searched first, so your customised templates are used before the system's templates.
For example, when you need to write a new Bliss module, do the following:
- Obtain a new buffer and switch to Bliss Mode.
- Type the word module Bliss Mode fills in the module bracket at the end of the line, and expects you to type in the module name by prompting with [Module Name] in the Mini-buffer.
- Type in the module name. Use ^C (exit-emacs) to terminate the field.
- Bliss Mode proceeds to fill in the %TITLE line above the module name, and asks you to fill in the rest of the title. It prompts [Module title]
- The next field is module options; Bliss Mode fills in the copyright notice, asking for your name and stepping through the possible module switches and header information.
If you try to expand a Bliss word that Bliss Mode does not understand, an error message is issued. All Bliss reserved keywords and comment characters are recognised.
- Interrupting and Aborting Emacs Commands
- Dealing with Common Emacs Problems
- Undoing Changes to the Buffer
Correcting Mistakes and Emacs Problems
If you type an Emacs command you did not intend, the results are often mysterious. This chapter describes what you can do to cancel your mistakes and recover from such mysterious situations.
- Interrupting and Aborting Emacs Commands shows how you can interrupt commands.
- Dealing with Common Emacs Problems describes how you can deal with some commonly-occurring Emacs problems.
- Undoing Changes to the Buffer describes how you can undo changes made to your editing buffers.
Interrupting and Aborting Emacs Commands
Cancel a running or partially-typed command. | ^G (interrupt-key) |
Abort all recursive editing levels and subsystems that are currently executing. | ESC-x top-level |
There are two ways of cancelling commands which have not finished executing: interrupting the command with ^G, or aborting all recursive edits with ESC-x top-level.
Interrupting with ^G is used to abort a partially-typed command or a numeric argument that you do not want. It also stops a running command in a safe way, so you can use ^G if you accidentally give a command which takes a long time to execute.
When you are in a position to use ESC-x, you can use ESC-x top-level. This command aborts all active recursive edits and subsystems. ESC-x top-level is like all other commands (apart from ^G) in that it is effective only when Emacs is waiting for keyboard input, whereas ^G can be used at any time.
Dealing with Common Emacs Problems
This section describes various conditions which can cause Emacs to function incorrectly or which can cause it to display strange things, and describes how to correct such conditions.
subsystems and recursive editing levels
Subsystems and recursive editing levels are important and useful features of Emacs, but they can make Emacs appear to behave strangely to the user who does not understand them or has never used them before.
If the mode line starts with a [, or does not look like the standard mode line, you have probably entered a subsystem or a recursive editing level.
In such a situation, first try typing ESC-x exit-emacs. This will exit from any recursive editing level or subsystem. The usual mode line and text display will reappear. If exit-emacs does not seem to work, try typing ^G to abort the current command. This may have the desired effect. Otherwise, use ESC-x top-level.
Undoing Changes to the Buffer
If you mistakenly issue commands that make changes to the buffer, you can often undo the changes without having to know precisely how they came about. This is done by using the ^X-^U command (undo). Type ^X-^U and a change is undone. undo then displays the message Hit <Space> to undo more in the Mini-buffer. For every Space that you press, another change you made to the buffers will be undone. When you press the first non-space character, that character is treated in the usual way and undo exits with the message Finished undoing. If the command is not a Return, its usual action is also executed.
The changes that ^X-^U undoes include all buffer insertions and deletions and any movement of dot in a buffer. Side-effects of MLisp functions, including creation of buffers and setting of variables, are not undone.
For commands that have not affected a buffer, ^X-^U appears to have no effect. So, if you use undo and nothing happens when an undo step is requested, this may well be because a non-buffer-modifying command was executed.
The undo function maintains a database of changes made to buffers. This database has a finite size; if you attempt to undo more than is contained in the database, undo will issue the following message:
I can't undo that. What's done is done.
Note that, when you undo changes, you can redo the changes by using undo again. For example, if you undo too many changes, use ^X-^U again to undo the unwanted undos.
Advanced Editing Facilities
This chapter describes Emacs' advanced editing facilities. The following features are covered:
- Extended Commands and Functions describes how Emacs extended commands work.
- Moving Up and Down Levels describes how to move up and down recursive editing levels.
- The Mark and the Region describes the Emacs mark and region .
- Deleting and Moving Text shows how to move regions of text around.
- Commands for Fixing Typos shows how to correct typographical errors in text.
- Advanced File Handling describes advanced file-handling features.
- Controlling the Display describes how to control the Emacs display.
- Using Multiple Buffers describes how to control multiple buffers inside the same Emacs.
- Using Multiple Windows describes how to handle multiple windows.
- Narrowing describes the how to limit the range of text editing commands.
- Searching describes Emacs' normal and regular expression searching facilities.
- Replacement Commands describes Emacs text replacement commands.
- Abbreviated Input describes how to use Abbreviation Mode to handle abbreviated input.
- Keyboard Macros describes how to create and execute keyboard macros.
Not all Emacs commands are of the one- or two-character variety. All commands have long names composed of English words separated with dashes. This is because the long names are easier to remember and are more suggestive of the command actions than the one- or two-character short forms.
The commands with long names are known as extended commands or functions because they extend the set of one-character and two-character commands.
Issuing Extended Commands
ESC-x (execute-extended-command)
Begins an extended command.
Extended commands are also called ESC-x commands, because they all start with the characters ESC-x. ESC-x is followed by the command's long, suggestive name (this is actually the name of the function to be called). Terminate the name of the function with a Return. For example, ESC-x delete-white-space Return calls the function delete-white-space.
We say that ESC-x FOO calls the function FOO. When documenting the individual extended commands, we call them functions to avoid confusion between them and the one-character or two-character commands. We also use ESC-x as a title for functions. The extended command is what you type, starting with ESC-x, and what the command does is call a function.
There are a great many functions in Emacs for you to call. They are described throughout this manual, according to what they do. This section is concerned only with extended commands in general.
typing the command name
When you type ESC-x, the cursor moves down to the Mini-buffer at the bottom of the screen. A colon is printed there, and when you type the command name it is echoed. You can use all the usual editing commands while in the Mini-buffer, except for the following:
- All the commands that are usually bound to Escape are now bound to ^\.
- A ^G cancels the whole ESC-x command.
These editing characters apply any time Emacs reads a line in the Mini-buffer, not just within ESC-x.
The colon which appears in the Mini-buffer is called a prompt. The prompt always tells you what sort of argument is required and what it is going to be used for; a colon means that you are using the command ESC-x, and that you should type the name of a function to be called.
command completion
You can abbreviate the name of the command, typing only as much as is needed to identify the command unambiguously. This is knows as command completion .
Using completion, you need type only part of the command name, and Emacs can then visibly fill in the rest, or as much as can be determined from the part you have typed.
You request completion by typing an Escape (or Do on Digital LK201 keyboards). For example, if you type ESC-x del Do the del expands to delete- because all command names that start with del continue with ete-.
If you ask for completion when there are several alternatives for the next character, a help window is displayed to show you the alternatives you can select. This window automatically disappears when the information is no longer relevant.
The Escape or Do keys are also used to terminate the command name and begin the arguments, but this only happens if the command name completes in full. In practice, the two meanings of Escape and Do do not conflict, because if the command name is not complete, it is ambiguous and Emacs does not accept it.
Typing the Help key in the middle of the command name prints a list of all the command names which begin with the text you have typed so far. You can then go on typing the name. If you type Help before typing any other commands, Emacs fills the help window with the names of all the available functions. The help window will be removed from the screen when the command name has been completely entered.
The default completion character is Escape and the default help character is a question mark (?).
numeric and string arguments
Some functions can use numeric prefix arguments. Simply give the ESC-x command an argument and ESC-x will pass it along to the function which it calls.
Some functions require string arguments (sometimes called suffix arguments ). When a function's name has been completely filled in, the function will prompt for arguments. If information is to be selected from a list of alternatives, then completion will be enabled for those functions. Arguments should be separated with a Return when complete.
For example, the function describe-command prints the full documentation of a function whose name must be given as a string argument. Using completion to enter the command, you might type:
ESC-x desc Escape com Escape print-d Escape
to display information about the print-default function.
Moving Up and Down Levels
Subsystems and recursive editing levels are two states in which you are temporarily doing something other than editing the visited files as usual. For example, you might be editing a word's definition for the ESC-x describe-word-in-buffer command, or looking at a documentation file with INFO. Running another subprocess under Emacs can also be thought of as a sublevel.
subsystems
A subsystem is an Emacs function which is an interactive program in its own right: it reads commands in a language of its own, and displays the results. You enter a subsystem by typing an Emacs command which invokes it, or by invoking it directly from DCL. Once entered, the subsystem runs until a specific command to exit the subsystem is typed. An example of an Emacs subsystem is INFO, the documentation reading program.
The commands understood by a subsystem are not like normal Emacs commands, because their purpose is something other than editing text. For example, INFO commands are designed for moving around in a tree-structured documentation file. In Emacs, most commands are control or escape sequences because printing characters insert themselves. In most subsystems, there is no insertion of text, so non-control characters can act as commands.
While you are inside a subsystem, the mode line usually gives the name of the subsystem, as well as other information optionally supplied by the subsystem. For example, INFO displays the current file name and node name.
Since each subsystem implements its own commands, we cannot guarantee anything about these commands. However, there are conventions as to the action of certain commands:
Scroll backward, like ESC-v in Emacs. | Backspace |
Scroll forward, like ^V in Emacs. | Space |
Exit from the subsystem. | q |
Begin an extended command, like ESC-x in Emacs. | x |
Display help on the subsystem's commands. | ? |
Note that not all of these commands necessarily exist in every subsystem. Try the ? key to see which commands a subsystem does define.
recursive editing levels
A recursive editing level is a state in which part of the execution of one command involves doing some editing. You may be editing the file you are working on, or you may be editing something totally different from what you were working on at top level. Recursive editing levels are indicated in the mode line by square brackets.
For example, the command ^U-^X-^D calls the function describe-word-in-buffer and lets you edit an existing word's description. During the execution of this command, you enter a recursive editing level in which you alter or create the new definition. While this is going on, the mode line contains [Edit description word: word xxx] to inform you that you are in a recursive editing level. When you exit the recursive editing level, the describe-word-in-buffer command continues its work by inserting the entry into the subr-names database. Only then is the original ESC-x describe-word-in-buffer command finished.
Alternatively, you can abort the recursive editing level, which gets you out of the ESC-x describe-word-in-buffer command and back to top level, without allowing the command to finish.
A recursive editing level differs from a subsystem in that the commands are ordinary Emacs commands (although a few may be slightly modified), whereas a subsystem defines its own commands. The text you edit inside a recursive editing level depends on the command which invoked the recursive editing level.
exiting levels and emacs
Exit from Emacs or from a recursive editing level. | ^C |
Return to the top level, aborting all recursive edits, packages and subsystems. | ESC-x top-level |
Temporarily returns to the parent process, if there is one. The paused Emacs may be re-started at a later time. | ESC-x pause-emacs |
The general Emacs exit command is ^C. This command is used to exit from a recursive editing level back to the top level of Emacs, and to exit from Emacs at top level.
We cannot say in general how to exit a subsystem, since each subsystem defines its own command language. The convention is to use the Q key.
You can exit from Emacs back to the parent process at any time (even from within a recursive editing level) using the ESC-x pause-emacs command. If you use this command while inside a recursive editing level, when Emacs is re-entered you will still be inside the recursive editing level.
Exiting Emacs does not normally save visited files, because it is not always true that you exit Emacs only when you have finished editing. If you want files saved, you must use ^X-^M before exiting. However, if modified buffers exist when you exit, Emacs will ask you to confirm that you wish to discard the edits you have made.
Several variables are provided that allow you to modify the default action when you enter and exit Emacs. These are:
To do this... | ...set this variable |
Execute a function to be executed when Emacs is initially entered. | enter-emacs-hook |
Execute a function when Emacs finally exits. | exit-emacs-hook |
Execute a function when Emacs re-starts after an ESC-x pause-emacs command. | return-to-emacs-hook |
Executed a function when ESC-x pause-emacs is used. | leave-emacs-hook |
interactive dcl subprocesses
An alternative to exiting Emacs or using ESC-x pause-emacs is to create another subprocess under Emacs. You can probably do in a subprocess whatever you would have done after exiting Emacs, and the subprocess will not interfere with Emacs' operation. Use the ESC-x return-to-monitor command to start a subprocess, then use the DCL LOGOUT command to return to Emacs.
All symbols and un-confined process logical names are propagated to the subprocess.
The Mark and the Region
In general, a command which processes an arbitrary part of the buffer must know where to start and where to stop. In Emacs, such commands usually operate on the text between dot and mark -- this range of text is called the region . To specify a region, you position dot at one end of the region, and set mark at the other end. It does not matter which end is positioned first, or which end comes earlier in the text. Here are some commands for manipulating the mark:
Sets the mark at the location of dot. | ^@ or Select (set-mark) |
Exchange dot and mark. | ^X-^X (exchange-dot-and-mark) |
Set dot and mark around the most recently found string or string partition. | ESC-x region-around-match |
Delete the current mark. | ^X-^@ (unset-mark) |
For example, if you want to convert part of the buffer to upper-case, you can use the ESC-x case-region-upper command, which operates on the text in the region. You first go to the beginning of the text to be altered, set the mark there, move to the end, and then type ESC-x case-region-upper. Or, you can set the mark at the end of the text, move to the beginning, and then type ESC-x case-region-upper.
The most common way to set the mark is with the ^@ or Select command. This sets the mark at the current location of dot. You can then move dot away, leaving the mark behind. The easiest way to type the ^@ command on Digital keyboards is to press and hold the Ctrl key and simultaneously press the Space bar.
Emacs allows you to see the currently selected region by highlighting the text between the mark and dot in buffers with the highlight-region variable set.
You can always see where the mark is with the command ^X-^X which puts the mark where dot is and dot where the mark was. The extent of the region is unchanged, but the cursor and dot are now at the previous location of the mark.
^X-^X is also useful when you are satisfied with the location of dot but want to move the mark; type ^X-^X to put dot where the mark currently is, then you can move it. A second use of ^X-^X, if necessary, puts the mark at the new location with dot back at its original position.
If you have set a mark and wish to remove it, use the command unset-mark by typing ^X-^@. The mark is removed, and dot remains unchanged. This is useful where a highlighted region may distract you when you have finished with it.
Deleting and Moving Text
The commonest way to move or copy text in Emacs is to delete it, and then restore it in one or more places.
text deletion
Most commands which erase text from the buffer save it somewhere so that you can retrieve it if you change your mind, or if you want to move or copy it to other parts of the buffer. The delete commands that do not save the text include ^D and Delete, which delete only one character at a time, and those commands that delete only spaces or line separators. Commands that can destroy significant amounts of non-trivial data generally save the text. If you use any delete command by mistake, you can use the ^X-^U command to undo it.
The text deletion commands are:
Delete the next character. | ^D (delete-next-character) |
Delete the previous character. | Delete (delete-previous-character) |
Delete the rest of the line or one or more lines. | ^K (kill-to-end-of-line) |
Delete the region. | ^W (delete-to-killbuffer) |
Delete the next word. | ESC-d (delete-next-word) |
Delete the previous word. | ESC-h (delete-previous-word) |
The most basic delete commands are ^D and Delete. ^D deletes the character the cursor is over. The cursor does not move. Delete deletes the character before the cursor, and moves the cursor back one character. Line separators act like single characters when they are deleted.
The ^W command is a general delete command. This command deletes everything between dot and mark. With this command, you can delete any contiguous characters if you first set the mark at one end of the character string, then move to the other end and use ^W. The deleted text is saved in a buffer called Kill buffer.
The simplest delete command is ^K. If given at the beginning of a line, it deletes all the text on the line leaving it blank. If given on a blank line, the blank line is deleted. As a consequence, if you go to the front of a non-blank line and type two ^K s, the line disappears completely.
More generally, ^K deletes from dot up to the end of the current line, unless it is at the end of a line. When dot is at the end of a line, ^K deletes the line separator following the line, thus joining the next line with the current line.
^K moves the text it deletes into Kill buffer. This is the same buffer that ^W uses, so ^K and ^W cannot be used at the same time as each command erases the data in Kill buffer first. However, successive uses of the ^K command add the newly-deleted data to the end of Kill buffer (the command only erases Kill buffer when the previous command was not a ^K). So, if you wish to move a region to another place in the buffer without disturbing dot, use a number of ^K s, or provide the ^K command with a prefix argument.
un-deleting text
Insert the last-deleted text. | ^Y (yank-from-killbuffer) |
Undeleting is retrieving text which has been deleted. The usual way to move or copy text is to delete it, then undelete it one or more times.
The ^Y command inserts the entire contents of Kill buffer into the current buffer at the location of dot.
other ways of copying text
Text is usually copied or moved by first deleting it and then undeleting it. There are other methods that are useful for copying one block of text to many places, or for copying many scattered blocks of text into one place. You can accumulate blocks of text from scattered locations either into a buffer or into a file.
To append blocks of text into a buffer, use the command ESC-x append-region-to-buffer, which inserts a copy of the region into the specified buffer at the location of dot in that buffer. If there is no buffer with the name you specify, the buffer is created.
Dot in the specified buffer is left at the end of the copied text, so successive uses of append-region-to-buffer accumulate the text in the specified buffer.
You can retrieve the accumulated text from that buffer with ESC-x yank-buffer. This inserts a copy of the text in the specified buffer into the current buffer. You can also select the other buffer for editing.
Rather than accumulating text in an Emacs buffer, you can append text directly to a disk file with the command ESC-x append-to-file. This command adds the text of the region to the end of the specified file. The file is modified immediately on disk. This command is normally used with files that are not being visited in Emacs.
Commands for Fixing Typos
This section describes commands that are especially useful when you catch a mistake in your text just after you have made it, or change your mind while composing.
Delete the previous character. | Delete (delete-previous-character) |
Delete the previous word. | ESC-h (delete-previous-word) |
Transpose the last two characters. | ^T (transpose-characters) |
Undo the last sequence of commands that affected buffers. | ^X-^U (undo) |
deleting your mistakes
The Delete command is the most important correction command. When used with self-inserting characters, it can be thought of as cancelling the last character typed.
When your mistake is longer than a couple of characters, it might be more convenient to use ESC-h. ESC-h deletes back to the start of the last word.
ESC-h is often useful even when you have typed only a few incorrect characters, or if you know you are confused in your typing and are not sure exactly what you typed. In such cases, you cannot correct with Delete except by looking at the screen to see what you did. It requires less thought to delete the whole word and start over again, and is quicker if the system is heavily loaded.
correcting transposition errors
The common error of transposing two characters can be corrected with the ^T command when the characters are adjacent. ^T transposes the two characters preceding dot, so if you catch your transposition error right away, you can correct it with a ^T. If you do not catch it so quickly, you must move the cursor back to the two transposed characters. If you transposed a space with the last character of the word before it, the word motion commands are a good way of moving to the correct position.
case conversion
A very common error is to type words in the wrong case. As this is such a common error, the word case-conversion commands ESC-l, ESC-u and ESC-^ have a special feature: they do not move the cursor. As soon as you see you have mis-typed the last word, you can simply change its case and go on. See Case Conversion Commands for details on using the case-conversion commands.
Advanced File Handling
The basic unit of stored data is the file. Each document lives in its own file. To edit a document, you must tell Emacs the name of the file that contains it. This is called visiting the file. To make your changes to the file permanent on disk, you must save the file. Emacs also has facilities for deleting files conveniently.
For convenience, all file operation commands are prefixed with ^X.
visiting files
Visit a file. | ^X-^V (visit-file) |
Save the current file to disk. | ^X-^s (write-current-file) |
Visiting a file means copying the file into Emacs where you can edit it. Emacs remembers the name of the file you visited. When you visit a file, a new buffer is created for the file; the buffer name is derived from the file name you visited. When you visit a file, the buffer containing the file is displayed in one of the windows on the screen. If only a small number of windows are currently displayed, a new window may be created for the buffer. The fully-expanded name of the file you are visiting is visible in the mode line when you are at top level.
The changes you make with Emacs are made to a copy of the file inside Emacs, not to the file itself. The changed text is not made permanent until you save it in a file. The first time you change the text, an asterisk appears in the mode line. This indicates that the text contains fresh changes which will be lost unless you save them.
To visit a file, use the command ^X-^V. Follow the command with the name of the file you wish to visit, terminated by a Return. You can abort the command while it is asking for a file name by typing ^G; you can also edit the filename with the standard editing commands. File name completion is enabled. See Command Completion for more information on name completion.
When you wish to save the file and make your changes permanent, type ^X-^s. After the save is finished, ^X-^s prints Wrote: filename in the Mini-buffer, where filename is the name of the file written.
If you want to create a file, just visit it. Emacs prints New File: filename in the Mini-buffer, but apart from that, behaves exactly as if you had visited an existing empty file. If you make any changes and save them, the file is created. If you visit a non-existent file unintentionally (because you typed the wrong file name), go ahead and visit the file you meant to visit. If you do not modify or save the unwanted file, it is not created.
If you visit one file and then visit another which would use the same buffer name, Emacs asks you what buffer to use for the new file. You can answer with a Return, and the old buffer's contents will be destroyed, or you can type the name of a buffer to use.
You can also set the variable ask-about-buffer-names so that Emacs automatically appends a sequence number to the buffer used. For example, if you visit file A.B, Emacs creates buffer a.b. With ask-about-buffer-names set to 1, if you then visit file SYS$SYSTEM:A.B, Emacs will use buffer a.b<2>), and so on.
miscellaneous file operations
Emacs has extended commands for performing many other operations on files.
ESC-x view-file
Scans or reads a file by sequential screensful without visiting the file. It enters a subsystem in which you type a Space to see the next screenful, or a Backspace to see the previous screenful. Typing any other character exits the command.
Write the contents of the current buffer into the file specified, and then visits that file. This can be thought of as a way of changing the name of the file you are visiting. | ^X-^W (write-named-file) |
Insert the contents of the specified file into the buffer at dot, leaving dot at the beginning of the inserted text. | ^X-^I (insert-file) |
Append the current buffer to the end of a nominated file. | ESC-x append-to-file |
Delete the specified file. | ESC-x unlink-file |
Write all file buffers that have been modified since they were last visited and deletes the appropriate checkpoint and/or journal files. | ^X-^M (write-modified-files) |
Write all file buffers that have been modified and then exits Emacs (or a recursive edit level). | ^X-^F (write-file-exit) |
The variable current-buffer-file-name contains the name of the file to which the current buffer is associated (if there is one). You can change the name by setting this variable to the required file name.
protection against disasters
Emacs offers two features that protect against losing changes to files that you are editing. These features are called checkpointing and journalling.
When checkpointing is enabled, Emacs saves your buffers from time-to-time when a certain number of keystrokes have been made. The buffers are written automatically, but you can also force Emacs to checkpoint at any time you wish. Checkpointing prevents you from losing more than a limited amount of work when a disaster occurs.
When journalling is enabled, Emacs saves all the insertions and deletions made to your buffers in a journal file . The changes are saved to the journal file at the same time as you make them to the proper file.
Each method has its advantages and disadvantages. You will notice that it can take a long time to write the checkpoint file for a large file, since Emacs writes out the whole file every time it checkpoints. On the other hand, journalling only writes out the changes that you have made, which is much faster.
Journalling is at a disadvantage when you use complex commands which make many changes to the buffer. When this happens, many journal records must be written to the journal file.
If you want Emacs to checkpoint, you must set the variable checkpoint-frequency to the number of keystrokes you want to strike between checkpoints. If checkpoint-frequency is zero, checkpointing is disabled for all buffers. By default checkpointing is set to occur every 300 keystrokes.
Each time you visit a file, no matter how, checkpointing is turned on for the created buffer if checkpoint-frequency is non-zero. Once you have visited a file, you can turn checkpointing on or off by setting the variable current-buffer-checkpointable to 1 or 0. You can tell if a buffer has checkpointing switched on and if checkpointing is enabled as the minor mode string Checkpoint will be displayed in the buffer's mode line.
If a checkpoint occurs while the activity indicator is being used, the checkpoint activity is indicated by a c in the bottom-left hand corner of the screen. When the checkpoint is complete, this will change back to the character displayed before the checkpoint started.
When a checkpoint occurs and the activity indicator is switched off, Checkpointing... is printed in the Mini-buffer. When all modified files have been checkpointed, Emacs displays Checkpointing... done. If an error is detected during the checkpoint, error. is displayed instead of done.
The action of saving each modified buffer occurs because the default checkpoint action is to call the function checkpoint-buffers. When checkpointing runs, it calls the function whose name is in the variable checkpoint-hook. The default value for checkpoint-hook is the function checkpoint-buffers.
checkpoint-buffers writes each buffer to its own checkpoint file. The file used has the same file name as the buffer name with an extension of .CKP All checkpoint files are written to the default directory by default. For non-file buffers, the buffer name is used and the extension .CKP is added to make a file name; if this results in a file name with incorrect syntax, CHECKPNT.CKP is used as the file name.
If you do not want checkpoint files written to the default directory, you can define the logical name Emacs_CHECKPOINT as the location for your checkpoint files. If this logical name is defined before Emacs is started, Emacs will write all checkpoint files to this directory rather than to the default directory.
When it is time to recover files after a system crash, simply rename the checkpoint files to their original file names.
Emacs deletes a checkpoint file whenever the proper file is saved. It also deletes checkpoint files when Emacs exits if the variable unlink-checkpoint-files is set.
If you want Emacs to journal, you must set the variable journal-frequency to the number of seconds you want Emacs to wait before writing out journal records. If journal-frequency is zero, journalling is disabled for all buffers. When Emacs starts up, checkpointing is enabled and journalling is disabled.
Each time you visit a file, no matter how, journalling is turned on for the created buffer if journal-frequency is non-zero. Once you have visited a file, you can turn journalling on or off by setting the current-buffer-journalled variable. You can tell if a buffer has journalling switched on and whether journalling is enabled as the mode line for the buffer will contain the minor mode string Journal.
If journalling is writing to the journal file and the activity indicator is being used, the journal activity is indicated by a j in the bottom left hand corner of the screen. When the journalling is complete, this will change back to the character displayed before the journalling started. However, it is rare that you will see the j, because journalling is very fast.
When it is time to recover files after a system crash, simply use the journal-recover command, specifying the name of the journal file to recover.
All the journal files that Emacs writes are kept in the directory Emacs_JOURNAL: For each buffer that had journalling enabled and had not been saved, there will be a journal file in Emacs_JOURNAL:.
For buffers with associated files, the journal name will be based on the file name. For example, the file LOGIN.COM will have a journal file named LOGIN.COM_Emacs_JOURNAL.
For buffers without associated files, the journal has a slightly different name. For example, the buffer Kill buffer will have a journal called KILL_BUFFER.BUFFER_Emacs_JOURNAL0000.
journal-recover prompts for the name of the journal file, which must be in Emacs_JOURNAL:. Emacs will check that the journal file is valid before attempting to apply it. For a file, Emacs checks that the version number of the file is the same as the journal was recorded for. For a buffer, Emacs checks that the buffer does not exist. These checks ensure that using journal-recover will not damage your data.
Emacs deletes a journal file whenever the proper file is saved. Journal files are also deleted when Emacs exits.
editing files with different line endings
Each operating systems has different end-of-line delimiters for text files. Emacs supports the end-of-line styles for Unix, Windows and Machintosh.
Emacs, by default, will save a file using the same end-of-line style it had when read in.
The end-of-line style is displayed in the status bar. The indicator will be:
- CRLF - for Windows and MSDOS text files
- LF - for Unix (Linux) text files
- CR - for Machintosh text files
- The indicator will be blank for files that Emacs detects as binary.
Emacs uses the following variables to support the editing of files:
current-buffer-end-of-line-style | buffer-specific variable containing a string value describing the end-of-line style of the file associated with the current buffer. |
default-buffer-end-of-line-style | contains a string value indicating the record format in which new files written by Emacs should be created. |
override-end-of-line-style | contains a string value describing the format in which to write out all new files, overriding the setting of current-buffer-end-of-line-style. The default value is to not override. |
Every buffer created by Emacs is assigned the buffer-specific variable current-buffer-end-of-line-style. This variable contains a string value which describes the end-of-file style of the file associated with each buffer. When Emacs visits an existing file, it determines the file's end-of-line style and sets the current-buffer-end-of-line-style variable accordingly. The values that this variable can take are described below.
File Type | current-buffer-end-of-line-style |
---|---|
Windows and MS-DOS text files | crlf |
Unix (Linux) text files | lf |
Macintosh text files | cr |
Binary files | binary |
When Emacs writes a buffer out to a file, the new file is created with the end-of-line style described by the current-buffer-end-of-line-style variable.
When you visit a new file, Emacs uses the default-buffer-end-of-line-style variable to set the equivalent buffer-specific variable. The default value is appropiate for your operating system.
You can override the setting of current-buffer-end-of-line-style and force files to be written in a particular style by setting the override-end-of-line-style variable. The default setting for this variable is none, which means do not override. You can set override-end-of-line-style to any of the supported end-of-line styles listed above. For example, to force all files to be written in lf format, use the following code:
(setq override-end-of-line-style "lf")
Controlling the Display
Since only part of a large buffer can fit on the screen, Emacs tries to show the part of the buffer that is likely to be most interesting. The display control commands allow you to tell Emacs that you want to see a different part of the buffer.
Clear and re-displays the screen. | ^L (redraw-display) |
Scroll forward one line. | ^Z (scroll-one-line-up) |
Scroll backward one line. | ESC-z (scroll-one-line-down) |
Move the line in which dot is located to the top of the current window. | ESC-! (line-to-top-of-window) |
Scroll one column from the left-hand side of the screen. | ESC-x scroll-one-column-left |
Scroll one column from the right-hand side of the screen. | ESC-x scroll-one-column-right |
Scroll the current column to the left of the screen. | ESC-@ (column-to-left-of-window) |
Move to the next page. | ^V or Next Screen (next-page) |
Move back a page. | ESC-v or Prev Screen (previous-page) |
The terminal screen is rarely large enough to display all of your file. If the whole buffer does not fit on the screen, Emacs displays a contiguous portion of the buffer that always contains dot. Emacs continues to display approximately the same portion of the buffer until dot moves outside of the displayed portion; Emacs then chooses a new portion centred around the new location of dot. This is Emacs' guess as to what you are most interested in seeing. If Emacs' guess is wrong, you can use the display control commands to see a different portion. The finite area of screen through which you can see part of the buffer is called a window . Emacs can display many windows on the screen; the maximum number of displayable windows is determined by the number of lines on the screen.
When Emacs chooses a new window position, it always tries to place dot half-way down the buffer's window. This is controlled by the variable scroll-step, whose value is the number of lines to automatically move the window. However, if the end of the buffer is on the screen, Emacs tries to leave as little as possible of the screen blank beneath it, so that the screen space is not wasted.
The basic display control command is ^L. This command clears the screen and re-draws it from internal information.
The vertical scrolling commands ^Z and ESC-z let you move a whole window up or down a few lines. ^Z with an argument shows you the specified number of lines at the bottom of the window, moving the text but not moving dot unless it moves out of the window. ^Z with a negative argument shows you more lines at the top of the window, as does ESC-z with a positive argument.
The horizontal scrolling commands let you move the whole window left or right by a few columns. This lets you view buffers with lines that are longer than your screen's width. The most useful of these commands is column-to-left-of-window. You can move dot in a line until you decide where the new left-hand side of the screen should be, then use the ESC-@ command to move the window to the selected column. When you want the window back to the left side of the buffer, go to the beginning of the line with ^A and use column-to-left-of-window again.
To read the buffer a window-full at a time, use the ^V or Next Screen command with no argument. This moves the start of the window 4/5th's of a screen further down the buffer. Dot will remain where it was unless it moves out of the window. Thus, each ^V command displays the next window-full, except for two lines of overlap to provide continuity. To move backward, use ESC-v without an argument, which moves a window-full backwards.
Scanning by screen-fulls through the buffer for some distance is most conveniently done with the ESC-x view-buffer command. This command enters a simple subsystem in which Space moves a screen-full forward and Backspace moves a screen-full backward. The Return character exits, leaving dot centred in whatever part of the buffer was visible. Any other character exits and returns dot to its former location, and is then executed as a command. view-buffer can be used to view another buffer by giving the buffer's name as a string argument.
Using Multiple Buffers
Emacs can contain a large number of buffers, each containing text. At any time, only one buffer can be selected and available for editing, but it is not difficult to switch to a different buffer. Each buffer individually remembers which file it is visiting, which modes are enabled, and whether there are any changes that need saving. Buffer manipulation commands are all prefixed with ^X.
Select or create a buffer in the current window. | ^X-b (switch-to-buffer) |
List the existing buffers. | ^X-^B (list-buffers) |
Delete the nominated buffer. | ESC-x delete-buffer |
Switch to the specified buffer using a different window. | ESC-x pop-to-buffer |
Each buffer in Emacs has a single name which normally does not change. A buffer's name can be any length. The name of a buffer, and the name of the file visited in it, are visible in the mode line when you are at top level.
creating and selecting buffers
To create a new buffer, you need only think of a name for it (say, FOO) and then type ^X-b FOO which is the command ^X-b followed by the buffer name. This makes a new, empty buffer and selects it for editing. The new buffer is not visiting any file and is called a scratch buffer. Each buffer has its own major mode; a new buffer's major mode is always Normal Mode.
To return to buffer FOO later after having switched to another buffer, the same command ^X-b FOO is used, since ^X-b can tell whether or not a buffer named FOO already exists.
^X-b changes the buffer associated with the current window. You can use ESC-x pop-to-buffer to use another window and select a new buffer.
using existing buffers
To get a list of all existing buffers, type ^X-^B. Each buffer's name, size in characters, buffer type (Scr for scratch, File for file and Macro for macro), major and minor modes, and visited file are all displayed. An M indicates a modified buffer which contains changes that have not been saved. C, A and R indicate that checkpointing, Abbreviation Mode and replace mode are in effect for the buffer.
The variable current-buffer-name contains the name of the currently selected buffer, and can be set to change the name of the buffer. Name collisions are detected and an error message is generated when such collisions occur.
The commands ESC-x append-region-to-buffer and ESC-x yank-buffer can be used to copy text from one buffer to another. See Deleting and Moving Text for details on deleting and moving text.
deleting buffers
After you use an Emacs session for a while, it may fill up with buffers which you no longer need. Eventually, you can reach a point where trying to create any more buffers results in an error message.
You can delete buffers using the ESC-x delete-buffer command. If you delete the current buffer in any way, Emacs will automatically select a new buffer as the current buffer. If other buffers are displayed on the screen, Emacs will select one of these buffers and will make it the current buffer. If you delete the last exisiting buffer, Emacs will automatically create a buffer called main and will make main the current buffer.
If you attempt to delete a buffer that has un-saved modifications, Emacs asks you to verify that you wish to discard the changes you have made.
Using Multiple Windows
Emacs allows you to split the screen into many windows and use them to display parts of any buffer. The number of windows that can be fitted on the screen is dependent on the number of lines on the displaying terminal. Each window must be at least two lines long, one line for the text portion and the other line for the window's mode line. So, for a 24-line VT220 terminal, the maximum number of displayable windows is 11.
The following commands are available for manipulating windows:
Split the current window horizontally into two windows. | ^X-2 (split-current-window) |
Split the current window vertically into two windows. | ^X-3 (split-current-window-vertically) |
Remove all other windows. | ^X-1 (delete-other-windows) |
Move dot to the window below the current window. | ^X-n (next-window) |
Move dot to the window above the current window. | ^X-p (previous-window) |
Enlarge the current window. | ^X-z (enlarge-window) |
Shrink the current window. | ^X-^Z (shrink-window) |
Reduce the width of the current window. | ESC-x narrow-window |
Increase the width of the current window. | ESC-x widen-window |
Delete the current window. | ^X-d (delete-window) |
Scroll the window below the current window. | ESC-^V (page-next-window) |
The command ^X-2 divides the current window into two equally-sized windows. The two windows will be connected to the same buffer, so that the text of that buffer around dot will be displayed in both windows.
^X-3 also divides the current window into two equally-sized windows, but the two windows are placed side-by-side instead of one above the other as is done by ^X-2.
To return to viewing only one window, use the command ^X-1. The current window expands to fill the whole screen, and all other windows disappear.
^X-d deletes the current window and re-positions dot in the window directly above. If only one window is on the screen, buffer Main is popped onto the screen.
While you have more than one window displayed on the screen, you can use ^X-n and ^X-p to switch between windows. If you attempt to move to the next window from the last window on the screen, Emacs considers the next window to be the window at the top of the screen. Similarly, if you attempt to move to the previous window when dot is in the top window, Emacs moves dot to the bottom window.
If the current buffer and window is the Mini-buffer at the bottom of the screen, then this window, too, is considered to be in the ring of windows displayed on the screen.
Often you will be editing one window while using another just for reference. The command ESC-^V is very useful in such cases -- it scrolls the next window without switching to it and switching back. It scrolls the same way ^V does: with no argument, 4/5th's of the window up; with an argument n, n pages up (or down when n is negative).
When you split a window, the space for the old window will be divided evenly between the two windows. You can redistribute screen space between the windows with the ^X-z and ^X-^Z commands. These make the currently selected window become one line bigger or smaller respectively (or as many lines as is specified with a numeric argument).
Narrowing
Narrowing means focussing-in on one portion of a buffer, making the rest of the buffer temporarily invisible and inaccessible.
Narrow down to the region between dot and mark. | ESC-x narrow-region |
Widens to view the entire buffer. | ESC-x widen-region |
When you have narrowed down to a part of the buffer, the narrowed part appears to be all there is. You cannot see the rest of the buffer; you cannot move into it, since motion commands will not move outside of the narrowed part; you cannot change it in any way. However, it has not been deleted, and if you save the file, all the invisible text will be saved.
Narrowing can be used to concentrate on a single subroutine or paragraph by eliminating clutter. Narrowing is also useful when you want to restrict the range of operation of commands such as replace commands.
The primary narrowing command is ESC-x narrow-region. It sets the virtual buffer boundaries at dot and mark, so that only the text that was between dot and mark remains accessible. Dot and mark do not change.
Use the ESC-x widen-region command to remove the narrowing. This makes all the text in the buffer accessible again.
Searching
Like other text editors, Emacs has commands that search for an occurrence of a text string. The Emacs search commands are unusual in that they can be normal text searches or regular expression pattern matches.
Regular expression searching allows complex patterns to be specified and searched for, with the ability to partition matched strings for later reference and substitution.
The ordinary searching commands are:
Search forward. | ^S (search-forward) |
Search backward. | ^R (search-reverse) |
^S prompts for a string, and search through the buffer starting at dot for the specified string. If no string is specified, search-forward searches for the next occurrence of the last search string.
Generally, Emacs looks for the string exactly as you typed it. So, if you search for FOO then FOO in the text will match, but Foo or foo will not. However, if you set the variable case-fold-search to 1, all searches will make upper- and lowercase letters appear the same.
regular expression searching
Regular expression searching provides a powerful pattern-matching facility with optional partitioning of the pattern for later substitution.
The regular expression search commands are:
Search for a pattern match forwards. | ESC-x ere-search-forward |
Search for a pattern match backwards. | ESC-x ere-search-reverse |
Both these commands expect an extended regular expression pattern for which to search. The pattern is interpreted and matched against text according to the following rules:
- Any character except a special character matches itself. Special characters are \, [, (, ), {, }, |, ., *, +, ? and sometimes ^, and $.
- A . matches any character except newline.
- A \ followed by any character except those mentioned in the following rules matches that character.
- A \w matches any word character, as defined in the current buffer's syntax table.
- A \W matches any non-word character, as defined in the current buffer's syntax table.
- A \b matches at a boundary between a word and non-word characters, as defined in the current buffer's syntax table.
- A \B matches anywhere but at a boundary between a word and non-word characters, as defined in the current buffer's syntax table.
- A \< matches at the beginning of a word.
- A \> matches at the end of a word.
- A \s matches a white-space character (Space and TAB).
- A \S matches any non-white-space character (Space and TAB).
- A \d matches any digit (0 to 9).
- A \D matches any non-digit (0 to 9).
- A non-empty string s bracketed [s] (or [^s]) matches any character in (or not in) s. In s ] may only appear as the first letter. A subrange a-z, with a and z in ascending ASCII order, stands for the inclusive range of ASCII characters. \s, \d and \w add white-space, digits and word-characters to the set.
- A \ followed by one or two digits n matches a copy of the string that the bracketed regular expression beginning with the n th ( matched.
- A regular expression of one of the preceding forms followed by:
- * matches the longest sequence of zero or more matches of the regular expression.
- + matches the longest sequence of one or more matches of the regular expression.
- ? matches a sequence of zero or one match of the regular expression.
- *? matches the shortest sequence of zero or more matches of the regular expression.
- +? matches the shortest sequence of one or more matches of the regular expression.
- {n} matches exactly n matches of the regular expression.
- {n,m} matches the longest sequence of atleast n but no more then m matches of the regular expression.
- {n,m}? matches the shortest sequence of atleast n but no more then m matches of the regular expression.
- {n,} matches the longest sequence of n or more matches of the regular expression.
- {n,}? matches the shortest sequence of n or more matches of the regular expression.
- A sequence of regular expressions of one of the preceding forms separated by | matches any one of the regular expressions.
- A regular expression, x, bracketed (x) matches what x matches. The matched string is available for use in rule 15, as insertion text in regular expression replace commands, or as an argument to the region-around-match function. The maximum number of partitioned strings is 99.
- (?%...) - ... is a list of options (not yet implemented).
- A regular expression, x, bracketed (?P<name>x) matches what x matches and allows later reference to the matched string by name.
- (?P=name) matches whatever the group named name matched.
- The expression (?#comment) is ignored and allows comments to be embedded in complex regular expressions.
- A regular expression, x, bracketed (?=x) matches if x matches but does not move dot forward.
- (?!x) matches if x does not matche but does not move dot forward.
- A regular expression, x, bracketed (:x) matches what x matches. The match string is not remembered for back referencing.
- A regular expression of this or one of the preceding forms, x, followed by a regular expression of one of the preceding forms, y, matches a match for x followed by a match for y with the match for x being as long as possible while still permitting a y match.
- A regular expression of one of the preceding forms preceded by ^ is constrained to matches that begin at the left-hand end of a line.
- A regular expression of one of the preceding forms followed by $ is constrained to matches that end at the right-hand end of a line.
- A regular expression of one of the preceding forms picks out the longest amongst the left-most matches if searching forward or the right-most if searching backward.
- An empty regular expression stands for a copy of the last regular expression encountered.
The function ere-looking-at returns 1 or 0 depending on whether the specified regular expression follows dot.
Note: The functions re-search-forward, re-search-reverse and looking-at implement a less powerful regular expression language that is deprecated in favor of the simpler to use and more powerful ere-search-forward, ere-search-reverse and ere-looking-at functions.
Replacement Commands
Emacs has a normal text replacement function similar to that found in most text editors. Emacs also has a query replace operation which asks you, for each occurrence of the search string, whether or not to replace it. Emacs also allows you to use regular expressions in replace commands.
To replace every instance of FOO after dot with BAR, you can type ESC-r FOO BAR. This runs the function replace-string. Replacement occurs only after dot, so if you want to cover the whole buffer you must go to the beginning of the buffer first. Replacement continues to the end of the buffer, but you can restrict the scope of the replace command using narrowing. See Narrowing for details on narrowing commands.
Unless the variable replace-case is zero, ESC-r tries to preserve case. For example, if you give both foo and bar in lower case, and if a particular FOO is found with a capital initial or all capitalized, the BAR which replaces it will be given the same case pattern. Thus, ESC-r foo bar would replace foo with bar, Foo with Bar and FOO with BAR. If replace-case is zero, the replacement string is inserted with the case you used when you typed it.
If case-fold-search is zero, the string to be replaced is found only when it has the same case as the string you typed.
query replace
If you want to change only some of the occurrences of a string but not all, you cannot use an ordinary replace command. Instead, use ESC-q. This displays each occurrence of the matched pattern and waits for you to confirm whether or not you want to replace it. The characters you can type when you are shown an occurrence of the original string are:
Replaces the string. | Space |
Skips to the next match without replacing this match. | n |
Replaces this match and displays the result. You are then asked for another input character. Since the replacement has already been made, Delete and Space are equivalent. | Comma |
Exits the query replace command without doing any more replacements. | ^G or q |
Replaces this match, then stops the query replace command. | Period |
Replaces all remaining matches without asking for further confirmations. | ! |
Enters a recursive editing level so that you can edit the occurrence rather than just replace it. When you are done, exit the recursive editing level with ^C and the next occurrence will be located. | r |
Any other character solicits a help display to remind you of the available options.
regular expression replace
The Emacs regular expression replace commands are:
Perform a replace operation using regular expression search and replacement strings. | ESC-x ere-replace-string |
Perform a query replace operation using regular expression search and replacement strings. | ESC-x ere-query-replace-string |
An extra feature of the ere-replace-string and ere-query-replace-string commands is the ability to insert portions of the matched string as part of the replacement.
Regular expression search strings may also be partitioned into a number of regular-expression substrings using the ( and ) delimiters. Each of the strings that matches one of the subpatterns may be separately inserted into the replacement text. You specify which match to insert by using \n. n is the position of the string to insert, starting from the left-hand side in the pattern. The first bracketed pattern is designated as string 1. For example, if you search for abc(.*)ghi(.*) and specify a replacement string of \2\1 and apply these regular expression to the text abcdefghijkl, the replacement text will be jkldef.
Note: The functions re-replace-string and re-query-replace-string implement a less powerful regular expression language that is deprecated in favor of the simpler to use and more powerful ere-replace-string and ere-query-replace-string functions.
Abbreviated Input
Abbreviation Mode allows you to abbreviate text with a single word. Emacs expands the abbreviation automatically as soon as you have finished typing the abbreviation.
Abbreviations are also useful for correcting commonly misspelled or mis-typed words (abbreviation thier could expand to their), and for upper-casing words like Emacs (abbreviation emacs could expand to Emacs).
The Abbreviation Mode commands are:
Define an abbreviation and expansion and puts them in the global abbreviation table. | ESC-x define-global-abbrev |
Define an abbreviation and expansion and puts them in the current buffer's local abbreviation table. | ESC-x define-local-abbrev |
Read a file of abbreviations, and merges their definitions into the existing set of abbreviation tables. Duplicate entries over-write existing entries in existing abbreviation tables. | ESC-x read-abbrev-file |
Write the abbreviations defined by all the abbreviation tables into a file for later reading. | ESC-x write-abbrev-file |
Associate a named abbreviation table as the current buffer's local abbreviation table. | ESC-x use-abbrev-table |
Check to see if a string will be expanded, and returns the expansion for the current buffer. | ESC-x test-abbrev-expand |
To use Abbreviation Mode, define a global or local abbreviation using either of the commands ESC-x define-local-abbrev or ESC-x define-global-abbrev. Abbreviation Mode is controlled by a variable called abbrev-mode. When abbrev-mode is non-zero, abbreviations will be expanded. Defining an abbreviation forces abbrev-mode to be set.
In writing this documentation, we could have defined am to be an abbreviation for abbrev mode. After typing just the letters am, we see just that, am; but if we then finish the word by typing space or period or any other punctuation character, the am is replaced by and redisplayed as abbrev mode. If we capitalize the abbreviation, Am, the expansion is capitalized: Abbrev mode. If we capitalize the whole abbreviation, AM, each word in the expansion is capitalized: Abbrev Mode. In this particular example, though, we would define am to expand to Abbrev Mode since it should always to be capitalized that way. With the am abbreviation defined like this, typing In am now would produce In Abbrev Mode now.
Abbreviation Mode does not interfere with the use of major modes, such as Text, Lisp or Pascal; nor does it interfere with other minor modes, such as Auto-fill. Each mode may redefine which functions are bound to characters; this does not affect Abbreviation Mode.
There are two scopes of abbreviation: local and global. A local abbreviation applies only in a buffer which is associated to the named local abbreviation table, while a global abbreviation applies in all buffers. If an abbreviation is defined both as a local abbreviation for the current buffer and as a global abbreviation, the local abbreviation expansion takes precedence over the global expansion.
Abbreviations can be defined one at a time (adding them as you think of them), or many at a time from an abbreviation file. You can save them in a file and read them back later. If you turn off Abbreviation Mode, abbreviations stop expanding automatically, but their definitions are remembered in case you turn Abbreviation Mode back on.
Keyboard Macros
Start defining a keyboard macro. | ^X-( (start-remembering) |
End the definition of a keyboard macro. | ^X-) (stop-remembering) |
Execute the most recent keyboard macro. | ^X-e (execute-keyboard-macro) |
You can define a keyboard macro to abbreviate a sequence of other commands. If you discover that you are about to type ^U-^D forty times, you can define a keyboard macro to execute ^U-^D and call it with a repeat count of forty.
Keyboard macros differ from ordinary Emacs commands in that they are written as keystrokes rather than as MLisp code. This makes it easier for novices to use them.
You define a keyboard macro while executing the commands which are part of the definition. In this way, you can see what the effects of your commands are -- you do not have to work them out in your head. When you have finished, the keyboard macro is defined and has also been, in effect, executed once. You can execute the sequence of commands again by invoking the macro.
To start defining a keyboard macro, type the ^X-( command. From then on, your commands continue to be executed, but also become part of the macro definition. When you have finished, the ^X-) command terminates the definition (without becoming part of it!).
The newly-defined macro can then be invoked with the ^X-e command. You can give ^X-e a repeat count as a prefix argument to execute the several times.
There are two ways of using a keyboard macro to perform some action on each line in the buffer. The first is to start by positioning dot on the line above the first line to be processed, then begin the macro definition with a ^N (next-line). The second method is to start on the proper line, and end the macro with a ^N. Repeating the macro will operate on successive lines using either method.
Emacs Customisation
This chapter describes some simple ways of customising Emacs without knowing much about MLisp programming. If you find that the information given in this chapter does not allow you to customise Emacs satisfactorily, then you should read the Barry's Emacs Programmer's Guide.
Emacs is designed to be customisable; you can rearrange many Emacs attributes to suit your own taste. Simple customisations are primarily of the following types:
- Changing function bindings from one character sequence to another
- Setting variables
- Causing a function to automatically load a library or package
- Defining actions to be taken when files of a specific type are accessed.
Emacs Environments
Most Emacs users find that they want to customise some features when they first start using Emacs. Later, as they become more familiar with Emacs features, new customisations become apparent.
You will decide which bindings, options and libraries you want to access regularly, and then expect them to be automatically available each time you use Emacs. You can achieve this by making your customisations, then saving them in an Emacs environment file. Emacs starts very quickly when using environment files.
Emacs saves the following information in environment files:
- Buffers
- Variables
- All loaded MLisp functions, packages and libraries
- Macros (including the keyboard macro)
- Global and local bindings
- Syntax tables
- Abbreviation tables
- Database search-lists
- The existence of subprocesses
- Scheduled procedures.
Subprocesses stored in an environment file are re-started when the Emacs environment is restored. The subprocess input, output and termination procedures and buffers are also restored. Note, however, that re-created subprocesses execute with the default directory of the new Emacs process. Symbols and logical names set up in the saved process are not restored.
Scheduled procedures are procedures delivered at a specified time. If that time has already expired, they are delivered immediately.
You use the ESC-x save-environment command to save the current environment in an Emacs environment file. save-environment asks for the name of the file you want to create. Press Return to get the default filename.
You use the /restore command qualifier on the Emacs command to make Emacs restore an environment. This qualifier is implicitly applied when it is not present on the command line. By default, Emacs restores the environment file from emacs_user:
Each operating system uses a different name for the default Emacs environment file. This allows you use share your emacs_user directory between different operating systems.
Operating System | Default Emacs Environment Filename |
---|---|
Windows NT | emacs_user:emacs7_nt.env |
Windows 95 | emacs_user:emacs7_95.env |
FreeBSD | emacs_user:emacs7_FreeBSD_i386.emacs_environment |
HP-UX | emacs_user:emacs7_hpux_pa_risc.emacs_environment |
AIX | emacs_user:emacs7_aix_ppc.emacs_environment |
You cannot restore an environment file that was saved by a previous version of Emacs or on a different operating system.
If Emacs detects an attempt to restore an incompatible environment, an error message is displayed and the editing session is aborted. If this happens to you, you must rebuild your environment file.
building an environment file
Since it is possible that an environment file can become out-of-date, it is best to build your environment file in such a way that it can be easily rebuilt.
Customising Variables
Some customisations in Emacs can be achieved by setting system variables . Some examples of system variables are:
ask-about-buffer-names split-height-threshold quick-redisplay
To change a variable while Emacs is running, use the command ESC-x set. set will ask you for the name of the variable you want to change. You can use name completion to enter the variable name. set will then ask for the value to be inserted into the variable. All the usual Mini-buffer editing commands are available while entering the value. You can abort the whole command using ^G.
If the variable is a boolean, you can type the string 1, on, yes or true to set the value of a variable to logical true, or 0, off, no or false to set the value to logical false. Otherwise, just type the value required.
If the variable is a string, then the value you type is inserted into the variable exactly as you type it. If the variable is numeric, the value is first converted to an integer.
To set a variable in your emacsinit.ml file, you must use the setq function rather than set. The format is as follows:
(setq variable-name value)
where variable-name is the name of the variable to be set, and value is the value required. If the value is a string, then the text of the string should be enclosed in double quotes.
Some variables have a different value in each buffer. These are called buffer-specific variables . Each buffer-specific variable has a default value which is used to initialise each buffer's local copy of the variable. To set the default value of a buffer-specific variable in your EmacsINIT.ML file, enter a line with the following format:
(setq-default variable-name value)
where variable-name is the name of the variable to be set, and value is the value required.
Some examples of setting variables are:
(setq journal-frequency 30) (setq global-mode-string "Emacs") (setq-default highlight-region 1)
Binding Keys to Functions
Emacs allows you to associate arbitrary sequences of keystrokes with any Emacs function. This association is called a key binding -- the key sequence is said to be bound to the function. More than one key sequence can be bound to each function.
For each buffer, Emacs keeps two possible sets of associations: the global bindings and the local bindings. Global bindings apply to all of Emacs' buffers, while local bindings apply only to a specific buffer. Each type of binding has its own command that makes the function-to-keystroke association.
To create a global binding while Emacs is running, use the ESC-x bind-to-key command. This command prompts you for the command name and the key sequence. To make the binding in your EmacsINIT.ML file, enter a line of the form:
(bind-to-key "function-name" "key-sequence")
where function-name is the name of the function to be bound to the sequence key-sequence.
To make local bindings, use the same syntax as above, but replace bind-to-key with local-bind-to-key.
Here are some examples of key bindings:
(bind-to-key "new-undo" "\^Z") (bind-to-key "execute-extended-command" "\[f12]") (local-bind-to-key "execute-mlisp-file" "\^X\^X")
Key Names
Emacs has a set of key names based on the names of keys from the PC keyboard.
describe-binding, describe-key, etc use the name of keys on the PC key board, not the LK201 as before.
Use \[pc-key-name] to bind to a key using the name of keys as they appear on a PC keyboard. Use \(lk201-key-name) for LK201 key names.
You can bind to seperately to a KEY and to ctrl-KEY. For example \[f5] and \[ctrl-f5]
The full list of PC key names is:
\[f1] | \[ctrl-f1] |
\[f2] | \[ctrl-f2] |
\[f3] | \[ctrl-f3] |
\[f4] | \[ctrl-f4] |
\[f5] | \[ctrl-f5] |
\[f6] | \[ctrl-f6] |
\[f7] | \[ctrl-f7] |
\[f8] | \[ctrl-f8] |
\[f9] | \[ctrl-f9] |
\[f10] | \[ctrl-f10] |
\[f11] | \[ctrl-f11] |
\[f12] | \[ctrl-f12] |
\[delete] | \[ctrl-delete] |
\[insert] | \[ctrl-insert] |
\[shift-delete] | \[shift-insert] |
\[end] | \[ctrl-end] |
\[home] | \[ctrl-home] |
\[page-down] | \[ctrl-page-down] |
\[page-up] | \[ctrl-page-up] |
\[pause] | \[ctrl-pause] |
\[scroll-lock] | \[ctrl-scroll-lock] |
\[left] | \[ctrl-left] |
\[right] | \[ctrl-right] |
\[up] | \[ctrl-up] |
\[down] | \[ctrl-down] |
\[mouse-wheel-neg] | \[ctrl-mouse-wheel-neg] |
\[mouse-wheel-pos] | \[ctrl-mouse-wheel-pos] |
\[kp-divide] | \[kp-dot] |
\[kp-enter] | \[kp-minus] |
\[kp-multiple] | \[kp-plus] |
\[kp0] | \[kp1] |
\[kp2] | \[kp3] |
\[kp4] | \[kp5] |
\[kp6] | \[kp7] |
\[kp8] | \[kp9] |
\[csi] | \[ss3] |
\[menu] | \[mouse] |
\[mouse-1-down] | \[mouse-1-up] |
\[mouse-2-down] | \[mouse-2-up] |
\[mouse-3-down] | \[mouse-3-up] |
\[mouse-4-down] | \[mouse-4-up] |
\[mouse-wheel] |
Libraries of Commands
A considerable number of the functions in Emacs reside in libraries. A function is not accessible unless the library that defines the function is loaded. A number of libraries are provided with Emacs, and can be loaded automatically or on request to make their functions available.
To load a library while Emacs is running, use ESC-x execute-mlisp-file. Emacs looks for the library either in your default directory or the directory you specify, or in the Emacs directory, or in the MLisp-library database search-list. Once Emacs has located the library, it is loaded. All the functions defined in the library are then available for use.
For example, if you load the AREA library, you can then use ESC-x delete-area to delete an area of the screen. The delete-area function is defined in the AREA library.
In addition to making functions accessible to ESC-x, the library may bind some functions to key sequences. This is done by the library loading another file called libraryname.key, where libraryname is the name of the library being loaded.
This separation of functions from key bindings allows you to define your own bindings for functions in the library that will override the default bindings. To do this, make a private copy of the .key file, edit it to make your own bindings, then put the modified in your emacs_user directory or in your personal emacslib database file.
Some libraries are loaded automatically in the course of executing certain functions. You will not normally notice this. For example, the process library is automatically loaded when you use the shell function for the first time. This facility is known as autoloading. Autoloading is used to make library functions available without you having to know how to load the library; autoloading also saves space in Emacs by loading libraries only when you need them.
Autoloading works as if you had used execute-mlisp-file on the required library. Autoloading is set up using the function autoload.
To make an autoload association, enter a line with the format shown below in your emacsinit.ml file:
(autoload "function-name" "file-name")
where function-name is the name of the function to be autoloaded and file-name is the library which must be loaded to define the specified function. Both are text strings and should be enclosed in double quotes.
Some examples of autoloads are:
(autoload "dabbrev" "dabbrev") (autoload "shell" "process")
You can create your own libraries which you can share with other Emacs users if you know how to write MLisp code. See the Barry's Emacs Programmer's Guide for details on MLisp programming.
customising emacs
There are many different ways you can customise Emacs. You can do the following:
- Change settings in the menus at the top of the screen.
- Give commands while Emacs is running to make it behave differently. For example, you could toggle between insert and overstrike. Changes such as this last just for the current Emacs session.
- Make changes to the Emacs startup files, so that the new behaviour is present when Emacs starts. For example, you could make Emacs behave differently depending on the type of file that you are editing.
- Use command line parameters when starting Emacs, so that you specify particular packages to load.
Emacs menus
Many of the options on the Emacs menus are similar to those found in other text editors. This section does not describe such options; instead, it describes those options that are less common.
The File menu
Option | Description |
---|---|
Open... | Open a file |
Save | Save the current buffer |
Save As... | Save the current buffer with a new name |
Save All Modified files | Save any file buffers contents. |
The Edit menu
Many of the options on the Edit menu are similar to those found in other text editors. The less common options are:
Option | Description | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Mark | Sets a hard mark so that you can select text. Unlike a normal Windows mark, this mark is not cleared when you move the cursor. | ||||||||||||||||
Case Blind Search | Makes Emacs ignore the case of letters when searching. | ||||||||||||||||
Advanced, Delete White Space | Removes all spaces and tabs that are next to the cursoron the current line. This removes white space to the left and to the right of the cursor. | ||||||||||||||||
Advanced, Tabify Region | Replaces spaces with tabs, where possible, throughout the selected region. | ||||||||||||||||
Advanced, Untabify Region | Replaces tabs with spaces throughout the selected region. | ||||||||||||||||
Advanced, Indent Region | Indents the selected text, moving it to the right by one tab stop. | ||||||||||||||||
Advanced, Undent Region | Moves the selected text to the left by one tab stop. | ||||||||||||||||
Preferences, Font | This option enables you to select a screen font for Emacs to use. The list shows just monospaced fonts. | ||||||||||||||||
Preferences, Colours | This option enables you to select the colours to use for | ||||||||||||||||
Preferences, Directories | This enables you to specify various locations that Emacs uses:
|
||||||||||||||||
Preferences, User Logical Names | A user logical name points to a directory. You can use the
logical name as shorthand way of referring to that directory. For example, if
you define a user logical name of tmp to point to
c:\windows\temp you could then refer to a file,
myfile.tmp within that directory as: tmp:myfile.tmp You can also use logical names when changing Emacs' default directory. You must use a colon (:) at the end of the name to force Emacs to translate the logical name. For example, to make Emacs change to the directory pointed to by the tmp logical name, you could use the following command: change-directory tmp: |
||||||||||||||||
Preferences, Printing | The strings you can enter to set up the print page are:
|
||||||||||||||||
Preferences, File Parsing rules | Use this option if you need to force Emacs to use long filenames or to use short filenames when accessing a particular disk. Normally, Emacs can determine the correct setting automatically, so you are unlikely to need to use this option. |
The View menu
Option | Description |
---|---|
View White Space | Displays tabs and returns. |
Wrap Long Lines | When this is set, Emacs wraps lines that are too long to fit in the window. The behaviour is not the same as you get with Windows programs such as Notepad: movement commands still regard the line as a single line. Thus, next-line moves the cursor to the next proper line, which may mean moving past several wrapped lines. |
Using the Emacs command line
There are a number of different command line options that you can use when starting Emacs:
To make Emacs open a file and go to a specific line number you can use any of the following:
$ bemacs +line-num file $ bemacs /line=line-num file $ bemacs /line:line-num file $ bemacs -line=line-num file $ bemacs -line:line-num file $ bemacs file:line-num:column-num
To make Emacs open a file and make the buffer read-only you can use either of the following:
$ bemacs /readonly file $ bemacs -readonly file
To make Emacs open a file and make the buffer writeable you can use either of the following:
$ bemacs /noreadonly file $ bemacs -noreadonly file
$ bemacs /cd $ bemacs -cd
Changing the Emacs startup files
When Emacs starts up, it does the following:
- Looks in the directory pointed to by the environemt variable emacs_user for the file emacsinit.ml. If the file is there, Emacs executes the commands in it.
- If Emacs cannot find emacsinit.ml in emacs_user:, it looks for the default emacsinit.m, which is in emacs_Library.
If you do not already have a copy of emacs_user:emacsinit.ml, the best way to start is to copy emacs_Library:emacsinit.ml to Emacs_User:Emacsinit.ml. You can then add your own customisations to the bottom of the file.
Setting a variable
The simplest thing you can do when editing Emacsinit.ml is to make changes to Emacs variables. For example, there is an Emacs global variable called replace-case. If you set this variable to 1 then, when doing string replacements, Emacs alters the case of the new string to match that of the string that is being replaced.
By default, Emacs starts with replace-case set to 0, but you could easily add the following line to your Emacsinit.ml file so that Emacs starts up with replace-case turned on:
(setq replace-case 1)
The standard variables are listed in the Mlisp Reference.
Customising Emacs for different filetypes
You can make Emacs behave differently depending on the type of file that you are editing. For example, when editing a text file you may want to make Emacs automatically set up margins, or to change which characters are word characters, or set up some key definitions.
Whatever it is you want Emacs to do, you must define a suitable function, or use an existing function. For example, to make Emacs execute a function called c-mode for all *.c files that it opens, you can add the following line to emacsinit.ml:
(auto-execute "c-mode" "*.c")
Package And Subsystem Index
Table 5 contains a list and brief description of the packages and libraries supplied with Emacs.
For details on using the packages and libraries listed here, see the Barry's Emacs Extensions Reference Manual.
Package and Subsystem Index
Package | Description |
ABBREV | Additional Abbreviation Mode functions that simplify using abbreviations |
ADDLOG | A simple diary logging system |
AREA | Horizontally- and vertically-bounded cut-and-paste functions |
AUTOARG | Implements Auto-arg mode |
BLISSMODE | A major mode for Bliss programmers |
BUFDIRED | A buffer manipulation package |
BUFF | A one-line buffer listing generator |
CENTERLIN | Line-centering functions |
CMODE | A major mode for C programmers |
CMS | Emacs' CMS interface |
DABBREV | Implements dynamic abbreviation expansion |
DCLMODE | A major mode for DCL programmers |
DEBUG | Emacs' Mock Lisp debugger |
DENT | Functions to indent and un-indent blocks of text |
DESCRIBEX | Implements the describe-command and describe-variable functions |
DESWORD | Implements the describe-word-in-buffer function |
DIRED | A directory editing subsystem |
ELECTRICC | Another major mode for C programmers |
ERRMSGPRS | Implements the default error message parser |
EXPANDX | Defines the expand-mlisp-word and expand-mlisp-variable functions |
FLASHER | Implements basic parenthesis balancing |
FUNDMODE | Implements Fundamental Mode -- an alternative to Normal Mode |
GLOBDEL | Implements functions to apply operations to all lines ontaining the specified pattern |
GOTO | Implements various functions for moving dot by specifying various entities |
HELPCHAR | Implements the Emacs Help character |
INCSEARCH | Implements incremental search functions |
INFO | The INFO subsystem |
KILLER | The Kill-Ring package |
LISPMODE | A major mode for Lisp and Mock Lisp programmers |
MAKEBOX | Functions for drawing boxes around text |
MESSAGES | Traps broadcast messages and displays them in a one-line window |
MINIBUF | A package that allows you to define and run small Mlisp programs |
MOUSE-RECTANGULAR | A mouse interface to the rectangular cut-and-paste routines |
NEWCOMPIL | Implements the compile-it and next-error commands |
NORMALMOD | Implements Normal Mode -- the default major mode for Emacs buffers |
NOTES | The VAX NOTES interface package |
NUMBER | A base conversion package |
PAGE | Page-oriented commands using virtual buffer bounds |
PARAGRAPH | Implements the paragraph commands used by the Text Mode functions |
PASCAL | A major mode for PASCAL programmers |
PROCESS | Implements the shell command |
RECTANGULAR | Implements rectangular cut and paste commands |
SCRIBE | A major mode for Scribe programmers |
SENTENCE | Implements the sentence commands |
SHELL | A DCL interaction package |
SPAWN | Implements the spawn function |
SPELL | Implements access to DECspell |
TABS | A package to do smart things with the Tab key |
TAGS | The TAGS program indexing library |
TEACH | The Emacs computer-based instruction package |
TECO | Implements functions to access TECO from within Emacs |
TEXTMODE | Implements Text Mode -- a major mode for text formatting |
THESAURUS | An interface to the DECspell thesaurus |
TIME | A package to display the time on the screen |
TRACE | The MLisp trace package |
UNDO | User-interface to the UNDO package |
VIEW | Implements the view commands |
WHATCURS | Displays statistics about the current cursor position |
WHEREIS | Locates a key sequence when given a function name |
WINDOWS | A window ring package |
WRITEREG | Functions that allow you to write regions to files |