In my post on sorting lines, a commenter pointed out that a command would work on a region even when it was not active (highlighted). This prompted me to pay a bit more attention to how regions behave in emacs. I found this page of Xah Lee’s useful.
The key concept is that a region almost always exists in your buffer, even if it is not highlighted. The region is defined as the space between the mark and the point. The mark is the last place you placed a mark (e.g. when you use C-SPACE
to start marking text), and the point is the location of the cursor. The region is highlighted if it is active, but it always exists even if it is not active and highlighted, even though you can’t see it. Some commands can still act on the region even if it is not active.
It can be confusing at first, as you can’t see the region if it is not active, and can’t generally see the mark. One way to see the current inactive region is to use C-x C-x
which runs the command exchange-point-and-mark
. From the emacs manual, this does the following:
Set the mark at point, and activate it; then move point where the mark used to be.
So the cursor moves to where the mark was and the region is highlighted for you to see. Using C-x C-x
again puts you back where you were.
An alternative is to install the package visible-mark which highlights the position of the current mark, and an optional number of previous marks. I’m not sure I would want to use it all the time, but it is great for getting a sense of where the mark is, and also when the mark changes location, such as when you do a search, or paste text.
You can customise this by adding something like the following (taken from the package documentation) to your emacs config file:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; visible mark - show where mark is ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defface visible-mark-active ;; put this before (require 'visible-mark)
'((((type tty) (class mono)))
(t (:background "magenta"))) "")
(require 'visible-mark)
(global-visible-mark-mode 1) ;; or add (visible-mark-mode) to specific hooks
(setq visible-mark-max 2)
(setq visible-mark-faces `(visible-mark-face1 visible-mark-face2))
This shows the two most recent marks with different colour highlights (pink for the most recent and gold for the next most recent). Quite instructive!
Finally, remember that you can use C-u C-SPACE
to jump around the previous marks in the buffer, which is great for quickly getting back somewhere useful.