Modify the command execution of Emacs
As you begin to look at some of the advanced techniques for text editing with Emacs, the first thing to learn is how to alter regular command execution in Emacs, including those commands you already know. The techniques that follow fill in a few blanks from material you learned in the first two tutorials in this series, showing you a few techniques related to altering commands before, during, and after they're run.
You know that the universal argument precedes a command with a number, as
described in the first tutorial in this series (see
Resources). But there's another way to do it that
can be quicker when you just want to specify a single digit: Use the
digit-argument function, which you type with the Meta
key in conjunction with a single digit:
The preceding command runs the
C-n, five times.
To specify a single digit, use
way is quicker than using the universal argument command (in this example, it
C-u 5 C-n) because it requires pressing one
Try it now on the practice file from the first tutorial in this series, which you saved to a file named practice.b:
Start Emacs with the file:
$ emacs practice.b
- Move the cursor down three lines by stepping it with:
- Move the cursor backward five characters:
M-- M-5 C-f
Notice that a negative number makes a given command work in reverse. (You can get the same result with
Move to the end of the buffer by typing:
M-9 M-9 M-9 C-f
Notice that you can specify longer, multidigit numbers by combining several Meta keystrokes in a row -- this is even quicker than using the universal argument.
Insert several blank lines: Type
M-4followed by the Enter key.
Indent eight spaces: Type
M-8followed by the spacebar.
Insert three dashes with
Your buffer should now look like the one in Figure 1.
Figure 1. Emacs editing with the digit-argument function
repeat function is useful for intensive editing.
As its name implies, it repeats the last command you typed.
Try running a command and repeating it:
C-bto move the cursor back one character.
- Repeat it once with
You can also repeat a command repeatedly -- do this by typing a
z again to repeat the command a second time; keep
z to repeat it again.
zto move the cursor back one character.
zmultiple times to move the cursor back several characters.
C-fto move the cursor forward one character.
C-x zto repeat the
forward-charonce, and then type
zto repeat until Emacs beeps to indicate that you've reached the end of the buffer.
If you precede the
repeat command with a number --
either given with the Meta key or through the universal argument function -- you
repeat the last command that many times.
Try it by typing
C-b M-5 M-0 C-x z to move the
cursor back 50 characters.
You learned about the various Emacs commands for searching and replacing text
in the second tutorial in this series, "Emacs editing environment, Part 2: Emacs
modes and editing features" (see Resources). As
described in that tutorial, the
function replaces one given string with another;
query-replace (bound to
M-%) does the same thing, but it prompts you before
making each replacement, letting you decide whether a given replacement should
When you run
query-replace, one of the options
available while making a given replacement is the recursive edit, which
is a special Emacs feature that lets you suspend the current editing task so
that you can interactively edit the buffer while the task waits in the
background. To indicate that you're in a recursive edit, the current mode in the
mode line is surrounded by square brackets. Exit the recursive edit by typing
exit-recursive-edit function; when you do so, the
square brackets are removed from the mode line, and you're back to the original
editing task or context from before you entered the recursive edit.
You can enter a recursive edit at any time by running the
recursive-edit function. As the name implies, you can
nest any number of recursive edits; each time you do, the current mode in the
mode line is enclosed by a new set of brackets.
In the case of
query-replace, you type
C-r at the query prompt to enter a recursive edit.
This lets you stop before a particular replacement is made so that you can edit
the buffer; when you exit the recursive edit, you're brought back to the
Try it now:
M-%to run the
- Answer the queries in the minibuffer:
Query replace: o
Query replace o with: a
- At the first replacement, get into the recursive edit by typing
- Query replacing o with a: (? for help) C-r
- Move to the end of the buffer by typing
M->and then type
William Blokeafter the three dashes.
C-M-cto exit the recursive edit.
nto decline replacement of the first match.
- The next match found is the o in Bloke that you typed after
query-replacefunction ran; type
yto replace it.
When this function exits, the final portion of your buffer should look like Figure 2, and Emacs should report in the minibuffer that it replaced one occurrence of the given string.
Figure 2. After a recursive edit during an Emacs query replace