Say I was working in a big file somewhere in the middle and all of the sudden I realise that I need to edit the top of the file. I do a M-< to go to the top and make my edits. Now I want to move back to the exact point where I was working before.

Is it possible to call a function before moving to the top, memorising the current point then going somewhere and calling the same function again but this time the cursor is moved to the point which was previously memorised and the memory is erased; ready to memorise a new point.

Edit: Here is what I ended up with,

(defconst POINT-REGISTER 0
  "Register to save the current point in.")

(defcustom saved-point nil
  "Whether a point is saved in `POINT-REGISTER`."
  :type 'boolean)

(defun save-or-goto-saved-point ()
  "Interactively memorise a point and return to it."
  (if saved-point
      (progn (register-to-point POINT-REGISTER)
             (setq saved-point nil))
    (progn (point-to-register POINT-REGISTER)
           (message "[<Your Name>] Point saved!")
           (setq saved-point t))))

3 Answers 3


C-x r SPC (M-x point-to-register) to register the position and C-x r j (M-x register-to-point) to return to it. This last operation does not erase the contents of the register, but if you use this register for a new point, its contents will be overwritten.

Improved for more comfort

Notice that this function uses the register 0. I do not know wether this register is used or not, if so, you can change it to any unused register.

(defun my-point-excursion-toggle ()
  "Saves the position of the point or return to the previous saved   position. "
  (cond ((get-register 0)
         (jump-to-register 0)
         (set-register 0 nil))
        (t(point-to-register 0)
          (message "Save point %d"(point)))

You can bind this function to any key sequence for instance:

(global-set-key (kbd "C-c t") #'my-point-excursion-toggle)
  • It seems that point to register prompts for some kind of input?
    – scribe
    Apr 16, 2021 at 21:56
  • I have add a command for doing the trick. Since it use the register 0 for memorising the point, it can results some side effects. Its up to you to check, you can change it for any value.
    – gigiair
    Apr 17, 2021 at 6:16

Yes, C-SPC calls set-mark-command, which pushes the current location of point onto a stack. If called with a prefix argument (C-u C-SPC) it pops the stack and returns point to the popped location. A number of other commands also push point onto the stack. For example, a search with C-s pushes point onto the stack so that you can return to where you started the search easily.

  • set-mark-command also starts highlighting the region, which is what I want when I am selecting a region but not when I am just wanting to save the cursor position for coming back. Is there a way to distinguish between those two uses and not highlight/highlight accordingly?
    – scribe
    Apr 16, 2021 at 4:34
  • Sure, you can toggle transient-mark-mode, or you can just ignore the region. Commands that don’t operate on the region, such as typing, will simply turn it off again.
    – db48x
    Apr 16, 2021 at 4:39
  • Say I do C-SPC, that saves the mark, then I do M-<, that moves to the top of the file. Here I want to add import xyz. What happens is that the region is wiped out and replaced with import xyz. I don't want that!
    – scribe
    Apr 16, 2021 at 4:45
  • It sounds like you have delete-selection-mode turned on. Either turn it off, or cancel the current region with C-g.
    – db48x
    Apr 16, 2021 at 4:50
  • 1
    You can also call set-mark-command twice C-SPC C-SPC. it will save the current location and disable the region.
    – djangoliv
    Apr 16, 2021 at 7:17

You talk about calling a function. Do you want to do what you describe in Elisp or interactively?

@db48x essentially interprets your question as asking how to get back to a previous position interactively, and that answer is fine for that.

For Elisp, however, there's typically no reason to set the mark and then use it to get back to it - and that's generally avoided.

Instead, just bind a local variable to the original position, then goto-char to that position later. Or use copy-marker for the original position, and then return to that marker position later.

Please clarify your question, to say whether you want to go to a previous position by program or interactively.

  • Or save-excursion, which does it all for you.
    – db48x
    Apr 16, 2021 at 4:30
  • @db48x: Yes, or save-excursion in some cases - good point. But no, it's no substitute for the more general treatment. In particular, if you want to do some other things at the original position. Just grep the Elisp source code for opoint, for example, to see how much just binding a var to the original position is used.
    – Drew
    Apr 16, 2021 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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