Skip to content

Latest commit

 

History

History
2561 lines (1550 loc) · 72 KB

emacs.org

File metadata and controls

2561 lines (1550 loc) · 72 KB

The Emacs Editor

Emacs is an advanced, extensible, customizable, self-documenting editor.

The home page is https://www.gnu.org/software/emacs/

To extend Emacs, refer the Emacs Lisp Reference Manual.

Copyright (c) 1985-1987, 1993-2021 Free Software Foundation.

Released under the GNU General Public License and GNU Free Documentation License.

Distribution

The source code is available, and you are free to use the same.

It is still protected by copyright and the GNU General Public License.

The Organization of the Screen

Introduction

Use GNU Emacs on the X Window System or the terminal.

It normally starts with only one frame.

Menu bar at the top.

Below the menu bar is the tool bar.

At the very bottom is the echo area.

The main area is called the window. Each window has a buffer.

The window can have a scroll bar on one side.

The last line of the window is the mode line.

You can have multiple windows and frames.

At any time, only one window is selected, and this has the cursor. It is called the current buffer.

Point

The cursor in the selected window is called the point.

It is a solid block, and is between two characters.

For a text “frob”, and cursor on “b”, it means it is between ‘o’ and ‘b’.

Inserting ‘!’ gives “fro!b”, and the cursor remains on ‘b’.

Each buffer has its own value of point, and the display remembers the same.

The Echo Area

The characters of commands are echoed.

It may also prompt you for completion.

Error messages can also be displayed.

Informative messages can also be displayed.

For example, “C-x =” echoes the character value.

Informative echo area messages are also saved in the Messages buffer.

If you miss a message, you can check the Messages buffer.

Var: “message-log-max” specifies the Messages buffer line number limit.

The echo area is also used to display the minibuffer to input arguments to commands.

The prompt string appears in the minibuffer temporarily.

You can get out of the minibuffer by using C-g.

The Mode Line

The Mode Line format:

cs:ch-fr buf pos line (major minor)

cs = Coding System

  • No special character set handling

= Files containing non-textual data 1 ISO Latin-1

cs is followed by: ‘:’ Text separated by newline characters in a file ‘' MS-DOS convention that uses a carriage return instead of a newline ‘/’ or ‘(Mac)’ for older Macintosh systems ‘(Unix)’ on some systems

Frames with emacsclient have ‘@’ to indicate Emacs process running as a daemon.

The ch shows two dashes: ‘–’ if the buffer is unmodified ‘**’ if the buffer is modified ‘%*’ for a read-only buffer

The character after ch is a ‘-‘. For a remote machine, ‘@’ is displayed.

fr gives the selected frame name.

buf is the name of the buffer.

pos tells if there is additional text above or below.

If buffer is small, pos is ‘All’. If you are at the beginning, pos is ‘Top’. If you are at the end of the buffer, pos is ‘Bot’. Or it is ‘nn%’ where nn is the percentage of the buffer above the top of the window.

line is the character ‘L’ followed by the number of at point.

After enabling Column Number mode, you can display the current column number too.

major is the major mode used in the buffer, which is the principal editing mode in the buffer. Examples: Text mode, Lisp mode, C mode etc.

Some additional information may be displayed after the major made. Examples: Compilation buffer, shell buffer etc.

minor is the list of minor modes, which are optional editing modes.

Narrow means editing is restricted to a portion of the text.

‘Def’ means a keyboard macro is currently being defined.

In a recursive editing level, square brackets (‘[…]’) appear around the parenthesis surrounding the mode.

The mode line is mouse-sensitive, and shows tooltips.

The Menu Bar

Use the mouse to choose a command from the menu bar.

Select the first menu bar by pressing F10.

To select press RET and to cancel press G-g.

Kinds of User Input

Primarily designed for use with the keyboard.

Commonly used modifier keys are Control and Meta.

Control-a is entered by holding Ctrl key while pressing ‘a’.

Similarly, Meta-a.

You can enter M-a by typing Esc a.

ESC is entered as a special character. You don’t hold down ESC while typing the next character.

C-M-a is by holding Ctrl and Alt and pressing ‘a’.

Note: On graphical displays, window managers may block some keyboard inputs, M-TAB, M-SPC, C-M-d and C-M-l.

So, customize window manager or rebind the keys.

Mouse clicks are referred to as input events.

Keys

C-f moves forward one character.

Some commands take more input events:

C-x C-f

A key sequence or key is a sequence of one or more input events that is meaningful as a unit.

If a key sequence is not long enough to invoke a command, it is a prefix key. Example: C-x

There is no limit to the length of the key sequences.

Keep it short to three or four input events.

You cannot add input events to a complete key, like C-f.

Default prefix keys: C-c C-h C-x C-x RET C-x a C-x n C-x r C-x t C-x v C-x 4 C-x 5 C-x 6 ESC M-g M-o

F1 and F2 are aliases for C-h and C-x 6.

You can customize, but, it is generally not recommended.

C-h or F1 after a prefix key displays the list of commands for the prefix.

Keys and Commands

Emacs assigns meanings to named commands.

Emacs gives keys their meanings by binding them to commands.

Examples: next-line, forward-word etc.

Internally, each command is a Lisp function.

The actions associated with the command are performed by running the function.

The bindings between keys and commands are recorded in tables called keymaps.

Example: C-n (key) is bound to next-line.

We say, “The command C-n (next-line) moves point vertically down”.

A variable is a name used to store a value. It is useful for customisation.

Entering Emacs

From the terminal, you can start Emacs with the “emacs” command.

To run in the background, use “emacs &”.

The inital frame is the buffer named ”GNU Emacs”.

The startup screen has information about Emacs and links to common tasks useful for beginners.

You can open the Emacs tutorial with C-h t (help-with-tutorial).

You can open a file with Emacs using “emacs foo.txt”.

You can use emacsclient helper program to open a file in the existing Emacs session.

The inhibit-startup-screen is non-nil, it will not show the startup screen.

Use “initial-buffer-choice” to display a file or directory on startup.

Exiting Emacs

C-x C-c (save-buffers-kill-terminal) to kill emacs.

C-z to suspend Emacs (runs command suspend-frame).

You can resume with the shell command %emacs.

If there are unsaved buffers, Emacs will prompt to save these buffers.

It will also ask for confirmation on any subprocesses that are still running.

For a client frame, it simply closes the connection.

Optionally, it can store session information.

If the value of the variable confirm-kill-emacs (default is nil) is non-nil, C-x C-c assumes it has a predicate function and invokes the same.

If the result of this function is non-nil, the session is killed. Otherwise, it continues to run.

One example function to use is yes-or-no-p.

If confirm-kill-processes (default is t) is nil, C-x C-c does not ask for confirmamtion.

To kill Emacs without being prompted, use M-x kill-emacs.

Basic Editing Commands

Inserting Text

The insertion point moves after entering text.

Use RET (newline) to start a new line.

You can also use C-j.

You can change the way Emacs handles text insertion by turning on minor modes.

In Auto Fill mode, lines are split automatically when they get too long.

Overwrite minor mode causes characters to be replaced (overwritten).

DEL runs the command delete-backward-char.

To insert non-graphic characters, start with C-q (quoted-insert).

Example: C-q DEL C-q 101B (inserts AB)

To use decimal or hexadecimal set variable read-quoted-char-radix to 10 or 16.

Unicode characters can be inserted using C-x 8.

Example: C-x 8 [ inserts ’ C-x 8 ] inserts ’ C-x 8 { inserts ” C-x 8 } inserts ”

