Backtracking through the Emacs mark ring

June 24, 2010

Here’s a really useful Emacs tip that is easy to miss.

All Emacs users know that C-SPC sets the mark (set-mark-command). But not everybody knows that the mark is not a single value. Every time a new mark is set, it is also pushed to the mark ring. The really useful thing about this is that you can move back through that ring by giving a prefix argument to set-mark-command.

So:

C-u C-SPC

will take you back to the previous place you set the mark. It turns out that this is often what you want. For example, this could be where you last yanked a block of text or started an isearch. If you want to go back further, just repeat the jump.

An example: Say I’m editing somewhere in the middle of a long file and want to do an isearch from the beginning of the buffer. The sequence of commands would then be:

M-< (sets the mark, then jumps to beginning of buffer)

C-s (do the isearch, will also set the mark unless cancelled)

Make a few changes

C-u C-SPC C-u C-SPC (jump to previous mark twice)

I should mention that there are in fact two mark rings in Emacs: A local and a global one. Local and global in this context refers to buffer – the local ring is a per-buffer ring, the global one is across buffers.

You can backtrack along the global one with pop-global-mark:

C-x C-SPC

The and global mark rings are described in sections 11.4 and 11.5 of the Emacs manual.

About these ads

4 Responses to “Backtracking through the Emacs mark ring”


  1. This is awesome!!!!!!!! I’m already using it. Yet again it’s just the thing and it already exists!


    • What I’d like to master now is the buffer-ring — does it exist? I’d like to be able to efficiently change buffers without resorting to ido mode or any other form of completion. For example, if the buffer I want is two down in the buffer list, can a “C-u” paired with “C-xC-b” get me there? I looked up buffer-ring in the manual and found nothing. I will look some more and let you know.

      • chopmo Says:

        I’m not sure “buffer-ring” is the term used, but the buffers are arranged in a cyclic buffer with the most recently visited buffers at the top.

        To illustrate, the description of next-buffer is “Switch to the next buffer in cyclic order.”

        I have bound S-C-n and S-C-p to next-buffer and previous-buffer, respectively:

        (global-set-key (kbd “C-S-n”) ‘next-buffer)
        (global-set-key (kbd “C-S-p”) ‘previous-buffer)

        Maybe this would be useful to you?


  2. chopmo :

    I have bound S-C-n and S-C-p to next-buffer and previous-buffer, respectively:
    (global-set-key (kbd “C-S-n”) ‘next-buffer)
    (global-set-key (kbd “C-S-p”) ‘previous-buffer)
    Maybe this would be useful to you?

    I’m mainly interested in more effective mouse usage, although I will explore the keyboard commands more thoroughly. Thanks!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: