Search code examples
emacskey-bindings

Emacs key bindings with multiple keystrokes


I would like to open my five (or so) favorite files with two keystrokes in emacs. I am thinking of F9-a for file a, F9-b for file b, etc.

I have the org-mode agenda at F5 (instead of the "default" C-a), so I hit F5-a for today's agenda. I'd love to have the same for my org-file, my contacts file and so on.

Is there a way (or rather: What is the way) to make F9 open a sub-menu with a list of my favorite files to choose with another keystroke?


Solution

  • This setup does what you are asking for, at least in terms of keymaps. It doesn't actually pop up a menu.

    (setq my-file-menu-keymap
          (let ((keymap (make-sparse-keymap)))
            (dolist (pair '(("a" . "~/.emacs")
                            ("b" . "~/.phones")
                            ("c" . "~/org/daily.org")))
              (define-key keymap (car pair)
                `(lambda () (interactive) (find-file ,(cdr pair)))))
            keymap))
    
    (global-set-key (kbd "<f9>") my-file-menu-keymap)
    

    I use a single key to cycle through my favorite files (works well for a small number of files and I don't care about the random access). You can find code for that in this answer to a different SO question (see cycle-special-files).