4

I'm a bloody beginner with lisp, so please bear with me.

I figure the best way to lean is to dive in. Because I'm setting up my Emacs environment, I'll edit my init.el often, I wanted to add a hotkey to find it for editing quickly, as I'll need it often in the course of the next weeks.

I tried:

(global-set-key [f7] '(find-file "~/.emacs.d/init.el"))

to no avail, the answer when pressing the next time is:

Wrong type argument: commandp, (find-file "~/.emacs.d/init.el")

I also tried to put it into an own func, mimicking a working hotkey (for deft (global-set-key [f8] 'deft)):

(defun sz-init-el ()
  (interactive)
  (find-file "~/.emacs.d/init.el"))
(global-set-key [f7] 'sz-init-el)

That worked. So I tried adding (interactive) to my first trial:

(global-set-key [f7] '((interactive) (find-file "~/.emacs.d/init.el")))

But that would not work (again: Wrong type argument: commandp, ...).

So, is there a way to set a global key binding without defining a function/command first? Or do I have to go via the defun detour?

Thank you for your help and answers!

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
A Sz
  • 984
  • 7
  • 19

1 Answers1

5

You need to use a lambda (aka anonymous function):

(global-set-key [f7] (lambda () (interactive) (find-file user-init-file)))

Also, use of variable user-init-file is preferred over hardcoding the name.

juanleon
  • 9,220
  • 30
  • 41
  • Thanks, makes sense (I know lambdas from python). -- particular thanks for the `user-init-file` pointer! – A Sz Apr 02 '14 at 15:46
  • 4
    Saying "I know lambdas from python" to lispers is like saying "I know wings from penguins" to seagulls. :-) – sds Apr 02 '14 at 16:24
  • @sds: I just meant that I have a basic grasp of anonymous functions, and that while not necessarily immediately clear why it'd be necessary (without knowing elisp's syntax better), I understand the syntax and don't need further clarifications what a lambda is. But I get your point. – A Sz Apr 02 '14 at 19:50
  • FYI, it's almost always best to *not* quote lambda expressions. See http://stackoverflow.com/questions/20948325/why-not-quoting-lambda – phils Apr 03 '14 at 00:55