Overriding Emacs' Broken Bookmark Position Code (Updated) (Shallow Thoughts)

Akkana's Musings on Open Source Computing and Technology, Science, and Nature.

Sun, 28 Jun 2026

Overriding Emacs' Broken Bookmark Position Code (Updated)

(Note: I've updated this post: see the solution at the end.)

Emacs has a useful function called bookmarks, where you can make short names for files you visit often.

But bookmarks has one terrible misfeature: it also remembers your position in the file.

That sounds like a good thing, right? But the problem is that the bookmarks system only records these positions sporadically. So it's easy to get stuck on a position you were editing months ago.

For example: I have a bookmark for the file where I keep track of appointments and other calendar entries. But lately, every time I open this bookmark, it opens it with the cursor positioned on September 24. That's three months away; its not the part of the file I'm interested in right now.

Checking ~/.emacs.d/bookmarks, that file hasn't been modified since May 30.

So what I think is happening: emacs only saves the bookmarks file when you add a new bookmark. Probably I defined a new bookmark on May 30, and maybe I happened to have my calendar file loaded then; and maybe I'd just added the dentist appointment on Sep 24. So emacs saved the bookmarks file, noting my then-current position in the file.

And then I'm stuck with that being my starting position every time I load that file, for many months hence, until I have occasion to define some new bookmark (I add new bookmarks maybe a few times a year).

How could anybody have thought that was the right behavior? I'd be fine with always starting at the beginning and not remembering my last position... but starting at some point that happens to be where I was on some day several months ago? Who would want that?

Glancing through the code, in bookmark.el there's a comment near the top:
;; Type `M-x customize-group RET bookmark RET' for user options.
So I tried that.

I was hoping to find an option to just not restore position, but here's nothing like that. What it does have is

Bookmark Save Flag: Choice: Value Menu Other
State : STANDARD.
Controls when Emacs saves bookmarks to a file.
--> nil means never save bookmarks, except when ‘bookmark-save’ is explicitly called (M-x bookmark-save).
--> t means save bookmarks when Emacs is killed.
--> Otherwise, it should be a number that is the frequency with which the bookmark list is saved (i.e.: the number of times which Emacs’s bookmark list may be modified before it is automatically saved.). If it is a number, Emacs will also automatically save bookmarks when it is killed.

Therefore, the way to get it to save every time you make or delete a bookmark is to set this variable to 1 (or 0, which produces the same behavior.)

To specify the file in which to save them, modify the variable ‘bookmark-default-file’.

Okay, great! If I set that to t, it will re-save the file every time I exit emacs, so those positions will get reset.

So I clicked on Value Menu. Oops! The choices there are nil, Integer, and Other. No t. Maybe clicking on Other will let me enter t? Nope, it doesn't do anything as far as I can tell.

But I can put a setq in my .emacs, right? Except — nowhere does it tell me the actual name of the variable. I can't even tell what the current (default) value is: what is STANDARD supposed to mean?

Some web searching found a page called bookmarks in the emacs manual. It says "If you set the variable bookmark-save-flag to 1, then each command that sets a bookmark will also save your bookmarks; this way, you don't lose any bookmark values even if Emacs crashes. (The value, if a number, says how many bookmark modifications should go by between saving.)" No mention of a t option. Nevertheless, I tried

(setq bookmark-save-flag t)
but nope, after several quits and restarts, the file's last-modified date was still May 30.

TL;DR

I finally found a solution after reading the source: bookmark-jump can take an after-jump-hook. So I defined a hook function that goes back to the beginning of the file:

(defun bof ()
  (goto-char (point-min)))
(add-hook 'bookmark-after-jump-hook 'bof)

All set: no more random positions. It's a shame that t option doesn't work; that sounded ideal. But at least I won't be stuck on random positions any more.

Update: The Real Solution

The day after I wrote this, I had lively discussions on Mastodon with several emacs folk. Some of it was annoying "how can you criticize this, just use some other package" type stuff, but I had a very constructive conversation with hmelman, who eventually pointed me to a reddit thread that hinted at something I'd already been trying: writing a function that would open a bookmarked file without actually running bookmark-jump. (It was also nice to see that I wasn't the only person annoyed by this.) It took some fiddling (mostly because I'm not as fluent in elisp as I'd like) but eventually I got it working:

;; bookmark.el has to be loaded first in order to access functions
;; like bookmark-location
(load "bookmark")
(defun jump-to-bookmarked-file (bookmark-name)
  "Attempt to visit a bookmarked file in a way that doesn't force
   the bogus saved bookmark location in the file"
  (interactive (list (bookmark-completing-read "Bookmark name")))
  (find-file (bookmark-location bookmark-name)))
(global-set-key "\C-xrb" 'jump-to-bookmarked-file)

I also enabled save-place-mode, which several people mentioned in the discussion: this remembers your last position in all files, whether bookmarked or not, and I'm liking it quite a bit, especially now that I have it working on bookmarked files too (if you use bookmark-jump, the bogus bookmark position overrides the save-place-mode position).

(use-package saveplace
  :hook (after-init . save-place-mode))

Now things seem to be working nicely, both with bookmarked files and others.

Tags: ,
[ 13:09 Jun 28, 2026    More linux/editors | permalink to this entry | ]

Comments via Disqus:

blog comments powered by Disqus