[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8. Basic Editing Commands

Here we explain the basics of how to enter text, make corrections, and save the text in a file. If this material is new to you, we suggest you first run the Emacs learn-by-doing tutorial, by typing Control-h t inside Emacs. (help-with-tutorial).

To clear and redisplay the screen, type C-l (recenter).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.1 Inserting Text

Typing printing characters inserts them into the text you are editing. It inserts them into the buffer at the cursor; more precisely, it inserts them at point, but the cursor normally shows where point is. See section Point.

Insertion moves the cursor forward, and the following text moves forward with the cursor. If the text in the buffer is `FOOBAR', with the cursor before the `B', and you type XX, you get `FOOXXBAR', with the cursor still before the `B'.

To delete text you have just inserted, use the large key labeled DEL, BACKSPACE or DELETE which is a short distance above the RET or ENTER key. Regardless of the label on that key, Emacs thinks of it as DEL, and that's what we call it in this manual. DEL is the key you normally use outside Emacs to erase the last character that you typed.

The DEL key deletes the character before the cursor. As a consequence, the cursor and all the characters after it move backwards. If you type a printing character and then type DEL, they cancel out.

On most computers, Emacs sets up DEL automatically. In some cases, especially with text-only terminals, Emacs may guess wrong. If the key that ought to erase the last character doesn't do it in Emacs, see If DEL Fails to Delete.

Most PC keyboards have both a BACKSPACE key a little ways above RET or ENTER, and a DELETE key elsewhere. On these keyboards, Emacs tries to set up BACKSPACE as DEL. The DELETE key deletes "forwards" like C-d (see below), which means it deletes the character underneath the cursor (after point).

To end a line and start typing a new one, type RET. (This key may be labeled RETURN or ENTER, but in Emacs we call it RET.) This inserts a newline character in the buffer. If point is at the end of the line, this creates a new blank line after it. If point is in the middle of a line, the effect is to split that line. Typing DEL when the cursor is at the beginning of a line deletes the preceding newline character, thus joining the line with the one before it.

Emacs can split lines automatically when they become too long, if you turn on a special minor mode called Auto Fill mode. See section Filling Text, for Auto Fill mode and other methods of filling text.

If you prefer printing characters to replace (overwrite) existing text, rather than shove it to the right, you should enable Overwrite mode, a minor mode. See section Minor Modes.

Only printing characters and SPC insert themselves in Emacs. Other characters act as editing commands and do not insert themselves. These include control characters, and characters with codes above 200 octal. If you need to insert one of these characters in the buffer, you must quote it by typing the character Control-q (quoted-insert) first. (This character's name is normally written C-q for short.) There are two ways to use C-q:

When multibyte characters are enabled, if you specify a code in the range 0200 through 0377 octal, C-q assumes that you intend to use some ISO 8859-n character set, and converts the specified code to the corresponding Emacs character code. See section Enabling Multibyte Characters. You select which of the ISO 8859 character sets to use through your choice of language environment (see section Language Environments).

To use decimal or hexadecimal instead of octal, set the variable read-quoted-char-radix to 10 or 16. If the radix is greater than 10, some letters starting with a serve as part of a character code, just like digits.

A numeric argument tells C-q how many copies of the quoted character to insert (see section Numeric Arguments).

Customization information: DEL in most modes runs the command delete-backward-char; RET runs the command newline, and self-inserting printing characters run the command self-insert, which inserts whatever character you typed. Some major modes rebind DEL to other commands.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.2 Changing the Location of Point

To do more than insert characters, you have to know how to move point (see section Point). The simplest way to do this is with arrow keys, or by clicking the left mouse button where you want to move to.

There are also control and meta characters for cursor motion. Some are equivalent to the arrow keys (it is faster to use these control keys than move your hand over to the arrow keys). Others do more sophisticated things.

C-a

Move to the beginning of the line (move-beginning-of-line).

C-e

Move to the end of the line (move-end-of-line).

C-f

Move forward one character (forward-char). The right-arrow key does the same thing.

C-b

Move backward one character (backward-char). The left-arrow key has the same effect.

M-f

Move forward one word (forward-word).

M-b

Move backward one word (backward-word).

C-n

Move down one line vertically (next-line). This command attempts to keep the horizontal position unchanged, so if you start in the middle of one line, you move to the middle of the next. The down-arrow key does the same thing.

C-p

Move up one line, vertically (previous-line). The up-arrow key has the same effect. This command preserves position within the line, like C-n.

M-r

Move point to left margin, vertically centered in the window (move-to-window-line). Text does not move on the screen. A numeric argument says which screen line to place point on, counting downward from the top of the window (zero means the top line). A negative argument counts lines up from the bottom (-1 means the bottom line).

M-<

Move to the top of the buffer (beginning-of-buffer). With numeric argument n, move to n/10 of the way from the top. See section Numeric Arguments, for more information on numeric arguments.

M->

Move to the end of the buffer (end-of-buffer).

C-v
PAGEDOWN
PRIOR

Scroll the display one screen forward, and move point if necessary to put it on the screen (scroll-up). This doesn't always move point, but it is commonly used to do so. If your keyboard has a PAGEDOWN or PRIOR key, it does the same thing.

Scrolling commands are described further in Scrolling.

M-v
PAGEUP
NEXT

Scroll one screen backward, and move point if necessary to put it on the screen (scroll-down). This doesn't always move point, but it is commonly used to do so. If your keyboard has a PAGEUP or NEXT key, it does the same thing.

M-x goto-char

Read a number n and move point to buffer position n. Position 1 is the beginning of the buffer.

M-g M-g
M-g g
M-x goto-line

Read a number n and move point to the beginning of line number n. Line 1 is the beginning of the buffer. If point is on or just after a number in the buffer, and you type RET with the minibuffer empty, that number is used for n.

C-x C-n

Use the current column of point as the semipermanent goal column for C-n and C-p (set-goal-column). When a semipermanent goal column is in effect, those commands always try to move to this column, or as close as possible to it, after moving vertically. The goal column remains in effect until canceled.

C-u C-x C-n

Cancel the goal column. Henceforth, C-n and C-p try to preserve the horizontal position, as usual.

If you set the variable track-eol to a non-nil value, then C-n and C-p, when starting at the end of the line, move to the end of another line. Normally, track-eol is nil. See section Variables, for how to set variables such as track-eol.

C-n normally stops at the end of the buffer when you use it on the last line of the buffer. However, if you set the variable next-line-add-newlines to a non-nil value, C-n on the last line of a buffer creates an additional line at the end and moves down into it.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.3 Erasing Text

DEL

Delete the character before point (delete-backward-char).

C-d

Delete the character after point (delete-char).

DELETE
BACKSPACE

One of these keys, whichever is the large key above the RET or ENTER key, deletes the character before point--it is DEL. If BACKSPACE is DEL, and your keyboard also has DELETE, then DELETE deletes forwards, like C-d.

C-k

Kill to the end of the line (kill-line).

M-d

Kill forward to the end of the next word (kill-word).

M-DEL

Kill back to the beginning of the previous word (backward-kill-word).

You already know about the DEL key which deletes the character before point (that is, before the cursor). Another key, Control-d (C-d for short), deletes the character after point (that is, the character that the cursor is on). This shifts the rest of the text on the line to the left. If you type C-d at the end of a line, it joins that line with the following line.

To erase a larger amount of text, use the C-k key, which erases (kills) a line at a time. If you type C-k at the beginning or middle of a line, it kills all the text up to the end of the line. If you type C-k at the end of a line, it joins that line with the following line.

See section Killing and Moving Text, for more flexible ways of killing text.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.4 Undoing Changes

Emacs records a list of changes made in the buffer text, so you can you can undo recent changes, as far as the records go. Usually each editing command makes a separate entry in the undo records, but sometimes an entry covers just part of a command, and very simple commands may be grouped.

C-x u

Undo one entry of the undo records--usually, one command worth (undo).

C-_
C-/

The same.

The command C-x u (or C-_ or C-/) is how you undo. Normally this command undoes the last change, and moves point back to where it was before the change.

If you repeat C-x u (or its aliases), each repetition undoes another, earlier change, back to the limit of the undo information available. If all recorded changes have already been undone, the undo command displays an error message and does nothing.

The undo command applies only to changes in the buffer; you can't use it to undo mere cursor motion. However, some cursor motion commands set the mark, so if you use these commands from time to time, you can move back to the neighborhoods you have moved through by popping the mark ring (see section The Mark Ring).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.5 Files

Text that you insert in an Emacs buffer lasts only as long as the Emacs session. To keep any text permanently you must put it in a file. Files are named units of text which are stored by the operating system for you to retrieve later by name. To use the contents of a file in any way, you must specify the file name. That includes editing the file with Emacs.

Suppose there is a file named `test.emacs' in your home directory. To begin editing this file in Emacs, type

 
C-x C-f test.emacs RET

Here the file name is given as an argument to the command C-x C-f (find-file). That command uses the minibuffer to read the argument, and you type RET to terminate the argument (see section The Minibuffer).

Emacs obeys this command by visiting the file: it creates a buffer, it copies the contents of the file into the buffer, and then displays the buffer for editing. If you alter the text, you can save the new text in the file by typing C-x C-s (save-buffer). This copies the altered buffer contents back into the file `test.emacs', making them permanent. Until you save, the changed text exists only inside Emacs, and the file `test.emacs' is unaltered.

To create a file, just visit it with C-x C-f as if it already existed. This creates an empty buffer, in which you can insert the text you want to put in the file. Emacs actually creates the file the first time you save this buffer with C-x C-s.

To learn more about using files in Emacs, see File Handling.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.6 Help

If you forget what a key does, you can find out with the Help character, which is C-h (or F1, which is an alias for C-h). Type C-h k followed by the key of interest; for example, C-h k C-n tells you what C-n does. C-h is a prefix key; C-h k is just one of its subcommands (the command describe-key). The other subcommands of C-h provide different kinds of help. Type C-h twice to get a description of all the help facilities. See section Help.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.7 Blank Lines

Here are special commands and techniques for inserting and deleting blank lines.

C-o

Insert one or more blank lines after the cursor (open-line).

C-x C-o

Delete all but one of many consecutive blank lines (delete-blank-lines).

To insert a new line of text before an existing line, type the new line of text, followed by RET. However, it may be easier to see what you are doing if you first make a blank line and then insert the desired text into it. This is easy to do using the key C-o (open-line), which inserts a newline after point but leaves point in front of the newline. After C-o, type the text for the new line. C-o F O O has the same effect as F O O RET, except for the final location of point.

You can make several blank lines by typing C-o several times, or by giving it a numeric argument specifying how many blank lines to make. See section Numeric Arguments, for how. If you have a fill prefix, the C-o command inserts the fill prefix on the new line, if typed at the beginning of a line. See section The Fill Prefix.

The easy way to get rid of extra blank lines is with the command C-x C-o (delete-blank-lines). C-x C-o in a run of several blank lines deletes all but one of them. C-x C-o on a lone blank line deletes that one. When point is on a nonblank line, C-x C-o deletes all following blank lines (if any).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.8 Continuation Lines

When a text line is too long to fit in one screen line, Emacs displays it on two or more screen lines. This is called continuation or line wrapping. On graphical displays, Emacs indicates line wrapping with small bent arrows in the left and right window fringes. On text-only terminals, Emacs displays a `\' character at the right margin of a screen line if it is not the last in its text line. This `\' character says that the following screen line is not really a new text line.

When line wrapping occurs just before a character that is wider than one column, some columns at the end of the previous screen line may be "empty." In this case, Emacs displays additional `\' characters in the "empty" columns before the `\' character that indicates continuation.

Continued lines can be difficult to read, since lines can break in the middle of a word. If you prefer, you can make Emacs insert a newline automatically when a line gets too long, by using Auto Fill mode. Or enable Long Lines mode, which ensures that wrapping only occurs between words. See section Filling Text.

Emacs can optionally truncate long lines--this means displaying just one screen line worth, and the rest of the long line does not appear at all. `$' in the last column or a small straight arrow in the window's right fringe indicates a truncated line.

See section Truncation of Lines, for more about line truncation, and other variables that control how text is displayed.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.9 Cursor Position Information

Here are commands to get information about the size and position of parts of the buffer, and to count lines.

M-x what-page

Display the page number of point, and the line number within that page.

M-x what-line

Display the line number of point in the whole buffer.

M-x line-number-mode
M-x column-number-mode

Toggle automatic display of the current line number or column number. See section Optional Mode Line Features.

M-=

Display the number of lines in the current region (count-lines-region). See section The Mark and the Region, for information about the region.

C-x =

Display the character code of character after point, character position of point, and column of point (what-cursor-position).

M-x hl-line-mode

Enable or disable highlighting of the current line. See section Displaying the Cursor.

M-x size-indication-mode

Toggle automatic display of the size of the buffer. See section Optional Mode Line Features.

M-x what-line displays the current line number in the echo area. You can also see the current line number in the mode line; see The Mode Line; but if you narrow the buffer, the line number in the mode line is relative to the accessible portion (see section Narrowing). By contrast, what-line shows both the line number relative to the narrowed region and the line number relative to the whole buffer.

M-x what-page counts pages from the beginning of the file, and counts lines within the page, showing both numbers in the echo area. See section Pages.

Use M-= (count-lines-region) to displays the number of lines in the region (see section The Mark and the Region). See section Pages, for the command C-x l which counts the lines in the current page.

The command C-x = (what-cursor-position) shows what cursor's column position, and other information about point and the character after it. It displays a line in the echo area that looks like this:

 
Char: c (99, #o143, #x63) point=28062 of 36168 (78%) column=53

The four values after `Char:' describe the character that follows point, first by showing it and then by giving its character code in decimal, octal and hex. For a non-ASCII multibyte character, these are followed by `file' and the character's representation, in hex, in the buffer's coding system, if that coding system encodes the character safely and with a single byte (see section Coding Systems). If the character's encoding is longer than one byte, Emacs shows `file ...'.

However, if the character displayed is in the range 0200 through 0377 octal, it may actually stand for an invalid UTF-8 byte read from a file. In Emacs, that byte is represented as a sequence of 8-bit characters, but all of them together display as the original invalid byte, in octal code. In this case, C-x = shows `part of display ...' instead of `file'.

`point=' is followed by the position of point expressed as a character count. The start of the buffer is position 1, one character later is position 2, and so on. The next, larger, number is the total number of characters in the buffer. Afterward in parentheses comes the position expressed as a percentage of the total size.

`column=' is followed by the horizontal position of point, in columns from the left edge of the window.

If the buffer has been narrowed, making some of the text at the beginning and the end temporarily inaccessible, C-x = displays additional text describing the currently accessible range. For example, it might display this:

 
Char: C (67, #o103, #x43) point=252 of 889 (28%) <231-599> column=0

where the two extra numbers give the smallest and largest character position that point is allowed to assume. The characters between those two positions are the accessible ones. See section Narrowing.

If point is at the end of the buffer (or the end of the accessible part), the C-x = output does not describe a character after point. The output might look like this:

 
point=36169 of 36168 (EOB) column=0

C-u C-x = displays the following additional information about a character.

Here's an example showing the Latin-1 character A with grave accent, in a buffer whose coding system is iso-latin-1, whose terminal coding system is iso-latin-1 (so the terminal actually displays the character as `À'), and which has font-lock-mode (see section Font Lock mode) enabled:

 
  character: À (2240, #o4300, #x8c0, U+00C0)
    charset: latin-iso8859-1
             (Right-Hand Part of Latin Alphabet 1…
 code point: #x40
     syntax: w 	which means: word
   category: l:Latin
   to input: type "`A" with latin-1-prefix
buffer code: #x81 #xC0
  file code: #xC0 (encoded by coding system iso-latin-1)
    display: terminal code #xC0

There are text properties here:
  fontified            t

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.10 Numeric Arguments

In mathematics and computer usage, argument means "data provided to a function or operation." You can give any Emacs command a numeric argument (also called a prefix argument). Some commands interpret the argument as a repetition count. For example, C-f with an argument of ten moves forward ten characters instead of one. With these commands, no argument is equivalent to an argument of one. Negative arguments tell most such commands to move or act in the opposite direction.

If your terminal keyboard has a META key (labeled ALT on PC keyboards), the easiest way to specify a numeric argument is to type digits and/or a minus sign while holding down the META key. For example,

 
M-5 C-n

moves down five lines. The characters Meta-1, Meta-2, and so on, as well as Meta--, do this because they are keys bound to commands (digit-argument and negative-argument) that are defined to set up an argument for the next command. Meta-- without digits normally means -1. Digits and - modified with Control, or Control and Meta, also specify numeric arguments.

You can also specify a numeric argument by typing C-u (universal-argument) followed by the digits. The advantage of C-u is that you can type the digits without modifier keys; thus, C-u works on all terminals. For a negative argument, type a minus sign after C-u. A minus sign without digits normally means -1.

C-u alone has the special meaning of "four times": it multiplies the argument for the next command by four. C-u C-u multiplies it by sixteen. Thus, C-u C-u C-f moves forward sixteen characters. This is a good way to move forward "fast," since it moves about 1/5 of a line in the usual size screen. Other useful combinations are C-u C-n, C-u C-u C-n (move down a good fraction of a screen), C-u C-u C-o (make "a lot" of blank lines), and C-u C-k (kill four lines).

Some commands care whether there is an argument, but ignore its value. For example, the command M-q (fill-paragraph) fills text; with an argument, it justifies the text as well. (See section Filling Text, for more information on M-q.) Plain C-u is a handy way of providing an argument for such commands.

Some commands use the value of the argument as a repeat count, but do something peculiar when there is no argument. For example, the command C-k (kill-line) with argument n kills n lines, including their terminating newlines. But C-k with no argument is special: it kills the text up to the next newline, or, if point is right at the end of the line, it kills the newline itself. Thus, two C-k commands with no arguments can kill a nonblank line, just like C-k with an argument of one. (See section Killing and Moving Text, for more information on C-k.)

A few commands treat a plain C-u differently from an ordinary argument. A few others may treat an argument of just a minus sign differently from an argument of -1. These unusual cases are described when they come up; they exist to make an individual command more convenient, and they are documented in that command's documentation string.

You can use a numeric argument before a self-inserting character to insert multiple copies of it. This is straightforward when the character is not a digit; for example, C-u 6 4 a inserts 64 copies of the character `a'. But this does not work for inserting digits; C-u 6 4 1 specifies an argument of 641. You can separate the argument from the digit to insert with another C-u; for example, C-u 6 4 C-u 1 does insert 64 copies of the character `1'.

We use the term "prefix argument" as well as "numeric argument," to emphasize that you type these argument before the command, and to distinguish them from minibuffer arguments that come after the command.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.11 Repeating a Command

Many simple commands, such as those invoked with a single key or with M-x command-name RET, can be repeated by invoking them with a numeric argument that serves as a repeat count (see section Numeric Arguments). However, if the command you want to repeat prompts for input, or uses a numeric argument in another way, that method won't work.

The command C-x z (repeat) provides another way to repeat an Emacs command many times. This command repeats the previous Emacs command, whatever that was. Repeating a command uses the same arguments that were used before; it does not read new arguments each time.

To repeat the command more than once, type additional z's: each z repeats the command one more time. Repetition ends when you type a character other than z, or press a mouse button.

For example, suppose you type C-u 2 0 C-d to delete 20 characters. You can repeat that command (including its argument) three additional times, to delete a total of 80 characters, by typing C-x z z z. The first C-x z repeats the command once, and each subsequent z repeats it once again.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Mark Kaminski on July, 3 2008 using texi2html 1.70.