19

The 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?

Matthew Piziak
  • 5,958
  • 3
  • 29
  • 77

4 Answers4

21

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:

  1. Mark the bit of code I want to copy.
  2. Hit C-u C-x TAB to indent the code by 4 spaces. That way it'll be rendered as code here on the site. This step deactivates the region.
  3. Hit C-x C-x to reactivate the region, then M-w to copy it to the clipboard so I can paste it in my 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.

glucas
  • 20,175
  • 1
  • 51
  • 83
17

Ask Emacs: 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-x is 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-x a 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 C-u C-<SPC>.

And this is what it says about it in node Disabled Transient Mark:

Turning [transient-mark-mode] off switches Emacs to an alternative mode of operation:

  • Setting the mark, with commands like C-<SPC> or 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-<SPC> C-<SPC> or C-u C-x C-x.

and:

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. (This is the C-x C-x command, exchange-point-and-mark, with a prefix argument.)

Drew
  • 75,699
  • 9
  • 109
  • 225
8

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:

  1. Select rectangle.
  2. Kill it.
  3. Undo killing rectangle.
  4. C-x C-x to restore selection to rectangle.

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.

wvxvw
  • 11,222
  • 2
  • 30
  • 55
8

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.)

When 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 transient-mark-mode disabled.)

jch
  • 5,680
  • 22
  • 39