reddit / emacs
How to save file positions and open them later with save-place easily?
linwaytin · 2022-12-17 23:48
Ok this seems a stupid question, but I just cannot find a simple and easy way to do that. The scenario is as follows. For files like `init.el` or some template files, I do not use them so often, so they are usually not saved by `recentf`. I want to have a method to quickly open them, with the cursor at the position I was at last time. For this purpose, I use `save-place-mode` for all files.
I can set up a set of keybindings, maybe with `hydra` or similar packages, to open those files. The inconvenience is that I need to edit my init file to add those keybindings.
Another option is `bookmark`, which allows me to add files easily. The problem here is that bookmark also stores the position, and the cursor will be at the position when the bookmark is saved, which I don't want. If bookmark can work with `save-place-mode`, it would be perfect, but it seems there is no simple way to make this.
Does anyone have ideas about this problem? Thanks.
xircon · 2022-12-18 00:12
I am also interested in this, I use bookmarks extensively and have the exact (irritating) issue....
00-11 · 2022-12-18 01:47
Have you tried using (standard) `bookmark-exit-hook`?
> `bookmark-exit-hook` is a variable defined in `bookmark.el`.
>
> Its value is `nil`
>
> This variable can be risky when used as a file-local variable.
>
> Documentation:
>
> Hook run when Emacs exits.
linwaytin · 2022-12-18 01:49
No...how does this solve the problem?
mmarshall540 · 2022-12-18 01:56
It sounds like you just need a quick way to open specific files. You could create a keybinding for it, or as you say a Hydra. Or you could stick it in the menubar (assuming you don't have it disabled).
(easy-menu-add-item
'menu-bar-file-menu
nil
["Open Init File"
(lambda ()
(interactive)
(find-file user-init-file))])
redguardtoo · 2022-12-18 02:00
`(setq recentf-max-saved-items 2048)`,
linwaytin · 2022-12-18 02:05
I do have something like this in my current setting, but each time adding a file I need to edit the init.el, which I curious if i can avoid. Anyway, thanks for the suggestion.
linwaytin · 2022-12-18 02:09
Ok this might be a good workaround. Thank you.
00-11 · 2022-12-18 03:59
If you like, you can add a function to `bookmark-exit-hook` that sets/updates a bookmark in each file you're visiting (at the current position). When you exit Emacs `bookmark-exit-hook` is run, and then bookmarks are saved (before exiting).
That alone won't handle files you visited in the session but closed (killed). To take care of those you could add a function to, say, `kill-buffer-hook` that would set/update a bookmark there when the buffer is killed. When you exit Emacs all bookmarks get saved (by default), including those.
These are possibilities, using bookmarks. No others will provide other solutions for you.
cradlemann · 2022-12-18 07:51
Here is my config
(use-package savehist
:demand t
:config
(savehist-mode))
(use-package saveplace
:defer 3
:init
(save-place-mode 1)
:custom
(save-place-ignore-files-regexp
"\\(?:COMMIT_EDITMSG\\|hg-editor-[[:alnum:]]+\\.txt\\|elpa\\|svn-commit\\.tmp\\|bzr_log\\.[[:alnum:]]+\\)$")
(save-place-file (concat user-emacs-directory ".my-saved-places"))
(save-place-forget-unreadable-files t))
TiMueller · 2022-12-18 12:39
It depends on the function you use to jump to the bookmarked file.
If you for instance use `(bookmark-jump (bookmark-bmenu-bookmark))`, point is where you set the bookmark. But if you use `(find-file (bookmark-location (bookmark-bmenu-bookmark)))`, just the path of the bookmark is used and point is, where you last edited the file.
linwaytin · 2022-12-18 14:53
Got you. That is also a solution.
grLng · 2022-12-18 15:00
I'm not exactly sure about what you exactly want, but with the config below when I open any file I visited before either be during the same session or not, the cursor automatically gets back to where it was when I closed the file.
Also I have a function `my/save-place-recenter` that recenter the buffer around the cursor when I open the file.
```
(use-package saveplace
:preface
(defun my/save-place-recenter ()
"Force windows to recenter current line (with saved position)."
(run-with-timer 0 nil
(lambda (buf)
(when (buffer-live-p buf)
(dolist (win (get-buffer-window-list buf nil t))
(with-selected-window win (recenter)))))
(current-buffer)))
:custom (save-place-forget-unreadable-files t)
:config
(add-hook 'find-file-hook 'my/save-place-recenter t)
(save-place-mode 1))
```
linwaytin · 2022-12-18 15:01
Thank you. This should work.
linwaytin · 2022-12-18 15:53
Sorry I tried and still not know how to make this work. I want to have a list like consult-bookmark so that I can filter and select the bookmark.
It seems all these functions are using bookmark-jump as default. Can I change it to find-file?
linwaytin · 2022-12-18 16:04
I prefer to have a list maintained by myself like the built-in bookmark, but this is also useful. Thank you.
grLng · 2022-12-18 18:15
np, not sure why my code snippet is showing as a single line above when I use ``` though... weird