3

Trying to solve the problem of hotkeys for bookmarks I decided to create a primitive version first. I wanted to attach bookmarks to keys <1> and <2>: when a certain key is pressed, I open the appropriate bookmark. But unfortunately the hook is not called when bookmarks list is opened. I have different functions bound to these keys:

  • 1 - bookmark-bmenu-1-window
  • 2 - bookmark-bmenu-2-window

Code

(defun jump-to-1th-bookmark ()
  (bookmark-jump "2015.txt"))

(defun jump-to-2nd-bookmark ()
  (bookmark-jump "emd"))

(defun my-bookmark-mode ()
      (local-set-key (kbd "1") 'jump-to-1th-bookmark)
      (local-set-key (kbd "2") 'jump-to-2nd-bookmark)
      )

(add-hook 'bookmark-bmenu-mode 'my-bookmark-mode)

How can I bind my functions instead of default?

1 Answer 1

6

Just bind the keys in the keymap used by the mode of buffer *Bookmark List*.

This is what mode keymaps are for. There is no need to use the mode hook.

(with-eval-after-load "bookmark"
  (define-key bookmark-bmenu-mode-map (kbd "1") 'jump-to-1th-bookmark)
  (define-key bookmark-bmenu-mode-map (kbd "2") 'jump-to-2nd-bookmark))
9
  • I upgraded your code to the working version. Please, feel free to upgrade it further: maybe, it would be possible to get rid of the hook.
    – user4035
    Commented Oct 16, 2015 at 6:42
  • 1
    @user4035 There was no hook in Drew's answer, and it works, I just tested it.
    – T. Verron
    Commented Oct 16, 2015 at 8:27
  • 1
    @user4035 What was the error? If it is unknown variable: bookmark-bmenu-mode-map, could you test it with the (require 'bookmark) line?
    – T. Verron
    Commented Oct 16, 2015 at 10:05
  • 1
    @user4035 Keeping the line in your config shouldn't cause any problem. If you don't want to use bookmarks in all of your emacs sessions, you can also wrap the define-key lines in an eval-after-load form, so that it is evaluated the first time you use one of the bookmarks functions. Using the hook, the lines would be evaluated each time you enter the bookmark-bmenu-mode, which could prove annoying (for example if you want to redefine these keys).
    – T. Verron
    Commented Oct 16, 2015 at 11:49
  • 1
    I restored my original answer. The point is that you do not need to use a mode hook to add a binding to a mode map. You do need to have the map defined, of course, which typically means that you need to have the library loaded. I added an explicit eval-after-load to make that clear.
    – Drew
    Commented Oct 16, 2015 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.