1

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.)

Arch Stanton
  • 1,525
  • 9
  • 22
  • 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 Jun 08 '19 at 19:11
  • @Drew I mean when I kill Emacs or the buffer. – Arch Stanton Jun 08 '19 at 19:57
  • Please put that info in the question itself. Comments can be deleted at any time. – Drew Jun 08 '19 at 20:21

2 Answers2

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.

(save-place-mode)
(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 "^")
  (save-place-find-file-hook))
(global-set-key (kbd "C-c '") #'goto-saved-place)
Arch Stanton
  • 1,525
  • 9
  • 22
1

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 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. – Arch Stanton Jun 09 '19 at 10:21
  • I'm not sure about how to read the alist, too. How about `(goto-char (cdr (assq )))`? – Arch Stanton Jun 09 '19 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](https://www.gnu.org/software/emacs/manual/html_node/elisp/Association-Lists.html) 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 Jun 09 '19 at 14:08