Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs .dir-locals.el - setting key bindings

I'm not sure this is possible, but I'd like to setup some project specific key bindings by using .dir-locals.el

Of course .dir-locals.el has to contain a special list of settings, so I can't do:

(global-set-key [24 down] 'move-text-down)

Is there any way I can inject a lambda to run arbitrary code or some other way to set key bindings in .dir-locals.el?

like image 455
ocodo Avatar asked Nov 02 '13 02:11

ocodo


1 Answers

The eval pseudo-variable enables you to specify arbitrary elisp for evaluation with your local variables.

e.g. https://stackoverflow.com/a/7340962/324105

See EmacsWiki for more details.

Note that this is not a particularly useful mechanism for setting key bindings, as every buffer using the keymap in question will be affected. You would probably be better off using the dir-local config to enable a minor mode with the specific keymap for that project. Alternatively, you might adapt this approach to file-local bindings (but a minor mode would be nicer).

That being said...

A fairly minimal form is ((nil . ((eval . (progn BODY))))) with BODY being the expressions to be evaluated. Of course if BODY is only a single expression, you do not need progn.

The following therefore displays a message when you visit any file (under the directory in question):

((nil . ((eval . (message "hello")))))

The car of each list in the dir-locals form is generally a major mode symbol, or nil (as in the above example) in which case the settings apply in any major mode.

The car can also specify a sub-directory string, in which case the cdr is another dir-locals form with settings applicable to that sub-dir.

like image 152
phils Avatar answered Nov 09 '22 05:11

phils