Text manipulation
In this section, learn some of the special commands and key bindings for editing text, most of which work regardless of the current major mode.
Regions can be automatically indented in different ways. The
indent-rigidly function, which you run by typing
C-x C-i, indents all lines in the region to the right
by one space.
Try it:
-
Type
C-Spaceto set the mark at the bottom of the buffer. -
Move point to the beginning of the line that reads, "Little lamb, I'll tell thee," to mark the last 10 lines in the buffer as the region.
-
Type
C-x C-ito indent the region one space. -
Type
C-x C-iagain to indent the region another space.
Just as you can run indent-rigidly multiple times,
you can give a certain number of spaces to indent by preceding it with
C-u followed by a number; use a negative number to
move the region toward the left.
Try it:
-
Indent the region by 10 more spaces: Type
C-u 10 C-x C-i. -
Take the indentation back five spaces: Type
C-u -5 C-x C-i.
After this, your buffer should look like Figure 4.
Figure 4. Inserting rigid indentation
The indent-rigidly function is also bound to the
C-x Tab.
To fill the region, justify the text with a ragged right margin and run
the fill-region function. A similar function,
fill-paragraph, applies to the current paragraph. It
has a keyboard equivalent of M-q.
Try it: Type M-x fill-region. Notice that once the
region is filled, point is moved to just after the region.
Like any of the Emacs formatting commands, the commands for indentation and
filling can be undone with the undo function, which
was described in the first tutorial in this series. Try it once now to undo the
region fill: Type C-_ once.
Just as you can add or remove vertical space with indentations, you can also
take out any existing horizontal space. To do this, use the
delete-horizontal-space function,
M-\ (that's the Meta key followed by the \ key). It
removes all leading whitespace between the first nonspace character anywhere to
the left of point and the first nonspace character to the right of point, no
matter where point happens to be in the space.
Go ahead and try it. Take out all the leading spaces in the buffer by moving
point to the space on each line and typing M-\ before
moving to the next line with spaces.
To add a line of vertical space, you can always press Enter, but doing
so also moves point. To add vertical space without moving point, run the
open-line function, which is bound to
C-o.
Try it: Move down eight lines from the top of the buffer so that point is at
the beginning of the line reading "Little lamb, I'll tell thee," and type
C-o. Now your buffer should look like
Figure 5.
Figure 5. Remove horizontal space and insert vertical space
Emacs has commands for transposition, which allow you to exchange the characters, words, or lines immediately after point with those immediately before point. These are good commands for making quick corrections in text.
Use C-t, the
transpose-chars function, to transpose the single
character before point and the single character after point; use
M-t, the transpose-words
function, to transpose the single words before and after point.
Try it now:
-
Move point to the o in For and then type
C-tto transpose the o and F characters. -
Type
C-tagain to transpose the r and F characters. -
Move point back to the F and type
C-tonce to move the F back one character. -
Move point back to the F again and type
C-tonce more to move the F back to the beginning of the line. -
Type
M-tto transpose the words For and he. -
Type
M-tagain to transpose the words For and calls. -
Move point between he and For and type
M-tagain to transpose them. -
Move point between calls and For and type
M-tagain to transpose them. -
Type
M-tone last time to transpose calls and he.
The transpose-lines function,
C-x C-t, transposes the line at point and the line
before it.
Try it: Move point to the beginning of the line that begins "I a child" and
type C-x C-t.
Note that you can also precede any of these commands with a numeric value by
using the universal argument, C-u: C-u 2 C-x C-t
transposes the line at point with the line two lines ahead of point.
Another useful command, delete-indentation, joins
the line at point with the previous line, separated by a space character. It's
bound to M-^, which you type by pressing and holding
Meta and using the Shift key to type a caret (^). An alternate,
C-1 M-^, joins the current line with the line that
follows it.
There are several Emacs functions for converting case. The
uppercase-word function (bound to
M-u) converts to uppercase letters of all the text
beginning from point forward to the end of the word. Similarly,
downcase-word (bound to
M-l) converts everything from point to the end of the
word to lowercase letters.
Try these on the buffer:
-
Move point to the line that begins with "For he calls" and type
M-c M-c M-l M-c M-l M-cso that the line is properly capitalized. -
Move point down to the line that begins with "We are called" and type
M-u M-u M-u M-u M-u M-uso that the entire line is converted to uppercase letters. -
Type
C-ato move point to the beginning of the line and typeC-c C-l C-l C-l C-l C-c C-lso that the line is properly capitalized.
You can operate on words before point by preceding any of these commands
with the negative-argument function, M--, which you
type by pressing and holding the Meta key and then pressing a hyphen character
(-). You can apply the case-changing commands to the region by using the
downcase-region and
upcase-region functions, which are bound to
C-x C-l and C-x C-u,
respectively.
Summary of text manipulation commands
Table 3 lists the various text manipulation commands you've just learned and describes their meanings.
Table 3. Emacs text manipulation commands
| Binding | Command or function | Description |
|---|---|---|
C-x C-i, C-x Tab
|
indent-rigidly
| This command indents lines in the region (or at point). |
fill-region
| This command fills all paragraphs in the region. | |
M-q
|
fill-paragraph
| This command fills the single paragraph at point. |
M-\
|
delete-horizontal-space
| This command removes any horizontal space to the right and left of point. |
C-o
|
open-line
| This command opens a new line of vertical space below point, without moving point. |
C-t
|
transpose-chars
| This command transposes the single characters to the right and left of point. |
M-t
|
transpose-words
| This command transposes the single words to the right and left of point. |
C-x C-t
|
transpose-lines
| This command transposes the line at point with the line before it. |
M-^
|
delete-indentation
| This command joins the line at point with the previous line. Preface with
C-1 to join the line at point with the
next line. |
M-u
|
uppercase-word
| This command converts the text at point to the end of the word to uppercase letters. |
M-l
|
downcase-word
| This command converts the text at point to the end of the word to lowercase letters. |
C-x C-l
|
downcase-region
| This command converts the region to lowercase letters. |
C-x C-u
|
upcase-region
| This command converts the region to uppercase letters. |




