I want to bind loading a specific theme to a key-binding.

However, this does not work.

(global-set-key (kbd "C-<f1>") '(load-theme 'zenburn))

I would like Ctrl and the F1 key to apply the zenburn theme. But on executing the form I get

Wrong type argument: commandp, (load-theme (quote zenburn))


What you want is

(global-set-key (kbd "C-<f1>")
                (lambda () (interactive)
                  (load-theme 'zenburn t)))

The problem was that global-set-key expects a "command", meaning a function (defined, say, with defun or lambda) with the additional magic form (interactive) added after the arguments list, which tells Emacs that the function is meant to be invoked by the user. The interactive form takes optional arguments specifying, for example, whether your command should prompt for options in the minibuffer, and so forth.

Your'(load-theme 'zenburn) is not a command, or even a function; it's just arbitrary data (equivalent to the result of running (list 'load-theme (list 'quote 'zenburn). Hence the error.

Note also that, since we trust zenburn, we're also applying t as the third argument to load-theme. Otherwise we'll be prompted to mark the theme as safe.

  • Oh nice, thanks! This works in Emacs' gui mode, but not in the terminal mode (i..e when using emacs -nw) What should I add to the code? – smilingbuddha Nov 6 '18 at 21:40
  • It works in my terminal (Konsole). My guess is that your terminal is intercepting the C-<f1> event, preventing Emacs from seeing it. Hopefully your terminal provides an option to disable this behavior. Otherwise, you'll probably have to pick a different key sequence. – Matt Kramer Nov 6 '18 at 21:45
  • Oh great! Indeed there was a binding, but I looked at konsole and I like it better than my current terminal emulator. :-D will be switching to it! – smilingbuddha Nov 6 '18 at 22:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.