Question

Is it possible to define file specific key-bindings in emacs? I suppose it would be possible to create a minor mode and have it loaded when the particular file is open but for only one key-binding that seems overkill.

Was it helpful?

Solution

If you combine the code to local-set-key and Buffer-locally overriding minor-mode key bindings in Emacs then you could end up with something like this:

(defun my-buffer-local-set-key (key command)
  (interactive "KSet key buffer-locally: \nCSet key %s buffer-locally to command: ")
  (let ((oldmap (current-local-map))
        (newmap (make-sparse-keymap)))
    (when oldmap
      (set-keymap-parent newmap oldmap))
    (define-key newmap key command)
    (use-local-map newmap)))

and then, as per Barmar's answer:

;; Local Variables:
;; eval: (my-buffer-local-set-key (kbd "C-c C-c") 'foo)
;; End:

Note that minor mode maps take precedence over the local map.

OTHER TIPS

Use eval: in the File Local Variables section:

;;; Local Variables:
;;; eval: (local-set-key ...)

It smells like you are doing things wrong --- that's my guess. If you have a particular file buffer for which a given key binding is appropriate, then define a mode especially for it and bind the key in that mode's keymap. Let the mode inherit from any other mode you like.

For example:

(define-derived-mode my-file-mode org-mode "My file mode")
(define-key 'my-file-mode-map (kbd "SPC") #'org-toggle-checkbox)

You don't really describe anything about your context: how do you access this file (C-x C-f something else)?, why only this file -- what is special about it? what is the key used for? So it is hard to give you any helpful advice.

If you really want to have some key act differently for this particular file, then maybe define a command that visits the file (however you want to visit it) and then creates an overlay over all of its text, and uses the overlay property keymap to add your binding everywhere. This of course sounds pretty silly, but as it stands now, so does your question.

Emacs works with buffers. Buffers are in modes. A file has little meaning in this context. Once the file is visited, its buffer is what you want to work with.

That's why @Barmar tried to answer in terms of a buffer and its mode. You apparently don't want this to be for a given mode, unless, I guess, the mode is specific to that one file. In that case, define a mode that applies (only) to that file.

Clarify your question and perhaps we will be able to help you more.

(Sounds like this might be an XY problem.)

Setting up a minor mode and loading it automatically when opening the specific file is actually simpler than I thought.

The mode file is something along these lines:

(define-minor-mode magic-mode
  "Provide functions to do magic."
  :lighter " !!!"
  :keymap (let ((map (make-sparse-keymap)))
            (define-key map (kbd "M-z") 'xyzzy)
            map)
)


(defun xyzzy()
  "Use at your own risk"
  (message "Nothing happens.")
  )



(provide 'magic-mode)

It has to be put somewhere the .emacs will look into and the following line is to be added to the .emacs:

(require magic-mode)

Finally, the followingblock should be added at the end of the file that should use the specific commands:

;; Local Variables:
;; eval: (magic-mode)
;; End:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top