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.
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:
The and global mark rings are described in sections 11.4 and 11.5 of the Emacs manual.