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.
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)))
You can use define-key:
(define-key c++-mode-map (kbd "<tab>") 'clang-format-region)
Related
When using ergo emacs, for some reason M-l and M-j (forward-char and backward-char respectively) don't work properly in the minibuffer (with ido mode).
I've tried setting the ido-completion-map with the following:
(add-hook 'ido-setup-hook
(lambda ()
(define-key ido-completion-map (kbd "M-k") 'ido-next-match)
(define-key ido-completion-map (kbd "M-i") 'ido-prev-match)
(define-key ido-completion-map (kbd "M-l") 'ido-next-match)
(define-key ido-completion-map (kbd "M-j") 'ido-prev-match)))
but these don't seem to stick.
I seem to be having a similar problem to this person: ido-mode binding masked by global-set-key but none of the solutions seems to work for me
Any help would be very appreciated
Kind regards
Nimai
Although the instructions at the outset of ido.el suggest using:
;; Customization
;; -------------
;;
;; Customize the Ido group to change the Ido functionality.
;;
;; To modify the keybindings, use the ido-setup-hook. For example:
;;(add-hook 'ido-setup-hook 'ido-my-keys)
;;
;;(defun ido-my-keys ()
;; "Add my keybindings for ido."
;; (define-key ido-completion-map " " 'ido-next-match)
;; )
I recently found that using the ido-common-completion-map had better luck when using a frame-switch function -- the original poster can substitute his / her own preferred keyboard shortcuts instead of m-tab and/or m-S-tab:
(add-hook 'ido-setup-hook 'ido-my-keys)
(defun ido-my-keys ()
"Add my keybindings for ido."
(define-key ido-common-completion-map (kbd "<M-tab>") 'ido-next-match)
(define-key ido-common-completion-map (kbd "<M-S-tab>") 'ido-prev-match) )
I have met the save problem, i'm using Emacs 24.4 with ergoemacs-mode-5.14.7.3 (i don't use the latest version of ergoemacs because it has the speed issue. See: github issue). After a lot of searching, i finally find this github commit, and get it work by adding below code to my emacs init file:
after enable ergoemacs-mode:
(when ido-mode
(global-set-key [remap ido-magic-forward-char] 'ido-next-match)
(global-set-key [remap ido-magic-backward-char] 'ido-prev-match))
Hope it helps, thanks!
I am using thhs code in the .emacs file to permanently enable the hs-minor-mode and to change the shortcut:
(setq-default hs-minor-mode t)
(global-set-key (kbd "C-c C-h") (kbd "C-c # C-h")) ;;hiding block of code
(global-set-key (kbd "C-c C-r") (kbd "C-c # C-s")) ;;revealing block of code
But the mode is not activated automatically. what should i do?
You can turn on hs-minor-mode for a specific mode like C, C++ mode using c-mode-common-hook.
(add-hook 'c-mode-common-hook #'hs-minor-mode)
In Emacs 24 or later, you can turn it on in all programming modes using prog-mode-hook.
(add-hook 'prog-mode-hook #'hs-minor-mode)
If you want it to be truly global, this does the trick:
(define-globalized-minor-mode global-hs-minor-mode
hs-minor-mode hs-minor-mode)
(global-hs-minor-mode 1)
If you want to enable it everywhere, and start the buffer with the code folded by hs-hide-all, do
(defun my-hide-all()
(interactive)
(hs-minor-mode)
(hs-hide-all))
(add-hook 'prog-mode-hook 'my-hide-all)
I have tried several different ways of doing this, and none have been successful. I want to switch the M-d and C-d functionality (delete word, delete char) respectively while working in c++ files.
Can someone please lend me a hand?
(add-hook 'c-initialization-hook
(lambda ()
(define-key c++-mode-map "\C-d" 'kill-word)
(define-key c++-mode-map "\M-d" 'c-electric-delete-forward)))
From CC Hooks - CC Mode Manulal:
Variable: c-initialization-hook
Hook run only once per Emacs session, when CC Mode is initialized. This is a good place to change key bindings (or add new ones) in any of the CC Mode key maps. See Sample .emacs File.
(eval-after-load "cc-mode"
'(progn
(define-key c++-mode-map (kbd "C-d") 'kill-word)
(define-key c++-mode-map (kbd "M-d") 'delete-char)))
I'm trying to redefine the "M-." in the ENSIME mode so that it runs auto-complete instead of ensime-edit-definition. Which is the default binding. I have the following code in the .emacs:
(defun my-scala-mode()
(ensime-mode)
(local-set-key [return] 'newline-and-indent)
(local-unset-key (kbd "M-."))
(local-set-key (kbd "M-.") 'auto-complete)
(global-unset-key (kbd "M-."))
(global-set-key (kbd "M-.") 'auto-complete)
;(scala-electric-mode)
(yas/minor-mode-on))
(add-hook 'scala-mode-hook 'my-scala-mode)
However, once ensime mode loads, and somehow redefines the keys back to the default. If I comment out "(ensime-mode)" then it maps correctly.
What should I do here? Is there another mode hook I'm missing? Or should the order be different?
Thank you
Apparently ensime-mode is a minor-mode, so its bindings take precedence over the major-mode's bindings. And local-set-key affects the major mode's bindings. You might want to do something like the following (guarantedd 100% untested) instead:
(require 'ensime)
(define-key ensime-mode-map (kbd "M-.") 'auto-complete)
or
(add-hook 'ensime-mode-hook (lambda () (define-key ensime-mode-map (kbd "M-.") nil)))
In particular, when I load dired-x, it sets M-o to toggle the omit minor mode. I use M-o for other-window, so I would like to change the key that dired-x binds to something else. I've attempted setting the key after the mode loads like this:
(add-hook 'dired-mode-hook
(lambda ()
(dired-omit-mode 1)
(global-set-key (kbd "M-o") 'other-window)
))
but to no avail.
Slightly better than adding another copy of your custom global binding to the local mode map, would be removing the local binding so that it no longer shadows the global binding. You might also give that function a new key before you do this.
(eval-after-load "dired-x"
'(progn
;; Add an alternative local binding for the command
;; bound to M-o
(define-key dired-mode-map (kbd "C-c o")
(lookup-key dired-mode-map (kbd "M-o")))
;; Unbind M-o from the local keymap
(define-key dired-mode-map (kbd "M-o") nil)))
The dired-mode bindings "shadow" the global ones so your "global-set-key" isn't helping. What you want to do is override the dired-mode binding:
(add-hook 'dired-mode-hook
(lambda ()
(dired-omit-mode 1)
(define-key dired-mode-map (kbd "M-o") 'other-window)
))