4

I would like to bind my TAB key in emacs to clang-format-region when in C/C++ mode. How do I achieve this without affecting other modes? In particular, global-set-key doesn't achieve what I want, since it makes editing text with emacs a tad difficult.

3 Answers 3

8

You can use define-key on c-mode-base-map (C, C++ ...)

(define-key c-mode-base-map (kbd "<tab>") 'clang-format-region)
2

You may use local-set-key

(add-hook
     'c++-mode-hook
      (lambda ()
      (local-set-key (kbd "<tab>") #'clang-format-region)))
1
  • Up-voting this because this is the only solution that worked for me. The local-set-key allows for overriding any global key maps inside the c++-mode. You are adding the key binding to the mode map. elisp-docs Jan 29 at 19:00
0

You can use define-key:

(define-key c++-mode-map (kbd "<tab>") 'clang-format-region)

Your Answer

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

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