Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to get emacs (23.3 on Arch Linux) to map Ctrl+F12 to the built-in "compile" function when in C-mode (actually CC-mode, which comes built-in as well). So far I've tried the following:

(defun my-c-mode-common-hook (define-key c-mode-map (kbd "C-<f12>") 'compile))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)


(eval-after-load 'c-mode
  '(define-key c-mode-map (kbd "C-<f12>") 'compile))

But neither works; I get <C-f12> is undefined. Based on what I've read here, here, and here, I can't see why it isn't working. Any thoughts?

share|improve this question
I tried the fix I mentioned in my answer on an archlinux guest and I was unable to get C-F12 to work. However changing to just F12 did work. This may be a distro specific bug. Would love to hear if you get this working. – vschum Aug 7 '11 at 23:50
@vschum: Using Gilles' version of the (eval-after-load) approach seems to work. – Wolf Aug 8 '11 at 13:20
up vote 3 down vote accepted

C mode (and specifically the c-mode-map variable) is provided by a package called cc-mode, not c-mode.

(eval-after-load 'cc-mode
  '(define-key c-mode-map (kbd "C-<f12>") 'compile))

For your other method, as vschum has already answered, you're missing the argument list in your defun. Furthermore, c-mode-common-hook isn't the right place for this: it's executed each time you enter C mode. The right time to add your binding is when C mode loads; you can do that either through the general eval-after-load mechanism as above, or through c-initialization-hook:

(defun my-c-mode-common-hook ()
  (define-key c-mode-map (kbd "C-<f12>") 'compile))
(add-hook 'c-initialization-hook 'my-c-mode-common-hook)
share|improve this answer

You've got a typo. You're missing the argument definition for the defun. In this case, since the function my-c-mode-common-hook doesn't take any arguments, add an empty set of parens after the function name.

(defun my-c-mode-common-hook ()
    (define-key c-mode-map (kbd "C-<f12>") 'compile))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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