Hidden Emacs Gems

# Mark Rings in Emacs

The first version of Emacs was released in 1976. Thus, this piece of software is among the oldest actively developed projects and is way older than perhaps the majority of its users (including my humble self). This is particularly remarkable given that the younger crowd tends to prefer IDEs like Eclipse.

Emacs comes with a steep learning curve. Even if you have been using it for productive software development for years, you can count on its built-in guarantee of wow-didnt-know-that-one moments. Just recently, I stumbled across the M-x cd function that allows you to change the directory. While in practice the function is not of much use, I was still amazed that I had not encountered it before.

In contrast, the mark ring is an often overlooked concept that is definitely worth considering for your daily workflow. Just like Emacs keeps track of your kill-commands in a kill ring, Emacs also creates a mark ring that saves locations, where you have previously set/unset marks (using C-<SPC>). Once you have accumulated a few marks, you can then cycle through the ring as known from the kill ring. Two distinct instances of exist:

1. The basic mark ring has buffer scope only (use C-u C-SPC or C-u M-x set-mark-command).

2. The global mark ring saves marks globally. That means, you potentially jump to a completely different buffer (use C-x C-@, C-x C-SPC or M-x pop-global-mark).

The mark ring is ideal to jump back to locations that you have previously edited. This can be particularly helpful if you are actually not fully aware of where you ended up in a nested code base after several semantic-symref lookups. Once you got what you have been looking for, just jump back to your current construction site. If you are planning ahead, you can leave a mark via C-SPC C-SPC such that you can revisit the location later.

For me, the mark ring gets a rating of occassional time saver. In most instances you need to return to the previous context only. This is typical during a C-s-search, where you simply return to your previous location via C-g. If you are editing some code and need to investigate something, you would split the buffer into two side-by-side buffers using C-x 3. Then you can edit in one buffer and navigate in the other buffer.

updated on 2016-01-13 based on correction by xezzy

From: xezzy
2016-01-06 20:50
Small typo: the key sequence is C-Space, not M-x Space.
For me, the behavior of global mark ring is confusing. I don't understand when the mark set up by C- C- makes an entry in the global ring.
The Emacs Manual makes me belive that only the first mark after switching into the buffer, and all consecutive marks don't go into the global ring, unless a switch the buffer again, and so on.
When I experiment with this, however, it doesn't seem to be the case. I cannot seem to pinpoint the real behavior, or maybe I don't understand this particular section of the manual.
Do you have any advice on this?
From: xezzy
2016-01-06 20:59
Hmm... I think I see the logic now.
If I think of it not as
"first mark after visiting the buffer gets into the global ring",
but as
"mark gets pushed into the global ring if the previous mark was in another buffer"
It brings a lot of clarity, even though practically it means the same thing. Then, the global mark ring and C-x C- really is for retracing your steps "across buffers". When you're finally in a buffer that you want to be in, you can use C-u C- to cycle through buffer-local mark ring to jump within the buffer.
From: andre
2016-01-13 23:43
Hi xezzy. Thanks for pointing out the typo and sharing your thoughts on the mark ring. To be honest, the mark ring could not manifest itself in my daily workflow. Currently, I am considering whether it could be useful when recording involved keyboard macros.