24

Can I make my cursor in a buffer jump back to its last position? (preferrably without having to mark the previous position)

For example, C-a jumps to the beginning of the current line, can I jump back to where it was before?

For example, when C-x C-f to open a file in a different directory, i want to jump back to the previous position in the path name.

Thanks.

  • Will C-x x which runs exchange-point-and-mark do? Emacs doesn't store all cursor movements, only those you specifically told it to. Adding and popping marks is one way of storing cursor movement history. emacswiki.org/emacs/MarkCommands - here's a good discussion of the subject. – wvxvw Mar 9 '15 at 16:07
  • 1
    C-x x is undefine. M-x exchange-point-and-mark: no mark set in this buffer. – Tim Mar 9 '15 at 16:09
  • @Tim It is C-x C-x. Also you need to set a mark first for that to work. To set a mark explicitly, you do C-SPC. To jump back to the previous mark, you do C-u C-SPC. – Kaushal Modi Mar 9 '15 at 16:19
  • Yeah, right, sorry, it was C-x C-x. C-x x is my own binding. – wvxvw Mar 9 '15 at 16:37
  • 1
    Not sure if this is a duplicate, but emacs.stackexchange.com/q/3421/454 might be useful. – phils Mar 9 '15 at 21:50
30

Here are few mark-related bindings that can help you:

  • C-SPC - set mark and activate region (bound to set-mark-command)
  • C-SPC C-SPC - set mark (activates and then deactivates region)
  • C-x C-x - bound to exchange-point-and-mark
  • C-u C-SPC - move to previous mark

Check out the Mark documentation for better understanding of this topic.

  • 9
    Note that commands which are liable to move the cursor some arbitrary (but potentially large) distance from where you were will (typically) automatically push the mark first. So you can frequently get back to where you were before with a single C-u C-SPC. – phils Mar 9 '15 at 21:47
  • With counsel-mark-ring it's also possible to list all the marks and navigate them with cursors, getting a real-time preview in the buffer. – Arialdo Martini Jul 2 at 5:57
6

If you're doing editing at the locations you land on, goto-last-change will do this for you. http://www.emacswiki.org/emacs/GotoLastChange

  • 2
    In Spacemacs it's bound to g ; – iLemming Jun 12 '18 at 23:22
1

You can use `Registers'.

From emacs info file

13.1 Saving Positions in Registers

C-x r <SPC> R' Record the position of point and the current buffer in register R (point-to-register').

C-x r j R' Jump to the position and buffer saved in register R (jump-to-register').

Typing C-x r <SPC>' (point-to-register'), followed by a character `R', saves both the position of point and the current buffer in register R. The register retains this information until you store something else in it.

The command `C-x r j R' switches to the buffer recorded in register R, and moves point to the recorded position. The contents of the register are not changed, so you can jump to the saved position any number of times.

If you use C-x r j' to go to a saved position, but the buffer it was saved from has been killed,C-x r j' tries to create the buffer again by visiting the same file. Of course, this works only for buffers that were visiting files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.