TransientMarkMode

Transient Mark mode gives you much of the standard selection-highlighting behavior of other editors. In GNU Emacs 23 and onwards, it is enabled by default. Otherwise, to enable it, put the following in your init file: (transient-mark-mode 1)

In Transient Mark mode, the region is active only temporarily, and it is highlighted while it is active. You activate the region for each command that uses it: place the mark, then move the cursor (and point).

Some features in Emacs support extra behaviors or require it to be on.

Highlighting the region would not be appropriate without Transient Mark mode. Because the region always exists (as soon as the mark exists), without a notion of active region, some text would always be highlighted (except when mark = point), which would prove to be a nuisance.

In Emacs 22 and later, ‘C-SPC C-SPC’ activates Transient Mark mode only temporarily. This means that even if you do not use Transient Mark mode, you can get the effect of it for the space of one command by doing ‘C-SPC C-SPC’.

See also:

Activating Transient Mark Mode Temporarily Prior to Emacs 22

This code snippet is from Emacs CVS.

(defun push-mark-command (arg &optional nomsg)
  "Set mark at where point is.
If no prefix arg and mark is already set there, just activate it.
Display `Mark set' unless the optional second arg NOMSG is non-nil."
  (interactive "P")
  (let ((mark (marker-position (mark-marker))))
    (if (or arg (null mark) (/= mark (point)))
	(push-mark nil nomsg t)
      (setq mark-active t)
      (run-hooks 'activate-mark-hook)
      (unless nomsg
	(message "Mark activated")))))
(defun set-mark-command (arg)
  "Set mark at where point is, or jump to mark.
With no prefix argument, set mark, and push old mark position on local
mark ring; also push mark on global mark ring if last mark was set in
another buffer.  Immediately repeating the command activates
`transient-mark-mode' temporarily.

With argument, e.g. \\[universal-argument] \\[set-mark-command], \
jump to mark, and pop a new position
for mark off the local mark ring \(this does not affect the global
mark ring\).  Use \\[pop-global-mark] to jump to a mark off the global
mark ring \(see `pop-global-mark'\).

If `set-mark-command-repeat-pop' is non-nil, repeating
the \\[set-mark-command] command with no prefix pops the next position
off the local (or global) mark ring and jumps there.

With a double \\[universal-argument] prefix argument, e.g. \\[universal-argument] \
\\[universal-argument] \\[set-mark-command], unconditionally
set mark where point is.

Novice Emacs Lisp programmers often try to use the mark for the wrong
purposes.  See the documentation of `set-mark' for more information."
  (interactive "P")
  (if (eq transient-mark-mode 'lambda)
      (setq transient-mark-mode nil))
  (cond
   ((and (consp arg) (> (prefix-numeric-value arg) 4))
    (push-mark-command nil))
   ((not (eq this-command 'set-mark-command))
    (if arg
	(pop-to-mark-command)
      (push-mark-command t)))
   ((and set-mark-command-repeat-pop
	 (eq last-command 'pop-to-mark-command))
    (setq this-command 'pop-to-mark-command)
    (pop-to-mark-command))
   ((and set-mark-command-repeat-pop
	 (eq last-command 'pop-global-mark)
	 (not arg))
    (setq this-command 'pop-global-mark)
    (pop-global-mark))
   (arg
    (setq this-command 'pop-to-mark-command)
    (pop-to-mark-command))
   ((and (eq last-command 'set-mark-command)
	 mark-active (null transient-mark-mode))
    (setq transient-mark-mode 'lambda)
    (message "Transient-mark-mode temporarily enabled"))
   (t
    (push-mark-command nil))))
(setq set-mark-command-repeat-pop nil)

Commands that Do Not Work Well with Transient Mark Mode

indent-rigidly (prior to Emacs 23 only)

  1. Set mark (C-SPC)
  2. Move the cursor a couple of [[lines?]] down
  3. C-x TAB

In GnuEmacs prior to Emacs 23, if ‘transient-mark-mode’ is on, the above works only one time. Consecutive presses of ‘C-x TAB’ beep at you (“The mark is not active now”).

In order to keep the region highlighted after ‘C-x TAB’ finishes, you can use this advice:

   (defadvice indent-rigidly (after deactivate-mark-nil activate)
     (if (called-interactively-p)
         (setq deactivate-mark nil)))

Trouble Activating the Region at the End of a Command

I wrote a small function called ‘duplicate-region’ (see DuplicateStartOfLineOrRegion), and I’d like the mark to be active after calling it.

Transient mark mode is on, I use ‘push-mark’ and ‘exchange-point-and-mark’, (I tried a few other options too) but I can’t make it work. Any idea ?

Cheers! – SebastienRoccaSerra

  (setq deactivate-mark nil)

DrewAdams

Thanks, done! – SebastienRoccaSerra

How do you make C-x C-x turn on highlighting, without changing anything else?


CategoryRegion