notes from /dev/null

by Charles Choi 최민수

Using Bookmarks in Emacs like you do in Web Browsers

13 Sep 2023  Charles Choi

Bookmarks are a useful, daresay essential feature for apps that work with an unbounded number of documents. Web browsers are ubiquitous, and as such have set the common expectation for using and managing bookmarks. Emacs predates web browsers and has long had bookmarks yet I’ve always found them cumbersome to use out of the box. The reasons I think are two-fold: 1) having a default user experience reliant on relatively long and arcane keyboard bindings (C-x r b|m|M) and 2) discovery via mouse being encumbered by being located under the Edit menu on the top-level menu bar. I’d further add that the default bookmark menu items are a bit too extra for my taste. I much prefer a minimal menu which is described further below.

What I want is to have a user experience akin to what a web browser provides, but for Emacs. Described below is what I think this looks like, at least on first pass:

  • Primarily mouse-driven workflows

    Conventional web browsers (Chrome, Firefox, Safari) all have a Bookmarks menu in the top level menu bar. Customize Emacs to do the same.

  • Simplified bookmark menu

    The above mentioned top level Bookmarks menu is simplified so that only the following operations are supported.

    • Edit Bookmarks

      Display the current list of bookmarks which can be subsequently edited.

    • Add Bookmark

      Create a bookmark where the current point is at.

    • Jump to Bookmark

      Go to a specific bookmark.

In addition, to ease the keyboard ergonomics of using bookmarks, I’ve assigned a dedicated function key each for jumping to or adding a bookmark.

As always, the customizations provided below are bespoke to me. Feel free to take and extend or disregard what is offered here.

All code tested on Emacs 28.2.

Define the Bookmark Menu in the Menu Bar

Here I use easy-menu-define to define the menu cc/bookmarks-menu as specified above. To avoid stomping over an existing bookmark, I use bookmark-set-no-overwrite instead of bookmark-set.

(easy-menu-define cc/bookmarks-menu nil
  "Keymap for CC Bookmarks Menu"
    ["Edit Bookmarks" list-bookmarks
     :help "Display a list of existing bookmarks."]
    ["--" nil]
    ["Add Bookmark…" bookmark-set-no-overwrite
     :help "Set a bookmark named NAME at the current location."]
    ["---" nil]
    ["Jump to Bookmark…" bookmark-jump
     :help "Jump to bookmark"]))

Adding the menu cc/bookmarks-menu to the top-level menu-bar can be achieved using the function easy-menu-add-item. Here I move the menu to appear before the Tools menu.

(easy-menu-add-item global-map '(menu-bar)

I’ve never really liked the default bookmarks sub-menu being located under the Edit menu so let’s hide it.

(define-key global-map [menu-bar edit bookmark] nil)

With the above, the payoff is a custom Bookmarks menu shown prominently in the menu bar.

Define One-Key Bindings for Jumping to and Adding a Bookmark

I jump to and add bookmarks frequently enough that dedicating a function key to each operation makes sense to me. YMMV.

(global-set-key (kbd "<f4>") 'bookmark-jump)
(global-set-key (kbd "<f11>") 'bookmark-set-no-overwrite)

Bookmark Save Flag

By default, any editing operation to your bookmarks are not saved by default as I've described before on this blog. Set bookmark-save-flag to 1 to ensure that your bookmark changes are always implicitly saved.

Closing Thoughts

With the above configuration, I've found bookmarks to be far more pleasant to use in Emacs. Motivated readers are encouraged to give it a try!

Some readers might mention Bookmark+ which could be used in place of the built-in bookmark commands in the menu structure described above. If that works for you, great!




Feeds & TagsGet Captee for macOS

Powered by Pelican