Overriding Emacs' Broken Bookmark Position Code
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.
[ 13:33 Jun 27, 2026 More linux/editors | permalink to this entry | ]