Using set-mark-command To Remember Locations

A couple of days ago I wrote a note to myself post to help me remember the align-regexp functionality. Today's post is another note to myself. A recent post by Xah Lee reminded me of some Emacs functionality that I am always forgetting. Every time I see the set-mark-command function described I think, “Boy, that's really useful, I need to remember it.” Then I promptly forget about it again.

The set-mark-command (bound to 【Ctrl+Space】) sets the mark at the point's position. The normal way of invoking it to remember locations is to type 【Ctrl+Space Ctrl+Space】. The first 【Ctrl+Space】 sets the mark and the second deactivates it so that no region is highlighted. Emacs also pushes the mark onto the buffer's mark ring and onto the global mark ring.

This is useful because we can use the marks recorded on the mark rings to return to a remembered location. If we want to return to a previous mark in the same buffer, we type 【Ctrl+u Ctrl+Space】 We can move back several locations by repeating the 【Ctrl+u Ctrl+Space】.

If we want to return to a mark which may be in another buffer we can type 【Ctrl+x Ctrl+Space】. This can also be repeated to move back several marks.

This may seem confusing but it boils down to just three key sequences

  • Ctrl+Space Ctrl+Space】 to push the current position onto the mark rings.
  • Ctrl+u Ctrl+Space】 to return to a previous position in the same buffer.
  • Ctrl+x Ctrl+Space】 to return to the previous position, which may or may not be in the same buffer.
This entry was posted in General and tagged . Bookmark the permalink.
  • xah lee

    i always forgot it to. Always wanted to one day thoroughly read that emacs doc about mark ring, few times i tried, ends in abortion.

    it happens that 2 days a go i was browsing reddit emacs section and someone mentioned point-stack-push and point-stack-pop. A quick web search i found:

    ;; 2011-07-24 from
    (defvar point-stack nil "a list to store cursor position. Each entry is a list of the form (bufferObject pointPosition)")

    (defun point-stack-push ()
    "Push current location and buffer info onto stack."
    (message "Location marked.")
    (setq point-stack (cons (list (current-buffer) (point)) point-stack)))

    (defun point-stack-pop ()
    "Pop a location off the stack and move to buffer"
    (if (null point-stack)
    (message "Stack is empty.")
    (switch-to-buffer (caar point-stack))
    (goto-char (cadar point-stack))
    (setq point-stack (cdr point-stack))))

    tried it, works, started to blog it, then i thought i really should check what emacs offers. So that's how i wrote it. Havent' really used them yet. :) What i've always been doing is simply split the window first.

    • jcs

      Yup, it's really useful. For example, when you you initiate a search the mark is set and pushed to the mark ring so you can use Ctrl+u Ctrl+Space to return to where you started. I wrote this post in the hopes that I would be able to remember how to use it.

  • Phil

    It's worth pointing out that the global mark ring stores only one mark per buffer, so you usually pop the global mark if you want to get to a different buffer.

    set-mark-command-repeat-pop is a particularly useful variable to set, or alternatively cua-selection-mode (which does not inflict the copy/cut/paste bindings upon you) provides this behaviour by default, as well as offering cua-auto-mark-last-change which optionally treats the position of the last buffer change as an implicit mark.

  • Phil

    There's also these, if you don't want to cycle through a mark ring:

    ;; C-x r SPC : point-to-register
    ;; C-x r j : jump-to-register

  • Alex

    That's really useful. What would be awesomer(?) is a list-buffers style buffer for navigating around your recently saved buffer marks. Are you aware of such a thing? A cursory google didn't show much.

    • jcs

      I'm not aware of anything like that but Emacs is so all encompasing it probably does exist somewhere. It wouldn't be hard to implement, I wouldn't think.