I'd like to have a command that moves the cursor to the last position it was in when I left the buffer (either by killing it or by killing Emacs).

I know I can use saveplace.el to save the position for when I visit that file again, but then saveplace also takes the cursor to that position automatically, while I would like to leave the cursor at the beginning of the buffer, and move to the last position only when I want to do it, using a command. (Basically, like `" does in Vim.)

  • Define "left the buffer". Did you kill it? Just switch to another buffer? Kill Emacs. The question is unclear. If you know when you want to save a position value (which is just a number), you can save it in a variable, using, for example, save-hist-additional-variables. And depending on the when you can use a particular hook - there are hooks for buffer switching, quitting Emacs, etc.
    – Drew
    Commented Jun 8, 2019 at 19:11
  • @Drew I mean when I kill Emacs or the buffer. Commented Jun 8, 2019 at 19:57
  • Please put that info in the question itself. Comments can be deleted at any time.
    – Drew
    Commented Jun 8, 2019 at 20:21

2 Answers 2


I have written something that after some quick testing seems to be working.

save-place-mode does the automatic restoring of the cursor position by adding save-place-find-file-hook – which takes the cursor to the saved position – to find-file-hook. To change the behaviour so that the restoring can be done on demand, I removed save-place-find-file-hook from the hook and wrapped it in an interactive function.

(remove-hook 'find-file-hook #'save-place-find-file-hook)
(remove-hook 'dired-initial-position-hook #'save-place-dired-hook)

(defun goto-saved-place ()
  (interactive "^")
(global-set-key (kbd "C-c '") #'goto-saved-place)

You can put the current position in a variable of your choosing and save that variable persistently. Put a function that sets the variable value on kill-buffer-hook, since you say (in a comment) that you want to do this just before killing the buffer. Or on kill-emacs-hook.

You can add your variable for this to savehist-additional-variables, and use savehist-mode to save such variables. You can also invoke savehist-save explicitly in your hook function.

  • I think the best approach is to make that variable an alist, but I don't know how to do that. saveplace already builds an alist of files and positions, but I need to know how to disable the automatic restoring of the position on the buffer and make it available on demand. So, basically, I need some more help: either on building an alist or on fixing saveplace's behaviour. Commented Jun 9, 2019 at 10:21
  • I'm not sure about how to read the alist, too. How about (goto-char (cdr (assq <filename> <alist>)))? Commented Jun 9, 2019 at 10:32
  • Not too sure what you're asking now. I suggest you formulate it as a separate question. Any variable can have an alist as its value. My suggestion is not about saveplace but about savehist: saving a variable value persistently. And yes, you can use goto-char after getting the position associated with the file name.
    – Drew
    Commented Jun 9, 2019 at 14:08

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.