Emacs: Jump to Previous Position

By Xah Lee. Date: . Last updated: .

There are 3 ways to jump cursor to a previous position.

Call any one of the above repeatedly will return cursor to the position you began.

Following are details.

exchange-point-and-mark

Alt+x exchange-point-and-markCtrl+x Ctrl+x

This moves cursor to previously marked position, and activate selection (highlight it)

This is most useful when you already have a selection, and want to extend or shrink the other end.

Move Cursor to Previous Mark in Current Buffer

  1. First, mark the position you want to jump to, by set-mark-commandCtrl+Space】. (press 【Ctrl+Space】 a second time to remove the highlight (or press 【Ctrl+g】).)
  2. Scroll down a few times.
  3. Press 【Ctrl+u Ctrl+Space】 to jump to previous mark in current buffer.

Move Cursor to Previous Mark, Any Buffer

pop-global-markCtrl+x Ctrl+Space】. This one cycles global-mark-ring.

mark-ring, global-mark-ring, pop-global-mark

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

There are 2 mark ring variables:

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.

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

here's commands to jump to marks.

  1. Ctrl+u Ctrl+Space】 → jump to previous mark in current buffer. This one cycles mark-ring.
  2. pop-global-markCtrl+x Ctrl+Space】. This one cycles global-mark-ring.

Try set marks in different places, then try one of the above command repeatedly.

Tip: Single Key to Cycle Thru Marks

You can set single key to cycle thru marks, such as F7.

;; single key to jump thru marks

(defun xah-pop-local-mark-ring ()
  "Move cursor to last mark position of current buffer.
Call this repeatedly will cycle all positions in `mark-ring'.
URL `http://ergoemacs.org/emacs/emacs_jump_to_previous_position.html'
version 2016-04-04"
  (interactive)
  (set-mark-command t))

(global-set-key (kbd "M-7") 'pop-global-mark) ; Meta+7
(global-set-key (kbd "M-8") 'xah-pop-local-mark-ring) ; Meta+8
Liket it? Put $5 at patreon. Or Buy Xah Emacs Tutorial. Thanks.