How to override/change mode key bindings in elisp? - emacs

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

Related

Locally override keybinding for emacs major mode

What is the proper way to override a keybinding for a major mode so it only affects the buffer-local keymap? I thought I could use local-set-key or make-local-variable, but my attempts there affect the keymaps globally (shown below).
Is it necessary to copy the map, as done below, or create a minor mode with a different keymap? It would be nice to be able to make it a local variable if possible.
For example, say I want to have original bound to C-c C-c in the global emacs-lisp-mode-map but after calling jump-to-other-buffer I want C-c C-c to be bound to local-version only in that buffer.
(defun original ()
(interactive)
(message "original"))
(defun local-version ()
(interactive)
(message "local binding"))
;; open new buffer in emacs-lisp mode and set a local key
(defun jump-to-other-buffer ()
(interactive)
(with-current-buffer (get-buffer-create "*test1*")
(emacs-lisp-mode)
;; These change bindings in all elisp buffers
;; (make-local-variable 'emacs-lisp-mode-map)
;; (define-key emacs-lisp-mode-map (kbd "C-c C-c") 'local-version)
;; (local-set-key (kbd "C-c C-c") 'local-version)
(let ((overriding-local-map (copy-keymap emacs-lisp-mode-map)))
(define-key overriding-local-map (kbd "C-c C-c") 'local-version)
(use-local-map overriding-local-map))
(pop-to-buffer (current-buffer))))
;; default binding in elisp buffers
(define-key emacs-lisp-mode-map (kbd "C-c C-c") 'original)

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!

Redefining keys in emacs' ENSIME scala mode

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

Remapping SLIME keys

Slime remaps several keys I like to use and have set in global-set-key.
I'd like to avoid directly editing slime.el.
What's the routine to override keys in a given mode?
Update:
In your .emacs, set the mode-map directly:
(define-key slime-mode-map "\M-n" 'next-line)
(define-key slime-mode-map "\M-p" 'previous-line)
It's not too easy to redefine a key for a mode since it can provide several keymaps. And there can be many modes that grab my favorite keys.
I solved it this way: I defined a minor mode that doesn't do anything except it has it's own keymap. I put some keys there i want to be really global.
;; my minor mode for really global keybindings
(defvar my-keys-minor-mode-map (make-keymap) "my-keys-minor-mode keymap.")
(define-minor-mode my-keys-minor-mode
"My minor mode for global keybindings."
:init-value t :lighter "" :keymap 'my-keys-minor-mode-map)
(defun my-minibuffer-setup-hook ()
(my-keys-minor-mode 0))
(add-hook 'minibuffer-setup-hook 'my-minibuffer-setup-hook)
(my-keys-minor-mode 1)
;; for example
(define-key my-keys-minor-mode-map (kbd "C-a") 'mark-whole-buffer)
...

Useful keyboard shortcuts and tips for ESS/R

I would like to ask regular ESS/R users what key bindings do they use frequently and tips on using ESS/R.
I have set several shortcuts in my .emacs file. The most useful are:
C-tab to switch between the R command line and the file (similar to josh answer, but much faster):
(global-set-key [C-tab] 'other-window)
Control and up/down arrow keys to search history with matching what you've already typed:
(define-key comint-mode-map [C-up] 'comint-previous-matching-input-from-input)
(define-key comint-mode-map [C-down] 'comint-next-matching-input-from-input)
Comment-uncomment a selected region with C-d or C-maj-d
(defun uncomment-region (beg end)
"Like `comment-region' invoked with a C-u prefix arg."
(interactive "r")
(comment-region beg end -1))
(define-key ess-mode-map (kbd "C-d") 'comment-region)
(define-key ess-mode-map (kbd "C-S-d") 'uncomment-region)
Also I've also enabled CUA mode (from options menu) and reconfigured quite a lot of shortcuts to require only two keystrokes (instead of four in standard mode):
;; Delete selection when pressing [delete] key
(delete-selection-mode t)
;; ESS Mode (.R file)
(define-key ess-mode-map "\C-l" 'ess-eval-line-and-step)
(define-key ess-mode-map "\C-p" 'ess-eval-function-or-paragraph-and-step)
(define-key ess-mode-map "\C-r" 'ess-eval-region)
;; iESS Mode (R console)
(define-key inferior-ess-mode-map "\C-u" 'comint-kill-input)
(define-key inferior-ess-mode-map "\C-w" 'backward-kill-word)
(define-key inferior-ess-mode-map "\C-a" 'comint-bol)
(define-key inferior-ess-mode-map [home] 'comint-bol)
;; Comint Mode (R console as well)
(define-key comint-mode-map "\C-e" 'comint-show-maximum-output)
(define-key comint-mode-map "\C-r" 'comint-show-output)
(define-key comint-mode-map "\C-o" 'comint-kill-output)
;; Search with C-f / C-F (control-maj-F for backware search)
(global-set-key "\C-f" 'isearch-forward)
(global-set-key (kbd "C-S-f") 'isearch-backward)
(define-key isearch-mode-map "\C-f" 'isearch-repeat-forward)
(define-key isearch-mode-map (kbd "C-S-f") 'isearch-repeat-backward)
;; Save with C-s / C-S
(global-set-key (kbd "C-s") 'save-buffer)
(global-set-key (kbd "C-S-s") 'write-file)
;; need to redefine them for isearch mode (don't know why)
(define-key isearch-mode-map (kbd "C-s") 'save-buffer)
(define-key isearch-mode-map (kbd "C-S-s") 'write-file)
;; Pause = dedicate window.
(defun toggle-current-window-dedication ()
(interactive)
(let* ((window (selected-window))
(dedicated (window-dedicated-p window)))
(set-window-dedicated-p window (not dedicated))
(message "Window %sdedicated to %s"
(if dedicated "no longer " "")
(buffer-name))))
(global-set-key [pause] 'toggle-current-window-dedication)
;; delete = delete
(global-set-key [delete] 'delete-char)
;; C-b = list buffers
(global-set-key (kbd "C-b") 'bs-show)
You will find many more useful shortcuts in ESS documentation.
C-c C-z ess-switch-to-end-of-ESS
is nice to jump from your source file that you are editing foo.R to the R console
I found this link to be extremely helpful. It provides elisp code to make Shift+Enter do many common tasks in a context dependent fashion.
http://kieranhealy.org/blog/archives/2009/10/12/make-shift-enter-do-a-lot-in-ess/
Great stuff, have been using it for ages. Unfortunately as of 15-11-2013 the uncomment key binding may not work due to EMACS changes (I think, at least it was working before I loaded the latest version). This is because the default uncomment function has 3 arguments but the one defined above has 2. The best way to fix this is to simply delete the uncomment function from the code and retain the keybinding, so it uses the default uncomment function. Or in other words just use this:
(define-key ess-mode-map (kbd "C-d") 'comment-region)
(define-key ess-mode-map (kbd "C-S-d") 'uncomment-region)
M-n and M-p in the ESS R console for next/previous command.