This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Globally override key binding in Emacs
In my .emacs file, I added a keybinding for control-meta-h
(global-set-key (kbd "C-M-h") 'windmove-left)
It works fine, as long I don't enter c-mode or c++-mode. Both overwrite it by the default behavior, which is c-mark-function. How you I avoid that my binding is overwritten by the c-mode/c++-mode hook?
Can be solved by defining the keybinds in a separate minor mode. For details, see Globally override key binding in Emacs, where scottfrazer provides the following solution:
(defvar my-keys-minor-mode-map (make-keymap) "my-keys-minor-mode keymap.")
(define-key my-keys-minor-mode-map (kbd "C-M-h") 'windmove-left)
(define-minor-mode my-keys-minor-mode
"A minor mode so that my key settings override annoying major modes."
t " my-keys" 'my-keys-minor-mode-map)
(my-keys-minor-mode 1)
Related
This question already has an answer here:
How to get completion for yasnippets using auto-complete
(1 answer)
Closed 7 years ago.
I have configured yasnippet and auto-complete bindings so it won't collide, auto complete uses <tab> and yasnippet uses <backtab>:
(add-to-list 'load-path
"~/.emacs.d/plugins/yasnippet")
(require 'yasnippet)
(yas-global-mode 1)
;; Remove Yasnippet's default tab key binding
(define-key yas-minor-mode-map (kbd "<tab>") nil)
(define-key yas-minor-mode-map (kbd "TAB") nil)
;; Set Yasnippet's key binding to shift+tab
(define-key yas-minor-mode-map (kbd "<backtab>") 'yas-expand)
I created two yasnippets one with the key lorem_dummy and the other with the key lorem_image, one of the examples:
# -*- mode: snippet; require-final-newline: nil -*-
# name: lorem_image
# key: lorem_image
# binding: direct-keybinding
# --
<img src="http://lorempixel.com/${1:width}/${2:height}/${3:$$(yas-choose-value '("abstract" "city" "animals" "food" "people" "transport" "business" "sports" "technics"))}/${4:text}" alt="$3">
is it possible to make Emacs auto complete work if I just write lorem and press to show in a dropdown the two choices ( lorem_dummy and lorem_image )? Because right now it doesn't come up the yasnippets, but the regular autocomplete does work ( variables and the rest of the stuff ).
-- update --
Ok, this is strange, after following the solution in the "duplicated question" link, everything seemed to work perfectly, but then after a few hours it started to behave strange, randomdly, sometimes it works sometimes it doesn't. I made a video showing the problem, it works, then I restart Emacs and stops working again.
video
Add ac-source-yasnippet to your ac-sources, e.g.
(eval-after-load "auto-complete"
'(add-to-list 'ac-sources 'ac-source-yasnippet))
This question already has answers here:
Unset 'Tab' binding for yasnippet?
(4 answers)
Closed 8 years ago.
How to unset yasnippet's tab key default behaviour or even redefine it?
I am having issues getting yasnippet and auto-complete to play well together and, in particular, find the Tab key behaviour to be problematic at times as auto-complete tends to prevent yasnippet from expanding snippets.
Experimented a bit and finally found a solution that works.
;; It is crucial you first activate yasnippet's global mode.
(yas/global-mode 1)
;; This illustrates how to redefine yas-expand to S-TAB.
(define-key yas-minor-mode-map [backtab] 'yas-expand)
;; Strangely, just redefining one of the variations below won't work.
;; All rebinds seem to be needed.
(define-key yas-minor-mode-map [(tab)] nil)
(define-key yas-minor-mode-map (kbd "TAB") nil)
(define-key yas-minor-mode-map (kbd "<tab>") nil)
In Emacs, while the major mode ESS[S] (Emacs speaks statistics) is in effect, RET is automatically bound to newline-and-ident but I would prefer it to be bound to just newline. Following the advice here, I bound RET to newline. This works for editing while in ESS, but it has the undesired effect of also affecting commands in the mini-buffer. I cannot use RET to finish commands in the mini-buffer; instead, it just inserts a new line into the mini-buffer rather than executing the command.
Is there a way to bind the key in a minor mode, but also have it not affect the mini-buffer whatsoever?
Even turning the minor-mode off doesn't seem to work and it would be cumbersome to constantly switch the minor mode on and off even if it did.
This is the relevant part of my .emacs file:
(defvar my-keys-minor-mode-map (make-keymap) "my-keys-minor-mode keymap.")
(define-key my-keys-minor-mode-map (kbd "RET") 'newline)
(define-minor-mode my-keys-minor-mode
"A minor mode so that my key settings override annoying major modes."
t " my-keys" 'my-keys-minor-mode-map)
(my-keys-minor-mode 1)
You have the answer in your question:
(define-key ess-mode-map (kbd "RET") 'newline)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Override Ctrl-TAB in EMACS org.mode
I'm trying to rebind C-y to redo.
I've tried all possible options:
(global-unset-key (kbd "C-y")), outside and inside a org mode hook,
and (define-key org-mode-map (kbd "C-y") nil).
Yet nothing. Outside of OrgMode it acts perfectly fine, but inside? it yanks.
This is actually not to first KeyBinding that doesn't work whenever I'm inside OrgMode,
and it boggles me.
Help, please.
After quick help from the fabulous phils, I stumbled upon two things:
Globally override key binding in Emacs
which I currently use, and
http://www.masteringemacs.org/articles/2011/02/08/mastering-key-bindings-emacs/
which I will surely read ASAP.
Thanks Phils;)
My current setup is this:
(defvar custom-keys-mode-map (make-keymap) "custom-keys-mode keymap.")
(define-minor-mode custom-keys-mode
"A minor mode so that my key settings override annoying major modes."
t " my-keys" 'custom-keys-mode-map)
(custom-keys-mode 1)
(defun my-minibuffer-setup-hook ()
(custom-keys-mode 0))
(add-hook 'minibuffer-setup-hook 'my-minibuffer-setup-hook)
(defadvice load (after give-my-keybindings-priority)
"Try to ensure that my keybindings always have priority."
(if (not (eq (car (car minor-mode-map-alist)) 'custom-keys-mode))
(let ((mykeys (assq 'custom-keys-mode minor-mode-map-alist)))
(assq-delete-all 'custom-keys-mode minor-mode-map-alist)
(add-to-list 'minor-mode-map-alist mykeys))))
(ad-activate 'load)
(define-key custom-keys-mode-map (kbd "<C-key>") 'some-command)
This question already has answers here:
Globally override key binding in Emacs
(8 answers)
Closed 8 years ago.
I've configured my emacs to use M-j as backward-char by
(global-set-key (kbd "M-j") 'backward-char) ; was indent-new-comment-line
in my .emacs file. This works fine in many modes (text/org/lisp).
But in c++-mode & php-mode it is bound to the default c-indent-new-comment-line
How can I bind M-j to use backward-char in these modes too.
And in general for ALL modes.
Thanks,
AnotherEmacsLearner
There are policies about which keys are supposed to be mode-dependent and which not. You can overrule bindings changed by a specific mode, but it is a hassle and has to be done for every mode you will be using. It is smarter to keep your own cross-cutting neato bindings to keys that major modes will not touch out of principle. I particularly like the F1-F12 keys for that, or the Sun Function keys when I can get them. The C-c + letter sequences are also explicitly reserved for user-defined commands and will not be rebound by major modes. (See: Key Binding Conventions)
I unset keys that are in the way for specific modes like this:
(add-hook 'gnus-summary-mode-hook
(function (lambda ()
(local-unset-key '[M-down])
(local-unset-key '[M-up]))))
(add-hook 'org-mode-hook
(function (lambda ()
(local-unset-key '[S-down])
(local-unset-key '[S-left])
(local-unset-key '[S-right])
(local-unset-key '[S-up]))))
This issue was addressed in this question. The way to do this is to create a minor mode with your bindings. Your minor mode bindings will shadow any major mode's bindings.
I like your example, since just within the last couple of weeks, I rebound M-h,j,k,l to their equivalent vim movements, and made a minor mode to do it (It turned out to be a great idea. Emacs's default bindings really are terrible). Here's a sample of some of my code:
(defvar kirkland-minor-mode-map (make-keymap) "kirkland-minor-mode keymap.")
(define-key kirkland-minor-mode-map (kbd "M-h") 'backward-char)
(define-key kirkland-minor-mode-map (kbd "M-l") 'forward-char)
(define-key kirkland-minor-mode-map (kbd "M-j") 'next-line)
(define-key kirkland-minor-mode-map (kbd "M-k") 'previous-line)
(define-minor-mode kirkland-minor-mode
"A minor mode so that my key settings aren't shadowed by other major/minor modes"
t " kirkland" 'kirkland-minor-mode-map)
One other thing I should mention is that while this will override any major mode bindings, it can still be overridden by other minor modes which are loaded later.
Nothing can stop any mode from redefining any key any way it wants and it always shadows the global-set-key. So you have to redefine it for every mode that redefines it:
(defun redefine-cc-mode-keys ()
(define-key c-mode-base-map "M-J" 'backward-char))
(add-hook 'c-initialization-hook 'redefine-cc-mode-keys)
or similar.