Manipulate the mark
Setting the mark and making selections are important, related Emacs concepts that you learned about in the first tutorial in this series (see Resources). But there's more to it than that, and this section describes some advanced editing techniques related to these topics.
You know that
set-mark-command function, sets the mark at point.
But when you continue editing, it's sometimes useful to know exactly where in
the buffer the mark has been set -- and it's also handy to go back to the mark,
if you set it at a point and want to refer to it for some reason. Emacs
currently doesn't have a way to visualize where the mark is set, but there are
ways to move point to the mark, which solves both problems.
You can do this a few ways. One is to precede
C-space with the universal argument
C-u command. Doing so changes the meaning of
C-space -- instead of setting the mark, it
moves to the mark.
The last thing you did in your practice buffer was yank a rectangle (see
the preceding section), so point moved to the
lower-right corner of the rectangle and the mark was set at the upper-right
corner. Try moving to the mark now by typing:
This is a good, fast technique, but it's not convenient if, after you move to
the mark, you want to return back to where point was -- that position is lost.
But there's another way to move to the mark that remembers the position of point
and is useful for such occasions. Use the
exchange-point-and-mark function, which is bound to
C-x C-x. It works the way it sounds: It moves point
to the mark but, at the same time, it sets a new mark at point.
Try it now:
M-< C-spaceto set the mark at the top of the buffer.
M-5 C-nto move away from the mark.
- Exchange point and mark: Type
- To move back to the last location of point -- and to set the mark back at
the top of the buffer -- run the command again: Type
- The mark is set to the top of the buffer again. If you run this command
again without setting a new mark, you go back to the top of the buffer no
matter where you've moved point. Try it by moving point and then running this
command again: Type
M-9 C-f C-x C-x.
You can use this command as a quick and powerful way to "see" where the mark is
set, by typing it twice: The first time you type
C-x C-x you move the cursor to the mark, but you also
set the mark at the point where you were. The second time you type
C-x C-x you set the mark back where it was, and you
move to the point where you were in the beginning.
Emacs has a few special functions that mark certain types of textual units. They're worth knowing about and are described here.
All of these mark commands can be preceded with digits to modify the number of units they mark and their position, either to the right of the current point (positive digits) or to the left (negative digits).
mark-word function marks from point to the end
of the current word and is bound to
M-@ (press and
hold the Meta key and type the
It's not useful for quickly cutting out a word -- you should still use
M-d to do that, as you learned in a previous tutorial
-- but it's a good command for marking a single word to perform some other
operation on it, such as case conversion with the
downcase-region functions, which convert all the text
in the region to uppercase and lowercase characters, respectively.
- Move to the last line in the buffer, right before the W: Type
M-> M-b M-b.
M-2 M-@to mark the two words to the right of point. (If you hadn't just moved to the left of these words, you could accomplish the same thing with
M-- M-2 M-@.)
- Run the function to convert the region to all uppercase letters: Type
M-x upcase-regionand press Enter.
- Notice that a new window opens, telling you that the
upcase-regionfunction is disabled; this is because it can be confusing to new users. You're well past that now, so type
yto enable it for this session. Then, if you like, type
yagain when you're prompted for whether you'd like to enable it for future sessions, too.
M-h, marks the current paragraph as the region. Point
can be anywhere in the paragraph when you type this. This function sets the mark
to the end of the paragraph and moves point to just before the paragraph.
Try marking a paragraph:
- Move to the last stanza with
M-5 C-p, which moves point to the second-to-last word on the last line.
M-hto select this paragraph.
- Notice that point moves to the blank line just before the stanza. The mark
is set to the line immediately preceding the stanza -- type
C-x C-xto verify that.
To mark the entire buffer at once, run the
Try it now:
C-x h C-w C-y marks the entire buffer as
the region, kills the region (so the entire buffer is killed), and then yanks
This technique is good for copying an existing buffer in its entirety into
another buffer. Do this by marking the buffer as the region and then typing
M-w to run the
kill-ring-save function. Then, you can paste it
somewhere else, even in another buffer.
You just learned that typing
C-u C-space returns to
the last mark that was set (see "Move to
the mark"). But
Emacs remembers more than the last mark -- it remembers the last 16 marks you
set, and it keeps them in a special place called the mark ring.
When you type
C-u C-space multiple times, Emacs
cycles through those 16 locations in the mark ring. After you type this command
16 times, Emacs loops around to the first mark in the ring.
Each buffer has its own mark ring that keeps the last 16 marks set in that particular buffer.
C-u C-space multiple times to cycle
through some of the last marks set in your practice buffer.
As long as a mark is set somewhere in the buffer, there is a region -- as you've learned, the current region is the area between the mark and the current point -- but the region is invisible, so you can't normally see it. One way you can see the region is when you first select it by dragging with the mouse -- and that only lasts until you press a key.
But you can make the region visible by running the
transient-mark-mode function, which is a toggle. When
this minor mode is set, the region is highlighted whenever you set the mark, and
it remains highlighted while you adjust it, such as by using the cursor motion
keys. As soon as you type another key that changes the buffer and doesn't have
to do with the region, such as when you type a regular alphanumeric character to
insert it at point (or if you type
highlighting disappears and the region is cleared, although the mark still
If you set the mark again or if you type a command that has to do with the region -- such as exchanging point and mark -- then the region is set and it is highlighted in the buffer again.
Try it now:
M-x transient-mark-modeto enable this mode.
- Move to the end of the buffer and set the mark: Type
- Move the cursor up by typing
C-pa few times and watch the mark grow.
- Move to the top of the buffer and insert an empty space: Type
M-< C-o. Notice that the region is no longer highlighted.
C-wto kill the region and notice how nothing is killed -- instead, Emacs beeps and reports in the minibuffer, "The mark is not active now."
- But you still have a mark set -- type
C-x C-xto demonstrate this, and watch how the region is quickly highlighted again.
Your Emacs session should look like Figure 3.
Figure 3. A visible region in Transient mark mode
Notice that this is one minor mode that doesn't write in the mode line
when it's activated. Type
Back in the first tutorial of this series, you learned how to set the mark and manipulate the region, and you also learned how to use the mouse to make selections. But when you're using X, there's another way to select and kill text with the mouse, without using the mark and point or even setting the mark or moving point at all. This is by using the secondary selection, which is a contiguous region of selected text whose ends aren't delineated by the mark and the current point. You make a secondary selection by holding down the Meta key while you use the mouse to make a selection.
You'll notice that the secondary selection is highlighted in a different color than the normal X selection.
Try making a secondary selection now:
- Press and hold the Meta key.
B1at the beginning of the first stanza and drag it to the end, and then let go of both the Meta key and the mouse button.
When you do this, point isn't moved from where it was.
You can also make a secondary selection without dragging. To do so, press and
hold the Meta key, click
B1 at the beginning,
B3 at the end, and release the Meta key.
There are a few more shortcuts for selecting words and lines as the secondary
selection, and they're analogous to the same mouse operations used to make a
regular selection: Press and hold the Meta key and then double-click the
first button (
M-B1-B1) when the mouse pointer is on a
word to select the entire word, and press and hold the Meta key while you
triple-click somewhere on a line (
select the entire line.
Try it now: Press and hold the Meta key and then double-click the word Tiger in one of the places it appears in the buffer. Notice, again, that point doesn't budge from its position.
To paste the secondary selection, press and hold the Meta key and then
B2 at the point in the buffer where you want to
paste it. When you do this, the cursor moves to the end of the selection you
paste. You can also paste it in another X client window.
Try it by clicking
B1 between WILLIAM and
BLAKE, pressing the spacebar to insert an extra space; then press and
hold the Meta key and click
B2 to paste the
secondary selection there.
Table 2 provides a summary of the advanced mark and selection commands that you just learned. It gives their function name, if applicable.
Table 2. Advanced Emacs mark and selection commands
||Moves to the previous mark in the mark ring.|
||Swaps the location of point and the mark.|
||Marks all text from point to the end of the current word.|
||Marks the current paragraph, regardless of the location of point.|
||Toggles Transient Mark mode.|
||Marks the entire buffer, regardless of the location of point.|
||Sets the beginning of a secondary selection; drag to make a secondary selection.|
||Sets the end of a secondary selection.|
||Marks a word as the secondary selection.|
||Marks a line as the secondary selection.|