Redefining keys in emacs' ENSIME scala mode - scala

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)))

Related

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

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)

ido-completion-map keys not working when ergoemacs is enable

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!

How to permanently enable the hs-minor-mode in emacs

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)

How to override/change mode key bindings in elisp?

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)
))

Emacs newline-and-indent in scala-mode

I have (global-set-key (kbd "RET") 'newline-and-indent) in my .emacs which works fine in all modes but scala-mode(the newest, revision 19295 from svn).
What do I need to change to get it working?
(add-hook 'scala-mode-hook
(lambda () (local-set-key (kbd "RET") 'reindent-then-newline-and-indent)))
The above somewhat fixes the problem. It now indents the line correctly after pressing Enter once, but still doesn't work if there is a blank line above the newline.
In scala-mode, "RET" is bound to scala-newline by default, and this overrides the global key binding set with global-set-key, hence the need for the hook specific to scala-mode. Consider using:
(add-hook 'scala-mode-hook
(lambda () (local-set-key (kbd "RET") 'newline-and-indent)))
instead of:
(add-hook 'scala-mode-hook
(lambda () (local-set-key (kbd "RET") 'reindent-then-newline-and-indent)))
If you don't want Emacs to change your indentation after leaving a line.