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.
-
`(global-set-key [f5] (lambda () (interactive) (if (equal (buffer-name) "*scratch*") (message "Congratulations -- you are executing F5 in buffer %s" (buffer-name)))))` Alternatively, you can use `buffer-file-name` and then the file path. You can also do this with file extensions -- e.g., `(if . . . (file-name-extension input-filename)` – lawlist Feb 01 '14 at 04:04
4 Answers
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.
Use eval:
in the File Local Variables section:
;;; Local Variables:
;;; eval: (local-set-key ...)

- 741,623
- 53
- 500
- 612
-
1Doesn't that just set the binding for current `major-mode`? So other buffers of this mode will get the binding as well? – abo-abo Jan 31 '14 at 19:29
-
Hmm, you're right, the local key map is usually shared by all buffers in the same major mode. The basic idea is still right: write some code that makes a binding in the current buffer, and put a call to it in the `Local Variables` section. – Barmar Jan 31 '14 at 20:03
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.)

- 29,895
- 7
- 74
- 104
-
Your answer would be more useful if you actually explained the way to do what you mention. That sounds good and I am willing to try it but I am not very familiar with creating modes and altering key-maps. – M. Toya Feb 01 '14 at 07:44
-
2My main suggestion was to state your problem directly, e.g., providing more context, instead of asking how to do what you think the solution should be (see the XY problem link). I suggest that because I sense somehow that you don't need what you think you need. Anyway, wrt using overlays with property `keymap`, see the Elisp manual, node `Overlay Properties`. But my guess is that @phils's answer is a better approach. – Drew Feb 01 '14 at 10:12
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:

- 615
- 8
- 24