Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a key in C/C++ mode only?

Tags:

emacs

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.

like image 413
eof Avatar asked Mar 15 '23 00:03

eof


2 Answers

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

(define-key c-mode-base-map (kbd "<tab>") 'clang-format-region)
like image 194
djangoliv Avatar answered Mar 25 '23 00:03

djangoliv


You may use local-set-key

(add-hook
     'c++-mode-hook
      (lambda ()
      (local-set-key (kbd "<tab>") #'clang-format-region)))
like image 28
gongzhitaao Avatar answered Mar 24 '23 23:03

gongzhitaao