Emacs: Jump to Previous Position

By Xah Lee. Date: . Last updated: .

Often, you need to go to a previous position in a buffer.

Emacs has a buffer mark ring that records mark positions and lets you jump to it.

Mark and Jump to Previous Mark

Mark a position. (do it twice to remove the highlight.) This pushes current position into the mark ring.
Ctrl+u Ctrl+Space】 (set-mark-command)
Move cursor to previous marked position in current buffer.

Repeat call move cursor to previously marked positions in current buffer, if set-mark-command-repeat-pop is true.

;; repeated C-u set-mark-command move cursor to previous mark in current buffer
(setq set-mark-command-repeat-pop t)
pop-global-markCtrl+x Ctrl+Space
Move cursor to previous marked position (may be in another buffer).

Repeat call move cursor to positions in global-mark-ring.

exchange-point-and-markCtrl+x Ctrl+x
Exchange cursor position and the last mark.

💡 TIP: often used to move cursor to the other end of text selection.

mark-ring, global-mark-ring

Emacs store cursor positions and let you jump to it quickly.

There are 2 mark rings:

Buffer Local Variable. A list of former marks of the current buffer, most recent first.
A list of saved global marks, most recent first.

mark-ring-max, global-mark-ring-max

By default, each mark ring keep 16 positions. As new mark is set, oldest is removed.

You can make it store less positions for better use of jump.

Put this in your Emacs Init File:

(setq mark-ring-max 6)
(setq global-mark-ring-max 6)

Emacs Mark Ring