Transient Mark is a nothing mode

#emacs

You like transient-mark-mode? You feel that turning it off is a totally different experience? Here's what blew my mind! There's no difference! The difference is visual!

The region always exists. It's simply a term for "the text between mark and point". Since the point always exists and there's almost always at least one mark in the mark ring, the region likewise always exists. Unless you've turned on delete-selection-mode, nothing reacts any different when the region is "activated". It may be clearer to think of it as the region being "highlighted", not "activated".

Okay… I lied. There is one difference brought about by transient-mark-mode: a number of commands, such as flush-lines, change their behavior when the region is "activated". That behavior is often useful.

What behavior is this? In the majority of cases, they'll behave as if you had carried out C-x n n (narrow-to-region) prior to calling the command.

That's all there is to it.

It's narrow-to-region minus the visual consequence of narrowing, and skips having to C-x n w (widen) afterwards.

I wish I could say it was more automated than manually calling narrow-to-region, but it's actually not, as you either you have to use C-SPC (set-mark-command) beforehand, or you use C-x C-x (exchange-point-and-mark) after, which activates the preexisting region. So what's the benefit?

I mean… you either call something that calls activate-mark, or you call narrow-to-region. You have to call something either way! Aside from skipping widen, what keystrokes are saved?

And… I like the idea of activating the region afterwards, instead of before, because it requires less foresight. Plus if you never internalize the fact that the region always exists, you probably do a lot of unnecessary C-SPC and grunt cursor work to define a region to operate on, as if you were in a non-Emacs editor. Turns out the region often has close to the correct boundaries anyway, so it's better to think of C-x C-x long before you even think of C-SPC.

A few sane approaches for "defining a new region":

  • When point is already at one end of the region you want. In this case, either use C-x C-x if the mark is at the other end, otherwise use avy (which will set the mark). Or you can use C-x C-x and then adjust with avy, possibly popping the recentmost mark.
  • When point isn't at either end of the region you want. In this case, use avy twice.
  • When the region you want is semantically simple, e.g. suitable for M-h (mark-paragraph). Note that you can repeat M-h to extend to several paragraphs.
  • When the region you want is semantically simple, but you forgot the hotkey for this semantic unit. Let's say it's a sentence. Now you either need C-SPC M-e, or to use avy.

To put it differently… C-SPC isn't for such trivial use cases as "defining a region to operate on". C-SPC is for interesting use cases like Multiple Cursors' mc/mark-pop, and only for those.

Did you ever accidentally deactivate a region and have to move the cursor around as you re-define the boundaries for a new region to get back to the state you were in? Don't do that! The region is never lost, only deactivated; type C-x C-x.

(transient-mark-mode 0)
(setq set-mark-command-repeat-pop t)
(defun toggle-region ()
  "If region is inactive, activate it, otherwise the opposite.
If you have transient-mark-mode disabled, this will temporarily turn it on
until you do something to deactivate the region.

If you have transient-mark-mode enabled, this command is not really necessary,
as \\[exchange-point-and-mark] (`exchange-point-and-mark') is similar."
  (interactive)
  (if (region-active-p)
     (deactivate-mark)
   (activate-mark)))

I believe that everyone benefits from some time without transient-mark-mode, and with visible-mark-mode. It helps you approach TMM with a less limited mindset.

Productive mindset for TMM

  • You're not so aware of the active region, if there is one. You never go to the extent of deactivating it with keyboard-quit, you just do what you were going to do anyway, potentially letting the window get covered with the region highlight face for no reason.
  • You use C-SPC to set marks, not to activate region. The fact this happens to activate the region is like the fact that your car smells faintly of dog urine: try to ignore it and just drive.
  • When you need an active region, then you check if it is. Because you actually didn't know. It's like how novice drivers pay mental bandwidth to many immobile objects in their field of view, such as parked cars, failing to see a pedestrian up ahead, while for experienced drivers the pedestrian is the main object they're aware of. The region is like a parked car. You needn't be that aware of it.
  • Delete-selection-mode is anathema.

What links here

Created (2 years ago)