Say, the C-M-j is bound to c-indent-new-comment-line for c-mode (and c++-mode) and indent-new-comment-line for other modes, but I want to define a function that will execute C-M-j to c-indent-new-comment-line in c-mode (and c++-mode) and indent-new-comment-line in other modes.

Is there any simple way that I can just embed the shortcut but not check the current major mode and then execute the different functions in defun...?


(defun example-fn ()

The (execute-C-M-j) is what I want, execute the key not the command it is bound to, but I don't know how to implement this?

  • Something like this?: (defun example-fn () (interactive) (if (eq major-mode 'c-mode) (message "Hello-world!") (message "Happy birthday!"))) (define-key global-map [f5] 'example-fn)
    – lawlist
    Commented Jan 18, 2015 at 7:58
  • What does a function which uses C-M-j to do X mean? After executing the function, you would want the desired shortcuts to be bound?
    – Pradhan
    Commented Jan 18, 2015 at 8:08
  • 2
    @lawlist No, you are saying the normal way(defun and then set-key), I mean the key is part of the function defined, you will execute a lot of commands including the key if you execute the function.
    – CodyChan
    Commented Jan 18, 2015 at 8:09

1 Answer 1


You can use funcall and key-binding to do this:

(funcall (key-binding (kbd "C-M-j")))

will execute the function currently bound to C-M-j.

funcall lets you pass in arguments as well, if needed.

Alternatively, you can use call-interactively to call a command bound to a key like this:

(call-interactively (key-binding (kbd "C-M-j")))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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