jcubic
jcubic

Reputation: 66490

How to jump to mark in Emacs

What variable or function I need to use to jump to the place of the marker set by set-mark-command C-SPC using goto-char?

(defun jump-to-mark ()
  (interactive)
  (goto-char <WHAT PUT HERE>))

Upvotes: 9

Views: 5014

Answers (4)

The exchange-point-and-mark command (bound to C-xC-x) jumps to the mark, and puts the current position (i.e. just before the jump) on top of the mark ring.

A side effect is that the region is activated. You can pass a prefix argument (i.e. press C-uC-xC-x) to avoid this.


As mentioned in other answers, another way to navigate in the mark ring consists in using C-uC-SPC, which jumps to the mark and removes it from the mark ring. Repeating the command thus makes you navigate through all successive mark positions in reverse-chronological order. However, mark positions visited that way are lost.

A sibling of C-uC-SPC is C-xC-SPC, which is very similar but acts on the global mark ring, which stores successive marks in all buffers.

Upvotes: 15

PascalVKooten
PascalVKooten

Reputation: 21443

To stay inline with your original code, you can just put (mark) instead of the WHAT PUT HERE, and it'll work. This is because (mark) returns the position of the mark (just like (point) returns the location integer of point).

(defun jump-to-mark ()
  (interactive)
  (goto-char (mark)))

Upvotes: 3

sds
sds

Reputation: 60014

I think what you are looking for is

pop-global-mark is an interactive compiled Lisp function in `simple.el'.

It is bound to C-x C-@, C-x C-SPC.

(pop-global-mark)

Pop off global mark ring and jump to the top location.

Another option is C-x C-x which runs the command exchange-point-and-mark.

Upvotes: 4

jcubic
jcubic

Reputation: 66490

I just found that it's mark-marker so my function to jump should be:

(defun jump-to-mark ()
  (interactive)
  (goto-char (mark-marker)))

Upvotes: 6

Related Questions