exchange-point-and-mark command is bound to C-x C-x, but I cannot think of any use cases that would make it deserving of such a prominent binding.
How can I use the
exchange-point-and-mark to improve my editing technique?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
There are two cases I can think of: reactivating the region, and adjusting the size of the region.
I most often use this binding to reactivate the region after performing some command that deactivates it, or doing something that sets mark and moves point without activating the region.
For example, do a C-s and search forward for something. Hit RET to exit the search, then C-x C-x to mark everything from where you started the search.
Another example that I do a lot when I want to include some sample elisp in a StackExchange answer:
The other case is when you are marking a region and want to extend/shrink it. In that case it is convenient to be able to jump to the "other end" of the region, move it, then jump back. If the region is active already then hitting C-x C-x will leave it active, allowing you to move either end.
C-h r i exchange-point-and-mark RET or, better,
C-h r i C-x C-x RET. This takes you to the information shown below.
This is what the Emacs manual, node Setting Mark says in answer to your question:
C-x C-xis useful when you are satisfied with the position of point but want to move the other end of the region (where the mark is). Using
C-x C-xa second time, if necessary, puts the mark at the new position with point back at its original position. Normally, if the mark is inactive, this command first reactivates the mark wherever it was last set, to ensure that the region is left highlighted. However, if you call it with a prefix argument, it leaves the mark inactive and the region unhighlighted; you can use this to jump to the mark in a manner similar to
And this is what it says about it in node Disabled Transient Mark:
transient-mark-mode] off switches Emacs to an alternative mode of operation:
Setting the mark, with commands like
C-x C-x, does not highlight the region. Therefore, you can't tell by looking where the mark is located; you have to remember.
The usual solution to this problem is to set the mark and then use it soon, before you forget where it is. You can also check where the mark is by using
C-x C-x, which exchanges the positions of the point and the mark - see Setting Mark.
While Transient Mark mode is off, you can activate it temporarily using
C-u C-x C-x.
C-u C-x C-xExchange point and mark, activate the mark and enable Transient Mark mode temporarily, until the mark is next deactivated. (This is the
exchange-point-and-mark, with a prefix argument.)
I use this when I've lost a selection due to some operation that removed it, and I want to restore selection. One such scenario would be:
This happens when I want to make an ad hoc backup of a selected area and to experiment on the copy, such as, for example, I want to make some find-and-replace, but I can't think of a way to do it in a way that doesn't destroy some text which is meant to remain, so I'd later revert it from the copy.
The other answers focus on the usefulness of
C-x C-x when
transient-mark-mode is active. But
C-x C-x predates
transient-mark-mode, and is useful independently of it.
The main role of
C-x C-x is to swap point and mark. This useful when you are editing two points in a single buffer and for some reason don't want to split the current window. Do some editing, set mark, move to some other point in the buffer, do some editing,
C-x C-x, do some editing at the former place,
C-x C-x, do some more editing, ad nauseam. (If you don't plan to come back to the second point,
C-u C-SPC may be more intuitive. Look up the mark ring in the Emacs manual.)
transient-mark-mode is disabled,
C-x C-x has another important purpose: it allows you to find out where the region is.
C-x C-x, quick glimpse at point,
C-x C-x again.
(And in case you are wondering — yes, some of us still prefer to use Emacs with