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
You can use define-key
on c-mode-base-map (C, C++ ...)
(define-key c-mode-base-map (kbd "<tab>") 'clang-format-region)
You may use local-set-key
(add-hook
'c++-mode-hook
(lambda ()
(local-set-key (kbd "<tab>") #'clang-format-region)))
-
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
You can use define-key
:
(define-key c++-mode-map (kbd "<tab>") 'clang-format-region)