TODO: A-[ behaves like C-x 8 [ and inserts ‘.

To find what key binding use C-x 8 C-h.

Note: Install ttf-ancient-fonts on Ubuntu to get Unicode characters.

Example: C-x 8 C inserts ©.

C-x 8 RET (insert-char) to prompt for Unicode name.

Example: Bactrian Camel.

You can input either:

  1. Unicode name
  2. Hexadecimal value (Unicode convention)
  3. Specified radix (octal)

The following return the same ’ character:

C-x 8 RET left single quotation mark RET C-x 8 RET left sin TAB RET C-x 8 RET 2018 RET C-x 8 [ TODO A-[ (if the Alt key works) ` (in Electric Quote mode)

A numeric argument to C-q or C-x 8 specifies how many copies of the character to insert.

Changing the Location of Point

To move point to:

C-f (forward-char) C-b (backward-char) C-p (previous-line) C-n (next-line)

You can also use arrow keys or left mouse button to move the point.

Arrow keys RIGHT (right-char) LEFT (left-char) UP (previous-line) DOWN (next-line)

C-a (move-beginning-of-line) HOME Move to the beginning of line

C-e (move-end-of-line) END Move to the end of the line

M-f (forward-word) C-RIGHT

M-RIGHT (right-word) It moves backward by one word if the current paragraph is right-to-left. Example: Bi-directional editing (Arabic, Farsi and Hebrew)

M-b (backward-word) Move backward one word

C-LEFT M-LEFT (left-word) It moves forward by one word if the current paragraph is right-to-left.

M-r (move-to-window-line-top-bottom) Reposition point on the left margin of the center-most text line of window. On subsequent invocations move to top-most line, bottom-most line and so forth (in cyclic).

M-< (beginning-of-buffer) Move to the top of the buffer

With numeric argument n, move to n/10 of the way from the top.

M-> (end-of-buffer) C-END Move to the end of the buffer.

C-v PageDown next Scroll the display one screen forward and move point onscreen if necessary (scroll-up-command).

M-v PageUp prior Scroll one screen backward, and move point if necessary (scroll-down-command).

M-g c Read a number n and move point to buffer position n. Beginning of the buffer is position 1.

M-g M-g M-g g Read a number n and move point to the beginning of the line number n (goto-line). Line 1 is the beginning of the buffer.

M-g TAB Read a number n and move to column n in the current line. Column 0 is the letmost column.

C-x C-n Use the current column of the point as the semipermanent goal column for C-n and C-p in the current buffer.

These commands try to move to this column, or close to it as possible.

C-u C-x C-n Cancel the goal column.

You can force Emacs to move according to logical lines (text lines in the buffer) by setting variable line-move-visual to nil.

If a logical line occupies multiple screen lines, the cursor then skips over the additional lines.

When line-move-visual is nil, if you set track-eol to non-nil, then C-n and C-p start at the end of the logical line, and move to the end of the next logical line.

By default, track-eol is nil.

Setting next-line-add-newlines to non-nil, and using C-n on the last line creates an additional line at the end and moves down to it.

Erasing Text

DEL (delete-backward-char) BACKSPACE Delete the character before point, or the region if it is active.

DEL is not the Delete key. The Delete command deletes the character under the cursor.

DEL removes character before point, moving cursor and characters after it backwards.

If point is at the beginning of a line, it deletes the preceding newline, and joins the line to the previous one.

If a region is active, DEL deletes the text in the region.

Delete (delete-forward-char) Delete character afte point, or the region if it active.

C-d (delete-char) Delete the character at point.

If the point is at the end of the line, it joins the following line to it.

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

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

M-DEL (backward-kill-word) M-BACKSPACE

Kill back to the beginning of the previous word.

Undoing Changes

C-/ C-x u C-_

Undo one entry of the undo records.

Normally, it undoes the last change, moving point back to where it was before the change.

If you repeat C-/, each repetition undoes another, earlier change.

If all changes are undone, the undo command displays an error message and does nothing.

Files

To begin editing a file use:

C-x C-f <file> RET

C-x C-f (find-file) uses minibuffer to read the argument.

It creates a buffer, copies the contents of the file into the buffer, and displays the same for editing.

C-x C-s (save-buffer)

It copies the altered buffer contents back into the file, making it permanent.

To create a new file, just use C-x C-f. This will create an empty buffer, which you can modify and save.

Help

C-h k (describe-key)

Example: C-h k C-n

C-h stands for “help”.

F1 stands as an alias for C-h.

Blank Lines

C-o (open-line) Insert a blank line after the cursor.

Note, unlike C-j, the point does not move.

C-x C-o (delete-blank-lines)

Delete all but one of many consecutive blank lines.

Continuation Lines

On a text terminal, Emacs displays line wrapping by displaying a `` character at the right margin.

C-k kills a logical line.

C-n (next-line) C-p (previous-line) These are exceptions that move one screen line.

On a graphical display, a truncated line is indicated by a small straight arrow in the right fringe.

On a text terminal, it is indicated by a ‘$’ character in the right margin.

You can make Emacs insert a newline automatically when a line gets too long (Auto Fill mode).

Visual Line mode enables word wrapping at word boundaries (space or tab characters) nearest to the right window edge.

It also redefines editing commands such as C-a, C-n, and C-k to operate on screen lines rather than logical lines.

Cursor Position Information

M-x what-line Display the line number point in the echo area.

M-x line-number-mode M-x column-number-mode Toggle automatically display of the current line number or column number.

M-= (count-words-region) Display the number of lines, words and characters that are present in the region.

With prefix, C-u M-=, the command displays a count for the entire buffer.

M-x count-words Displays the number of lines, words and characters that are present in the buffer. If region is active, displays the numbers for the region.

C-x = Display the character code of character at point, and column of point (what-cursor-position).

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

a. Decimal, octal and hex character codes. b. ‘point=’ shows the position of the point as a character count. c. Total number of characters in the buffer (percentage of the total) d. ‘column=’ horizontal position of point.

If what-cursor-show-names is true, the name of the character defined by the Unicode Character Database is shown.

Example: (99, #o143, #x63, LATIN SMALL LETTER C)

For narrowed buffer, additional text describing the range is displayed.

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

M-x hl-line-mode Enable or disabling highlighting of the current line.

M-x size-indication-mode Toggle automatic display of the size of the buffer.

Numeric Arguments

No argument is equivalent to an argument of one.

M-1, M-2 are bound to digit-argument.

M-5 C-n

Negative arguments can be passed.

M– means -1.

M– is bound to negative-argument.

To move down fifty lines, use:

M-5 0 C-n

To insert five zeroes use:

M-5 C-u 0

C-u terminates the prefix argument.

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 by sixteen.

C-u C-u C-f moves forward sixteen characters.

C-u C-n C-u C-u C-n Move down a good fraction of the screen.

C-u C-u C-o Make sixteen blank lines.

C-u C-k Kill four lines.

C-u 6 4 a Insert 64 copies of the character ‘a’.

C-u 6 4 1 Specifies an argument of 641.

You can separate the argument from the digit with another C-u.

C-u 6 4 C-u 1 Inserts 64 copies of the character ‘1’.

M-q (fill-paragraph) It fills text.

With an argument, it justifies the text as well.

C-k with argument n can kill n lines.

C-u 2 C-k

On graphical displayes, C-0, C-1 behave the same as M-0, M-1 etc.

Repeating a Command

If the command you want to repeat needs user input, the M-x command-name RET may not work for numeric arguments.

C-x z (repeat) Another way to repeat an Emacs command many times.

It repeats the previous Emacs command.

For more than once, type additional ‘z’.

Repetition ends when you type a character other than ‘z’ or press a mouse button.

C-u 2 0 C-d to delete 20 characters. To delete 80 characters, type C-x z z z.

The Minibuffer

The minibuffer is where Emacs commands reads complicated arguments such as:

  • file names
  • buffer names
  • Emacs command names
  • Lisp expressions.

It is a “minibuffer” because it’s a special-purpose buffer with a small amount of screen space.

Using the Minibuffer

When in use, it appears in the echo area with a cursor.

It usually starts with a prompt ending with a colon.

The prompt states what kind of input is expected, and how it will be used.

The prompt is highlighted using the minibuffer-prompt face.

The simplest way is to enter text and use RET to submit the argument and exit the minibuffer.

C-g can be used to exit the minibuffer by canceling the command asked for the argument.

The prompt may show a default argument inside parenthesis and before the colon. This will be used if you just type RET.

Example: C-x b (to switch buffer)

With Minibuffer Electric Default mode, a global minor mode, Emacs will hide the default argument as soon as you modify the contents of the minibuffer.

On return to the original minibuffer text, the prompt will show the default.

When variable minibuffer-eldef-shorten-default is set to t, the default argument is displayed as ‘[default-arg]’ instead of ‘(default default-arg)’, saving screen space.

Use M-x minibuffer-electric-default-mode to enable this mode.

Note: If an error message or an informative message is emitted while the minibuffer is active, the message hides the minibuffer for a few seconds, or until you type something; then the minibuffer comes back.

While the minibuffer is in use, Emacs does not echo keystrokes.

Minibuffers for File Names

C-x C-f (find-file) uses the minibuffer to read a file name.

Example:

Find file: /u2/emacs/src/

Find file: is the prompt

u2/emacs/src’ is the default directory.

You can type a filename in the default directory as input.

Example: ~/.bashrc

Alternative defaults for the file name are available by typing M-n.

A file in the parent directory can be specified using ‘..’

Example: ~/.ssh/../.bashrc

You can use M-DEL to kill directory name backwards.

Example: ~/code/twitch/ (M-BACKSPACE).

You can kill the entire default with C-a C-k.

You can specify an absolute filename.

Example: /etc/resolv.conf

A double slash causes Emacs to ignore everything before the second slash in the pair.

The ignored part of the filename is dimmed if the terminal allows it.

Use M-x file-name-shadown-mode to disable the dimming and to turn off File Name Shadow Mode.

Example: C-x C-f -> ~/code// /etc/resolv.conf

When completing remote filenames, double slash causes Emacs to ignore the file-name part, leaving the rest (method, host and username) intact.

Typing three slashes in a row ignores everything in remote filenames.

~/ is your home directory.

~user-id/ means the home directory of a user whose login name is user-id.

Example: ~/code/twitch ~shakthi/code/twitch

On MS-Windows and MS-DOS systems, a user does not always have a home directory.

On these systems, ~user-id/ is only for the logged in current user.

To prevent Emacs from inserting the default directory when reading file names, change the variable insert-default-directory to t.

The minibuffer will start out empty.

The relative file name arguments are still interpreted based on the same default directory.

You can enter remote file names in the minibuffer.

Editing in the Minibuffer

The minibuffer is an Emacs buffer.

The usual Emacs commands are available for editing the argument text.

The prompt, however is read-only, and cannot be changed.

RET in the minibuffer submits the argument, and you cannot use it to enter a newline.

C-q C-j inserts a C-j control character, which is equivalent to a newline.

C-o (open-line) can also be used .

TAB, SPC, and ‘?’ are often bound to completion commands.

Use C-q to insert a TAB, SPC or ‘?’ character.

C-a (move-beginning-of-line) in a minibuffer moves point to the beginning of the argument text, and not the beginning of the prompt.

You can switch to another window with C-x o.

You cannot split the minibuffer window.

If you add two or more lines worth of text into the minibuffer, it expands automatically to accommodate the text.

The variable resize-mini-windows controls the resizing of the minibuffer.

The default value is grow-only.

If the value is t, the minibuffer window will also shrink automatically if you remove some lines of text from the minibuffer, down to a minimum of one screen line.

The value is nil, the minibuffer window will never change the size automatically, but you can use the usual window-resizing commands.

The variable max-mini-window-height controls the maximum height for resizing the minibuffer window.

A floating-point number specifies a fraction of the frame’s height.

An integer specifies the maximum number of lines: nil means do not resize the minibuffer window automatically. The default value is 0.25.

The C-M-v command in the minibuffer scrolls the help text from commands that display help text in another window.

You can also scroll the help text with M-PageUp and M-PageDown (or, equivalently, M-prior and M-next).

This is especially useful with long lists of possible completions.

Example: C-x C-f /usr/bin TAB C-M-v

Emacs normally disallows most commands that use the minibuffer while the minibuffer is active.

To allow such commands in the minibuffer, set the variable enable-recursive-minibuffers to t.

You might need also to enable minibuffer-depth-indicate-mode to show the current recursion depth in the minibuffer prompt on recursive use of the minibuffer.

When not active, the minibuffer is in minibuffer-inactive-mode, and clicking mouse-1 there shows the Messages buffer.

If you use a dedicated frame for minibuffers, Emacs also recognizes certain keys there, for example, n to make a new frame.

Completion

Completion is helpful to enter arguments.

TAB, RET, and SPC are rebound in the minibuffer to special completion commands.

Type ? to see a list of completion alternatives.

Although completion is usually done in the minibuffer, the feature is sometimes available in ordinary buffers too

Completion Example

M-x uses the minibuffer to read the name of a command.

Example: M-x TAB

In order to enable auto-fill-mode, you can try M-x au TAB -> M-x auto TAB -> M-x auto-f TAB.

TAB displays a list of all possible completions in another window

Hence, typing just a u TAB - f TAB (also SPC) allows you to enter ‘auto-fill-mode’.

Completion Commands

TAB

Complete the text in the minibuffer as much as possible; if unable to complete, display a list of possible completions (minibuffer-complete).

SPC

Complete up to one word (next hyphen or space) from the minibuffer text before point (minibuffer-complete-word).

This command is not available for arguments that often include spaces, such as file names.

RET

Submit the text in the minibuffer as the argument, possibly completing first (minibuffer-complete-and-exit).

?

Display a list of completions (minibuffer-completion-help).

The following commands can be used with the completion list:

M-v PageUp prior

Typing M-v, while in the minibuffer, selects the window showing the completion list (switch-to-completions).

RET mouse-1 mouse-2

While in the completion list buffer, this chooses the completion at point (choose-completion)

TAB RIGHT

While in the completion list buffer, these keys move point to the following completion alternative (next-completion).

S-TAB LEFT

While in the completion list buffer, these keys move point to the previous completion alternative (previous-completion).

q

While in the completion list buffer, this quits the window showing it and selects the window showing the minibuffer (quit-window).

Example: Use C-o to show switching windows. Go to Completions buffer, and type q.

z

While in the completion list buffer, kill it and delete the window showing it (kill-current-buffer).

Completion Exit

On pressing RET (minibuffer-complete-and-exit), there are four types of behaviour:

  1. Strict completion: accepts only exact completion matches.

Otherwise, Emacs refuses to exit the minibuffer; instead it tries to complete, and if no completion can be done it momentarily displays ‘[No match]’ after the minibuffer text.

You can still leave the minibuffer by typing C-g to cancel the command.

Example: M-x nothing

  1. Cautious completion: is like strict completion, except RET exits only if the text is already an exact match.

Cautious completion is used for reading file names for files that must already exist, for example.

  1. Permissive completion: allows any input; the completion candidates are just suggestions.

Typing RET does not complete, it just submits the argument as you have entered it.

  1. Permissive completion with confirmation: is like permissive completion, with an exception.

If you typed TAB and this completed the text up to some intermediate state (i.e., one that is not yet an exact completion match), typing RET right afterward does not submit the argument.

Emacs asks for confirmation by momentarily displaying ‘[Confirm]’ after the text; type RET again to confirm and submit the text.

You can tweak the confirmation behavior by customizing the variable confirm-nonexistent-file-or-buffer.

The default value is after-completion gives you a prompt.

If change it to nil, Emacs does not ask for confirmation, falling back to permissive completion.

This behavior is used by most commands that read file names, like C-x C-f, and commands that read buffer names, like C-x b.

Example: C-x b

How Completion Alternatives are Chosen

Emacs attempts to offer plausible completions under most circumstances.

Emacs performs completion using one or more completion styles—sets of criteria for matching minibuffer text to completion alternatives.

If a style yields one or more matches, that is used as the list of completion alternatives.

If a style produces no matches, Emacs falls back on the next style.

The list variable completion-styles specifies the completion styles to use.

The default completion styles are (in order):

  1. basic

A matching completion alternative must have the same beginning as the text in the minibuffer before point.

Furthermore, if there is any text in the minibuffer after point, the rest of the completion alternative must contain that text as a substring.

  1. partial-completion

This aggressive completion style divides the minibuffer text into words separated by hyphens or spaces, and completes each word separately.

When completing command names, ‘em-l-m’ completes to ‘emacs-lisp-mode’.)

Example: em-li-mo.

A ‘*’ in the minibuffer text is treated as a wildcard—it matches any string of characters at the corresponding position in the completion alternative.

  1. emacs22

Similar to basic, except that it ignores the text in the minibuffer after point.

This corresponds to the completion behavior in Emacs 22.

Additional completion styles that are defined:

  1. substring

A matching completion alternative must contain the text in the minibuffer before point, and the text in the minibuffer after point, as substrings (in that same order).

Example: If the text in the minibuffer is ‘foobar’, with point between ‘foo’ and ‘bar’, that matches ‘afoobbarc’, where a, b, and c can be any string including the empty string.

  1. flex

It is also known as flx or fuzzy or scatter completion, attempts to complete using in-order substrings.

For example, it can consider ‘foo’ to match ‘frodo’ or ‘fbarbazoo’.

  1. initials

This very aggressive completion style attempts to complete acronyms and initialisms.

Example: It matches ‘lch’ to ‘list-command-history’.

  1. emacs21

In this style, if the text in the minibuffer is ‘foobar’, only matches starting with ‘foobar’ are considered.

You can use different completion styles in different situations, by setting the variable completion-category-overrides.

For example, the default setting says to use only basic and substring completion for buffer names.

8.4.5 Completion Options

Command names are case sensitive.

‘AU’ does not complete to ‘auto-fill-mode’.

When completing file names, case differences are ignored if the variable read-file-name-completion-ignore-case is t.

The default value is nil on systems that have case-sensitive file-names, such as GNU/Linux; it is t on systems that have case-insensitive file-names, such as Microsoft Windows.

When completing buffer names, case differences are ignored if the variable read-buffer-completion-ignore-case is set to t; the default is nil.

When completing file names, Emacs usually omits certain alternatives that are considered unlikely to be chosen, as determined by the list variable completion-ignored-extensions.

Each element in the list should be a string.

Any file name ending in such a string is ignored as a completion alternative.

Any element ending in a slash (/) represents a subdirectory name.

The standard value of completion-ignored-extensions has several elements including “.o”, “.elc”, and “~”.

For example, if a directory contains ‘foo.c’ and ‘foo.elc’, ‘foo’ completes to ‘foo.c’.

If all possible completions end in otherwise-ignored strings, they are not ignored: in the previous example, ‘foo.e’ completes to ‘foo.elc’.

Shell completion is an extended version of filename completion.

If completion-auto-help is set to nil, the completion commands never display the completion list buffer; you must type ? to display the list.

If the value is lazy, Emacs only shows the completion list buffer on the second attempt to complete.

If there is nothing to complete, the first TAB echoes ‘Next char not unique’; the second TAB shows the completion list buffer.

If completion-cycle-threshold is set to t, completion commands can cycle through completion alternatives.

Each subsequent invocation of the completion command replaces that with the next completion alternative, in a cyclic manner.

If you give completion-cycle-threshold a numeric value n, completion commands switch to this cycling behavior only when there are n or fewer alternatives.

Minibuffer History

Every argument that you enter with the minibuffer is saved in a minibuffer history list so you can easily use it again later.

M-p

Move to the previous item in the minibuffer history, an earlier argument (previous-history-element).

Example: M-x auto-fill-mode M-x battery M-p

M-n

Move to the next item in the minibuffer history (next-history-element).

UP DOWN

Like M-p and M-n, but move to the previous or next line of a multi-line item before going to the previous history item (previous-line-or-history-element and next-line-or-history-element) .

M-r regexp RET

Move to an earlier item in the minibuffer history that matches regexp (previous-matching-history-element).

M-s regexp RET

Move to a later item in the minibuffer history that matches regexp (next-matching-history-element).

If you type M-n in the minibuffer when there are no later entries in the minibuffer history (e.g., if you haven’t previously typed M-p), Emacs tries fetching from a list of default arguments: values that you are likely to enter.

You can think of this as moving through the “future history”.

The “future history” for file names includes several possible alternatives you may find useful, such as the file name or the URL at point in the current buffer.

The defaults put into the “future history” in this case are controlled by the functions mentioned in the value of the option file-name-at-point-functions.

By default, its value invokes the ffap package (see FFAP), which tries to guess the default file or URL from the text around point.

To disable this guessing, customize the option to a nil value, then the “future history” of file names will include only the file, if any, visited by the current buffer, and the default directory.

If you edit the text inserted by the M-p or M-n minibuffer history commands, this does not change its entry in the history list.

However, the edited argument does go at the end of the history list when you submit it.

A numeric prefix argument n to M-r and M-s means fetch the nth matching entry.

An upper-case letter in the regular expression makes the search case-sensitive.

You can also search through the history using an incremental search. Example: Isearch Minibuffer.

Emacs keeps separate history lists for several different kinds of arguments.

For example, there is a list for file names, used by all the commands that read file names.

Other history lists include buffer names, command names (used by M-x), and command arguments (used by commands like query-replace).

The variable history-length specifies the maximum length of a minibuffer history list.

Adding a new element deletes the oldest element if the list gets too long.

If the value is t, there is no maximum length.

The variable history-delete-duplicates specifies whether to delete duplicates in history.

If it is t, adding a new element deletes from the list all other elements that are equal to it. The default is nil.

Repeating Minibuffer Commands

Every command that uses the minibuffer once is recorded on a special history list, the command history, together with the values of its arguments, so that you can repeat the entire command.

M-x is recorded there, since M-x uses the minibuffer to read the command name.

C-x ESC ESC

Re-execute a recent minibuffer command from the command history (repeat-complex-command).

A numeric argument specifies which command to repeat; 1 means the last one, 2 the previous, and so on.

Example: C-u 2 C-x ESC ESC

It works by turning the previous command into a Lisp expression and then entering a minibuffer initialized with the text for that expression.

You can also change the command by editing the Lisp expression before you execute it.

The executed command is added to the front of the command history unless it is identical to the most recent item.

Once inside the minibuffer for C-x ESC ESC, you can use the usual minibuffer history commands (see Minibuffer History) to move through the history list.

You can make incremental search commands appear in the history by setting isearch-resume-in-command-history to t.

M-x list-command-history

Display the entire command history, showing all the commands C-x ESC ESC can repeat, most recent first.

Lisp programs can re-execute a command by calling eval with the command-history element.

Entering Passwords

You may want to enter a password to visit a file on another machine via a network protocol such as FTP, you often need to supply a password to gain access to the machine.

Emacs displays a prompt in the echo area (such as ‘Password: ’); after you type the required password, press RET to submit it.

To prevent others from seeing your password, every character you type is displayed as an asterisk (‘*’) instead of its usual form.

Most of the features and commands associated with the minibuffer cannot be used when entering a password.

There is no history or completion, and you cannot change windows or perform any other action with Emacs until you have submitted the password.

While you are typing the password, you may press DEL to delete backwards, removing the last character entered.

C-u deletes everything you have typed so far.

C-g quits the password prompt (see Quitting).

C-y inserts the current kill into the password (see Killing).

You may type either RET or ESC to submit the password.

Yes or No Prompts

An Emacs command may require you to answer a yes-or-no question during the course of its execution.

Two varieties:

  1. The prompt ends with ‘(y or n)’:

You answer by typing a single key, which immediately exits the minibuffer and delivers the response.

Example: C-x C-w (write-file) to save a buffer, and enter the name of an existing file, Emacs issues a prompt like this:

File ‘foo.el’ exists; overwrite? (y or n)

  1. The prompt ends with ‘(yes or no)’:

The case when giving the wrong answer would have serious consequences.

It thus features a longer prompt ending with ‘(yes or no)’.

For example, if you invoke C-x k (kill-buffer) on a file-visiting buffer with unsaved changes, Emacs activates the minibuffer with a prompt like this:

Buffer foo.el modified; kill anyway? (yes or no)

You can use the following commands at the prompt:

  • Recenter the selected window with C-l,
  • Scroll that window (C-v or PageDown scrolls forward, M-v or PageUp scrolls backward),
  • Switch to another window with C-x o,
  • Use the history commands M-p and M-n, etc.
  • Type C-g to dismiss the query, and quit the minibuffer and the querying command.

9. Running Commands by Name

Every Emacs command has a name that you can use to run it.

For convenience, many commands also have key bindings.

Example: M-x forward-char RET M-x forw TAB c RET C-f

When M-x completes on commands, it ignores the commands that are declared obsolete.

Obsolete commands are those for which newer, better alternatives exist, and which are slated for removal in some future Emacs release.

C-g to cancel and not run a command.

Numeric arguments are passed before the M-x command.

Example: C-u 42 M-x forward-char RET

If the command has a key binding, Emacs mentions this in the echo area after running the command.

You can turn off these messages by setting the variable suggest-key-bindings to nil.

The value of suggest-key-bindings can also be a number, in which case Emacs will show the binding for that many seconds before removing it from display.

The default behavior is to display the binding for 2 seconds.

Emacs mentions such shorthands in the echo area if they are significantly shorter than the full command name, and extended-command-suggest-shorter is t.

M-x works by running the command execute-extended-command.

10. Help

Emacs provides a wide variety of help commands, all accessible through the prefix key C-h or F1.

You can also type C-h C-h to view a list of help commands (help-for-help).

Example: C-h C-h

You can scroll the list with SPC and DEL.

Many help commands display their information in a special help buffer.

If you are looking for a certain feature, but don’t know what it is called or where to look, we recommend three methods:

  1. apropos command
  2. Manual index
  3. FAQ and package keywords

C-h a topics RET

This searches for commands whose names match the argument topics.

The argument can be a keyword, a list of keywords, or a regular expression.

Example: buffer

C-h i d m emacs RET i topic RET

This searches for topic in the indices of the Emacs Info manual, displaying the first match found.

Press , to see subsequent matches. You can use a regular expression as topic.

C-h i d m emacs RET s topic RET

searches the text of the manual rather than the indices.

C-h C-f

This displays the Emacs FAQ, using Info.

C-h p

This displays the available Emacs packages based on keywords.

C-h or F1 mean “help” in various other contexts as well.

You can type them after a prefix key to view a list of the keys that can follow the prefix key.

You can also use ? in this context.

Example: M-x ?

A few prefix keys don’t support C-h or ? in this way, because they define other meanings for those inputs, but they all support F1.

10.1 Help Summary

C-h a topics RET

Display a list of commands whose names match topics (apropos-command).

C-h b

Display all active key bindings; minor mode bindings first, then those of the major mode, then global bindings (describe-bindings).

C-h c key

Show the name of the command that the key sequence key is bound to (describe-key-briefly). Here c stands for “character”.

Example: C-h c M-x

C-h d topics RET

Display the commands and variables whose documentation matches topics (apropos-documentation).

C-h e

Display the Messages buffer (view-echo-area-messages).

C-h f function RET

Display documentation on the Lisp function named function (describe-function). Since commands are Lisp functions, this works for commands too.

Example: C-h f auto-fill-mode RET

C-h h

Display the HELLO file, which shows examples of various character sets.

Example: Run Emacs in X11.

C-h i

Run Info, the GNU documentation browser (info). The Emacs manual is available in Info.

C-h k key

Display the name and documentation of the command that key runs (describe-key).

Example: C-h k C-p

C-h l

Display a description of your last 300 keystrokes (view-lossage).

C-h m

Display documentation of the current major mode and minor modes (describe-mode).

C-h n

Display news of recent Emacs changes (view-emacs-news). See Help Files.

C-h o symbol

Display documentation of the Lisp symbol named symbol (describe-symbol).

This will show the documentation of all kinds of symbols: functions, variables, and faces.

Example: C-h o describe-function

C-h p

Find packages by topic keyword (finder-by-keyword). This lists packages using a package menu buffer

C-h P package RET

Display documentation about the specified package (describe-package).

Example: C-h P calculator RET

C-h r

Display the Emacs manual in Info (info-emacs-manual).

C-h s

Display the contents of the current syntax table (describe-syntax).

The syntax table says which characters are opening delimiters, which are parts of words, and so on.

C-h t

Enter the Emacs interactive tutorial (help-with-tutorial).

C-h v var RET

Display the documentation of the Lisp variable var (describe-variable).

Example: C-h v suggest-key-bindings RET

C-h w command RET

Show which keys run the command named command (where-is).

Example: C-h w help-with-tutorial RET

C-h C coding RET

Describe the coding system coding (describe-coding-system).

Example: C-h C utf-8 RET

C-h C RET

Describe the coding systems currently in use.

C-h F command RET

Enter Info and go to the node that documents the Emacs command command (Info-goto-emacs-command-node).

Note: On Ubuntu 22.04.04 LTS install emacs-common-non-dfsg for Info manuals.

Example: C-h F apropos RET

C-h I method RET

Describe the input method method (describe-input-method).

Example: C-h I dutch RET

C-h K key

Enter Info and go to the node that documents the key sequence key (Info-goto-emacs-key-command-node).

C-h L language-env RET

Display information on the character sets, coding systems, and input methods used in language environment language-env (describe-language-environment). See Language Environments.

Example: C-h L tamil RET

C-h S symbol RET

Display the Info documentation on symbol symbol according to the programming language you are editing (info-lookup-symbol).

C-h .

Display the help message for a special text area, if point is in one (display-local-help). (These include, for example, links in Help buffers.)

10.2 Documentation for a Key

The help commands to get information about a key sequence are:

  1. C-h c (describe-key-briefly), and
  2. C-h k (describe-key).

Example: C-h c C-p

C-h k key gives more information by displaying a help buffer.

C-h K key displays displays the section of the Emacs manual that describes the command.

C-h c ignores mouse movement events.

After C-h k you can select a menu item from the menu bar.

C-h w command RET lists the keys that are bound to command.

Example: C-h w TAB C-h w describe-key RET

It displays the list in the echo area.

If it says the command is not on any key, that means you must use M-x to run it.

C-h w runs the command where-is.

10.3 Help by Command or Variable Name

C-h f function RET (describe-function) displays the documentation of Lisp function function, in a window.

Commands are Lisp functions.

Example: C-h f auto-fill-mode RET

It is only way to get the documentation of a command that is not bound to any key.

C-h f is also useful for Lisp functions that you use in a Lisp program.

If you type C-h f RET in the buffer around point, it describes the function called by the innermost Lisp expression, provided that function name is a valid, defined Lisp function.

Example: C-h f RET (point on auto-fill-mode)

C-h f is also useful just to verify that you spelled a function name correctly.

If the minibuffer prompt for C-h f shows the function name from the buffer as the default, it means that name is defined as a Lisp function.

Type C-g to cancel the C-h f command.

C-h v (describe-variable) is like C-h f but describes Lisp variables instead of Lisp functions.

Its default is the Lisp symbol around or before point, if that is the name of a defined Lisp variable.

Example: Point on suggest-key-bindings

Help buffers that describe Emacs variables and functions normally have hyperlinks to the corresponding source code.

Example: C-h f describe-function RET

To find a command’s documentation in a manual, use C-h F (Info-goto-emacs-command-node).

C-h o (describe-symbol) is like C-h f and C-h v, but it describes any symbol, be it a function, a variable, or a face.

If the symbol has more than one definition, like it has both definition as a function and as a variable, this command will show the documentation of all of them, one after the other.

10.4 Apropos

The apropos commands ask, “What are the commands for working with files?”.

You specify an apropos pattern, which means either a word, a list of words, or a regular expression.

The following apropos commands:

  1. read an apropos pattern in the minibuffer,
  2. searches for items that match the pattern, and
  3. displays the results in a different window.

C-h a

Search for commands (apropos-command).

With a prefix argument, search for noninteractive functions too.

Example: C-h a file RET

When you specify more than one word in the apropos pattern, a name must contain at least two of the words in order to match.

Example: C-h a kill back backward behind before RET

M-x apropos

Search for functions and variables. Both interactive functions (commands) and noninteractive functions can be found.

Example: M-x apropos window

M-x apropos-user-option

Search for user-customizable variables. With a prefix argument, search for non-customizable variables too.

Example: M-x apropos-user-option buffer

M-x apropos-variable

Search for variables. With a prefix argument, search for customizable variables only.

Example: M-x apropos-variable mode

M-x apropos-local-variable

Search for buffer-local variables.

Example: apropos-local-variable line

M-x apropos-value

Search for variables whose values match the specified pattern.

With a prefix argument, search also for functions with definitions matching the pattern, and Lisp symbols with properties matching the pattern.

Example: M-x apropos-value frame

M-x apropos-local-value

Search for buffer-local variables whose values match the specified pattern.

Example: M-x apropos-local-value delete

C-h d

Search for functions and variables whose documentation strings match the specified pattern (apropos-documentation).

Example: C-h d regex

If the variable apropos-do-all is t, most apropos commands behave as if they had been given a prefix argument.

There is one exception: apropos-variable without a prefix argument will always search for all variables, no matter what the value of apropos-do-all is.

All apropos commands except apropos-documentation list their results in alphabetical order.

If the variable apropos-sort-by-scores is t, these commands instead try to guess the relevance of each result, and display the most relevant ones first.

The apropos-documentation command lists its results in order of relevance by default.

In order to list them in alphabetical order, change the variable apropos-documentation-sort-by-scores to nil.

10.5 Help Mode Commands

Help buffers provide the same commands as View mode (see View Mode):

  1. SPC scrolls forward, and
  2. DEL or S-SPC scrolls backward.

Example: C-h f auto-fill-mode

A few special commands are also provided:

RET

Follow a cross reference at point (help-follow).

TAB

Move point forward to the next hyperlink (forward-button).

S-TAB

Move point back to the previous hyperlink (backward-button). mouse-1 mouse-2

Follow a hyperlink that you click on.

C-c C-c

Show all documentation about the symbol at point (help-follow-symbol).

C-c C-f r

Go forward to the next help topic (help-go-forward).

C-c C-b l

Go back to the previous help topic (help-go-back).

A help buffer can also contain hyperlinks to Info manuals, source code definitions, and URLs (web pages).

The first two are opened in Emacs, and the third using a web browser via the browse-url command.

The TAB and S-TAB commands are cyclical.

10.6 Keyword Search for Packages

Most optional features in Emacs are grouped into packages.

Emacs contains several hundred built-in packages, and more can be installed over the network.

Most packages are associated with one or more keywords based on what they do.

Type C-h p (finder-by-keyword) to bring up a list of package keywords, together with a description of what the keywords mean.

To view a list of packages for a given keyword, type RET on that line; this displays the list of packages in a Package Menu buffer.

C-h P (describe-package) prompts for the name of a package (see Packages), and displays a help buffer describing the attributes of the package and the features that it implements.

Example: C-h P battery

10.7 Help for International Language Support

For information on a specific language environment type C-h L (describe-language-environment).

This displays a help buffer describing the languages supported by the language environment, and listing:

  1. The associated character sets,
  2. coding systems,
  3. input methods,
  4. some sample text for that language environment.

The command C-h h (view-hello-file) displays the file etc/HELLO, which demonstrates various character sets by showing how to say “hello” in many languages.

The command C-h I (describe-input-method) describes an input method—either a specified input method, or by default the input method currently in use. See Input Methods.

Example: C-h I tamil-dvorak

The command C-h C (describe-coding-system) describes coding systems—either a specified coding system, or the ones currently in use. See Coding Systems.

Example: C-h C utf-8

10.8 Other Help Commands

C-h i (info) runs the Info program, which browses structured documentation files.

C-h 4 i (info-other-window) does the same, but shows the Info buffer in another window.

Type h after entering Info to run a tutorial on using Info.

With a numeric argument n, C-h i selects the Info buffer ‘*info*<n>’.

This is useful if you want to browse multiple Info manuals simultaneously.

If you specify just C-u as the prefix argument, C-h i prompts for the name of a documentation file, so you can browse a file which doesn’t have an entry in the top-level Info menu.

The help commands C-h F function RET and C-h K key, described above, enter Info and go straight to the documentation of function or key.

When editing a program, if you have an Info version of the manual for the programming language, you can use C-h S (info-lookup-symbol) to find an entry for a symbol (keyword, function or variable) in the proper manual.

Example: C-h S info-lookup-symbol

If you are not sure what you typed, use C-h l (view-lossage).

C-h l displays your last 300 input keystrokes and the commands they invoked.

To review recent echo area messages, use C-h e (view-echo-area-messages). This displays the buffer Messages, where those messages are kept.

C-h m (describe-mode) displays documentation on the current major mode, which normally describes the commands and features that are changed in this mode, and also its key bindings.

C-h b displays a list of all the key bindings now in effect: first the local bindings of the current minor modes, then the local bindings defined by the current major mode, and finally the global bindings.

C-h s displays the contents of the syntax table, with explanations of each character’s syntax.

You can get a list of subcommands for a particular prefix key by typing C-h, ?, or F1 (describe-prefix-bindings) after the prefix key.

Example: C-u C-x C-h

A few prefix keys for which not all of these keys work—those that provide their own bindings for that key.

One of these prefix keys is ESC, because ESC C-h is actually C-M-h, which marks a defun. However, ESC F1 and ESC ? work fine.

Example: ESC F1

10.9 Help files

There are several other files describing topics like:

  1. copying conditions,
  2. release notes,
  3. instructions for debugging, and
  4. reporting bugs

C-h C-c

Display the rules under which you can copy and redistribute Emacs (describe-copying).

C-h C-d

Display help for debugging Emacs (view-emacs-debugging).

C-h C-e

Display information about where to get external packages (view-external-packages).

C-h C-f

Display the Emacs frequently-answered-questions list (view-emacs-FAQ).

C-h g

Visit the page with information about the GNU Project (describe-gnu-project).

C-h C-m

Display information about ordering printed copies of Emacs manuals (view-order-manuals).

C-h C-n

Display the news, which lists the new features in this version of Emacs (view-emacs-news).

C-h C-o

Display how to order or download the latest version of Emacs and other GNU software (describe-distribution).

C-h C-p

Display the list of known Emacs problems, sometimes with suggested workarounds (view-emacs-problems).

C-h C-t

Display the Emacs to-do list (view-emacs-todo).

C-h C-w

Display the full details on the complete absence of warranty for GNU Emacs (describe-no-warranty).

10.10 Help on Active Text and Tooltips

The active text (text that does something special in response to mouse clicks or RET) often have associated help text.

This includes hyperlinks in Emacs buffers, as well as parts of the mode line.

On graphical displays, as well as some text terminals which support mouse tracking, moving the mouse over the active text displays the help text as a tooltip.

On terminals that don’t support mouse-tracking, you can display the help text for active buffer text at point by typing C-h . (display-local-help).

This shows the help text in the echo area.

To display help text automatically whenever it is available at point, set the variable help-at-pt-display-when-idle to t.

11. The Mark and the Region

The text between point and the mark is called the region.

You can also explicitly deactivate the mark at any time, by typing C-g.

If the variable highlight-nonselected-windows is t, each window highlights its own region.

Setting Mark

C-SPC C-@ Set the mark at point, and activate it (set-mark-command).

C-x C-x Set the mark at point, and activate it; then move point where the mark used to be (exchange-point-and-mark).

Drag-mouse-1 Set point and the mark around the text you drag across.

mouse-3 Set the mark at point, then move point to where you click (mouse-save-then-kill).

Shifted cursor motion keys

Set the mark at point if the mark is inactive, then move point. See Shift Selection.

Example: C-SPC C-f C-x C-u.

You can remember a position in the buffer (by typing C-SPC C-SPC), and later jump back there by typing C-u C-SPC.

Many commands that insert text, such as C-y (yank), set the mark at the other end of the inserted text, without activating it.

Commands to Mark Textual Objects

M-@ Set mark at the end of the next word (mark-word). This does not move point.

Example: Repeated invocations extends the region. C-u 5 M-@

C-M-@ Set mark after end of following balanced expression (mark-sexp). This does not move point.

M-h Move point to the beginning of the current paragraph, and set mark at the end (mark-paragraph).

C-M-h Move point to the beginning of the current defun, and set mark at the end (mark-defun).

C-x C-p Move point to the beginning of the current page, and set mark at the end (mark-page).

C-x h Move point to the beginning of the buffer, and set mark at the end (mark-whole-buffer).

Operating on the Region

Once you have a region, here are some of the ways you can operate on it:

Kill it with C-w (see Killing).

Copy it to the kill ring with M-w (see Yanking).

Convert case with C-x C-l or C-x C-u (see Case).

Undo changes within it using C-u C-/ (see Undo).

Replace text within it using M-% (see Query Replace).

Indent it with C-x TAB or C-M-\ (see Indentation).

Fill it as text with M-x fill-region (see Filling).

Check the spelling of words within it with M-$ (see Spelling).

Evaluate it as Lisp code with M-x eval-region (see Lisp Eval).

Save it in a register with C-x r s (see Registers).

Save it in a buffer or a file (see Accumulating Text).

If you want the commands to operate on the empty region, change the variable use-empty-active-region to t.

If you change the variable delete-active-region to nil, then these commands don’t act differently when the mark is active.

If you change the value to kill, these commands kill the region instead of deleting it.

Some commands usually have the word region in their names, like C-w (kill-region) and C-x C-u (upcase-region).

If the mark is inactive, they operate on the inactive region—that is, on the text between point and the position at which the mark was last set.

To disable this behavior, change the variable mark-even-if-inactive to nil.

Then these commands will instead signal an error if the mark is inactive.

To toggle Delete Selection mode on or off, type M-x delete-selection-mode.

The Mark Ring

C-SPC C-SPC Set the mark, pushing it onto the mark ring, without activating it.

C-u C-SPC Move point to where the mark was, and restore the mark from the ring of former marks.

If you set set-mark-command-repeat-pop to t, then immediately after you type C-u C-SPC, you can type C-SPC instead of C-u C-SPC to cycle through the mark ring.

By default, set-mark-command-repeat-pop is nil.

The variable mark-ring-max specifies the maximum number of entries to keep in the mark ring.

This defaults to 16 entries.

The Global Mark Ring

The length of the global mark ring is controlled by global-mark-ring-max, and is 16 by default.

Shift Selection

To turn off shift-selection, set shift-select-mode to nil.

Disabling Transient Mark Mode

While Transient Mark mode is off, you can activate it temporarily using C-SPC C-SPC or C-u C-x C-x.

C-SPC C-SPC Set the mark at point (like plain C-SPC) and enable Transient Mark mode just once, until the mark is deactivated.

C-u C-x C-x Exchange point and mark, activate the mark and enable Transient Mark mode temporarily, until the mark is next deactivated.

One reason you may want to use them is that some commands operate on the entire buffer instead of the region when Transient Mark mode is off.

Enabling Transient Mark mode momentarily gives you a way to use these commands on the region.

12. Killing and Moving Text

Killing means erasing text and copying it into the kill ring.

Yanking means bringing text from the kill ring back into the buffer.

Deletion and Killing

The commands have the word, ‘kill’.

Example: kill-line

The kill ring is shared by all buffers.

C-/ (undo) is used to undo a kill command, but, does not remove it from the kill ring.

Commands that erase text but do not save it in the kill ring are known as delete commands; their names usually contain the word ‘delete’.

Example: C-d (delete-char) DEL (delete-backward-char)

Yanking

Introduction

C-y Yank the last kill into the buffer, at point (yank).

You jump easily to the mark position, if you wish, with C-u C-SPC.

M-y Replace the text just yanked with an earlier batch of killed text (yank-pop).

C-M-w Cause the following command, if it is a kill command, to append to the previous kill (append-next-kill)

Example: C-k C-M-w C-y

With a plain prefix argument (C-u C-y), the command instead leaves the cursor in front of the inserted text, and sets the mark at the end.

Using any other prefix argument specifies an earlier kill; e.g., C-u 4 C-y reinserts the fourth most recent kill.

The Kill Ring

The maximum number of entries in the kill ring is controlled by the variable kill-ring-max.

The default is 60.

The actual contents of the kill ring are stored in a variable named kill-ring.

you can view the entire contents of the kill ring with C-h v kill-ring.

Yanking Earlier Kills

M-y (yank-pop) command to cycle through the possibilites.

Example: C-y M-y (…)

Appending Kills

“This is a line *of sample text”

Example: M-d M-DEL M-d M-DEL C-h v kill-ring M-b M-b C-u M-d (alternate) M-f M-f C-u M-DEL (alternate)

You can force it to combine with the last killed text, by typing C-M-w (append-next-kill).

The C-M-w tells its following command, if it is a kill command, to treat the kill as part of the sequence of previous kills.

A kill command following M-w (kill-ring-save) does not append to the text that M-w copied into the kill ring.

“Cut and Paste” Operations on Graphical Displays

Introduction

You can specify another coding system by typing C-x RET x or C-x RET X.

You can also request a different data type by customizing x-select-request-type.

Using the Clipboard

You can change save-interprogram-paste-before-kill to t for Emacs to first save the clipboard to its kill ring, preventing you from losing the old clipboard data.

If you change yank-pop-change-selection to t, then M-y saves the new yank to the clipboard.

To prevent kill and yank commands from accessing the clipboard, change the variable select-enable-clipboard to nil.

If you wish to prevent Emacs from transferring data to the clipboard manager, change the variable x-select-enable-clipboard-manager to nil.

Prior to Emacs 24, the kill and yank commands used the primary selection (see Primary Selection), not the clipboard.

If you prefer this behavior, change select-enable-clipboard to nil, select-enable-primary to t, and mouse-drag-copy-region to t. So:

  • clipboard-kill-region kills the region and saves it to the clipboard
  • clipboard-kill-ring-save copies the region to the kill ring and saves it to the clipboard
  • clipboard-yank yanks the contents of the clipboard at point.

Cut and Paste with Other Window Applications

Under the X Window System, there exists a primary selection containing the last stretch of text selected in an X application (usually by dragging the mouse).

If you change the variable select-active-regions to only, Emacs saves only temporarily active regions to the primary selection, i.e., those made with the mouse or with shift selection.

If you change select-active-regions to nil, Emacs avoids saving active regions to the primary selection entirely.

To insert the primary selection into an Emacs buffer, click mouse-2 (mouse-yank-primary) where you want to insert it.

Secondary Selection

M-Drag-mouse-1 Set the secondary selection, with one end at the place where you press down the button, and the other end at the place where you release it (mouse-set-secondary).

The selected text is highlighted, using the secondary-selection face, as you drag.

M-mouse-1 Set one endpoint for the secondary selection (mouse-start-secondary).

Use M-mouse-3 to set the other end and complete the selection.

M-mouse-3 Set the secondary selection (mouse-secondary-save-then-kill), with one end at the position you click M-mouse-3, and the other at the position specified previously with M-mouse-1.

M-mouse-2 Insert the secondary selection where you click, placing point at the end of the yanked text (mouse-yank-secondary).

Double or triple clicking of M-mouse-1 operates on words and lines, much like mouse-1.

If mouse-yank-at-point is non-nil, M-mouse-2 yanks at point.

Accumulating Text

There are convenient methods for copying one block of text in many places, or for copying many scattered blocks of text into one place.

We describe the commands to accumulate scattered pieces of text into a buffer or into a file.

M-x append-to-buffer Append region to the contents of a specified buffer.

M-x prepend-to-buffer Prepend region to the contents of a specified buffer.

M-x copy-to-buffer Copy region into a specified buffer, deleting that buffer’s old contents.

M-x insert-buffer Insert the contents of a specified buffer into current buffer at point.

M-x append-to-file Append region to the contents of a specified file, at the end.

Example: Use GNU Emacs and scratch buffers.

Rectangles

A rectangular region can also be marked using the mouse: click and drag C-M-mouse-1 from one corner of the rectangle to the opposite.

C-x r k Kill the text of the region-rectangle, saving its contents as the last killed rectangle (kill-rectangle).

C-x r M-w Save the text of the region-rectangle as the last killed rectangle (copy-rectangle-as-kill).

C-x r d Delete the text of the region-rectangle (delete-rectangle).

C-x r y Yank the last killed rectangle with its upper left corner at point (yank-rectangle).

C-x r o Insert blank space to fill the space of the region-rectangle (open-rectangle). This pushes the previous contents of the region-rectangle to the right.

C-x r N Insert line numbers along the left edge of the region-rectangle (rectangle-number-lines). This pushes the previous contents of the region-rectangle to the right.

C-x r c Clear the region-rectangle by replacing all of its contents with spaces (clear-rectangle).

M-x delete-whitespace-rectangle Delete whitespace in each of the lines on the specified rectangle, starting from the left edge column of the rectangle.

C-x r t string RET Replace rectangle contents with string on each line (string-rectangle).

M-x string-insert-rectangle RET string RET Insert string on each line of the rectangle.

C-x SPC Toggle Rectangle Mark mode (rectangle-mark-mode)

Example Text: GNU Emacs comes with ABSOLUTELY NO WARRANTY; type C-h C-w for full details. Emacs is Free Software–Free as in Freedom–so you can redistribute copies of Emacs and modify it; type C-h C-c to see the conditions. Type C-h C-o for information on getting the latest version.

When the region is in rectangle-mark-mode, C-x C-x runs the command rectangle-exchange-point-and-mark, which cycles between the four corners of the region-rectangle.

CUA

M-x cua-mode sets up key bindings that are compatible with the Common User Access (CUA) system used in many other applications.

C-x, C-c, C-v, and C-z invoke commands that cut (kill), copy, paste (yank), and undo respectively.

The C-x and C-c keys perform cut and copy only if the region is active.

Otherwise, they still act as prefix keys, so that standard Emacs commands like C-x C-c still work.

Note that this means the variable mark-even-if-inactive has no effect for C-x and C-c.

To enter an Emacs command like C-x C-f while the mark is active, use one of the following methods: either hold Shift together with the prefix key, e.g., S-C-x C-f, or quickly type the prefix key twice, e.g., C-x C-x C-f.

To disable the overriding of standard Emacs binding by CUA mode, while retaining the other features of CUA mode described below, set the variable cua-enable-cua-keys to nil.

CUA mode by default activates Delete-Selection mode (see Mouse Commands) so that typed text replaces the active region. To use CUA without this behavior, set the variable cua-delete-selection to nil.

Use C-RET to start a rectangle, extend it using the movement commands, and cut or copy it using C-x or C-c.

You can use this rectangle support without activating CUA by calling the cua-rectangle-mark-mode command.

There is also the standard command rectangle-mark-mode.

Use C-S-SPC to toggle the global mark on and off.

13. Registers

Introduction

You can view r register contents using M-x view-register RET r.

To change the length of the delay of the preview window to list the existing registers, customize register-preview-delay.

Bookmarks record files and positions in them, so you can return to those positions when you look at the file again.

Saving positions in registers

C-x r SPC r Record the position of point and the current buffer in register r (point-to-register).

C-x r j r Jump to the position and buffer saved in register r (jump-to-register).

Saving Text in Registers

C-x r s r Copy region into register r (copy-to-register).

Note: C-u C-x r s r, the same command with a prefix argument, copies the text into register r and deletes the text from the buffer as well.

C-x r i r Insert text from register r (insert-register).

M-x append-to-register RET r Append region to text in register r.

M-x prepend-to-register RET r Prepend region to text in register r.

When you are collecting text using append-to-register and prepend-to-register, you may want to separate individual collected pieces using a separator.

In that case, configure a register-separator and store the separator text in to that register.

For example, to get double newlines as text separator during the collection process, you can use the following setting.

(setq register-separator ?+)
(set-register register-separator "\n\n")

Saving Rectangles in Registers

C-x r r r Copy the region-rectangle into register r (copy-rectangle-to-register). With prefix argument, delete it as well.

C-x r i r Insert the rectangle stored in register r (if it contains a rectangle) (insert-register).

Saving Window Configurations in Registers

C-x r w r Save the state of the selected frame’s windows in register r (window-configuration-to-register).

C-x r f r Save the state of all frames, including all their windows, in register r (frameset-to-register).

Use C-x r j r to restore a window or frame configuration.

If you wish to delete these frames instead, use C-u C-x r j r.

Keeping Numbers in Registers

C-u number C-x r n r Store number into register r (number-to-register).

C-u number C-x r + r If r contains a number, increment the number in that register by number.

Note that command C-x r + (increment-register) behaves differently if r contains text. See Text Registers.

C-x r i r Insert the number from register r into the buffer.

Note: C-x r + with no numeric argument increments the register value by 1. C-x r n with no numeric argument stores zero in the register.

Keeping File Names in Registers

To put a file name into register r, use:

(set-register r '(file . name))

For example:

(set-register ?z '(file . "/gd/gnu/emacs/19.0/src/ChangeLog"))

puts the file name shown in register ‘z’.

To visit the file whose name is in register r, type C-x r j r.

Keyboard Macro Registers

C-x C-k x r (kmacro-to-register) stores the last keyboard macro in register r.

To execute the keyboard macro in register r, type C-x r j r.

Bookmarks

C-x r m RET Set the bookmark for the visited file, at point.

C-x r m bookmark RET Set the bookmark named bookmark at point (bookmark-set).

C-x r M bookmark RET Like C-x r m, but don’t overwrite an existing bookmark.

C-x r b bookmark RET Jump to the bookmark named bookmark (bookmark-jump).

C-x r l List all bookmarks (list-bookmarks).

M-x bookmark-save Save all the current bookmark values in the default bookmark file.

If you set the variable bookmark-save-flag to 1, each command that sets a bookmark will also save your bookmarks.

If you set this variable to nil, Emacs only saves bookmarks if you explicitly use M-x bookmark-save.

The variable bookmark-default-file specifies the file in which to save bookmarks by default.

If you set the variable bookmark-use-annotations to t, setting a bookmark will query for an annotation.

Bookmark position values are saved with surrounding context, so that bookmark-jump can find the proper position even if the file is modified slightly.

The variable bookmark-search-size says how many characters of context to record on each side of the bookmark’s position.

M-x bookmark-load RET filename RET Load a file named filename that contains a list of bookmark values. You can use this command, as well as bookmark-write, to work with other files of bookmark values in addition to your default bookmark file.

M-x bookmark-write RET filename RET Save all the current bookmark values in the file filename.

M-x bookmark-delete RET bookmark RET Delete the bookmark named bookmark.

M-x bookmark-insert-location RET bookmark RET Insert in the buffer the name of the file that bookmark bookmark points to.

M-x bookmark-insert RET bookmark RET Insert in the buffer the contents of the file that bookmark bookmark points to.