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 and global mark ring that records mark positions and lets you jump to it.

First, to mark a position, set-mark-commandCtrl+Space】. (do it twice to remove the highlight.) This pushes current position into the mark-ring.

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

Ctrl+u Ctrl+Space
Move cursor to previous marked position in current buffer. Repeat call will move cursor to positions in mark-ring.
Alt+x pop-global-markCtrl+x Ctrl+Space
Move cursor to previous marked position (may be in another buffer). Repeat call will move cursor to positions in global-mark-ring.
Alt+x exchange-point-and-markCtrl+x Ctrl+x
Move cursor to the other end of selection.

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

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

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

There are 2 mark rings:

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)

Tip: Single Key to Cycle Thru Marks

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

But you need to define a wrapper command first.

(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 "<f7>") 'pop-global-mark)
(global-set-key (kbd "<f8>") 'xah-pop-local-mark-ring)

[see Emacs: How to Define Keys]

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys

ELisp

ELisp Examples

ELisp Write Major Mode


Emacs Tutorial

Quick Start

Manage Windows

File

Buffer

Find Replace

Copy/Paste

Unicode

Whitespace

Rectangle Edit

Line Wrap

Shell

View Special File

Working with Brackets

Org Mode

HTML

Emacs Efficiency

Misc