This chapter describes basic Emacs functions that allow you to enter text, make corrections, and save the text in a file.
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.
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.
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.
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 |
LK201 compose key sequence chart | c |
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 |
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.
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.
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) |
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.
The default actions of the mouse follow the normal windows conventions. The following sections give details of the actions.
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.
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.
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.
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.
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 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.
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.