I'm using M-<left> and M-<right> to cycle buffers in Emacs 24 (with previous-buffer and next-buffer respectively). I also use C-<tab> to cycle through frames.
In Org mode those keys are bound to specific functions. The problem is, if I position the cursor in a neutral area (over whitespace), the keys register and fail; I'd like them to fall back to the behaviour detailed above.
Sadly I don't speak Lisp so I can't tacle the problem on my own.
How can I define a shortcut to custom behaviour when over whitespace in Org mode?
If the issue only presents itself in org-mode, then my best guess is that there are key definitions in that major mode that are trumping your global key settings. One option would be to remove the org-mode-map definitions that are affecting your global settings, and reassign them to different keys. To remove org-mode-map definitions, here is one possible method:
(eval-after-load "org" '(progn
(define-key org-mode-map (kbd "<M-left>") nil)
(define-key org-mode-map (kbd "<M-right>") nil)
(define-key org-mode-map (kbd "C-<tab>") nil) ))
Related
I'm trying to redefine the keys used to navigate the history when inside several commands accepting regexps and offering C-p / C-n history navigation. I'd like to use other keys, in addition to C-p / C-n. For example when using occur or replace-regexp, C-p and C-n can be used to go to previous and next elements.
I've tried several things but can't make it work. I think I'm missing the "big picture" here.
Which mode-map do I need to modify, when and how? Everything I tried failed.
P.S: Note that I've got my own minor mode with all my keymaps as adviced here.
I'm assuming you just needed minibuffer-local-map. Subsequent definitions using keys previously assigned to that key map will trump the prior definitions. To disable a prior key assignment, then just create a new definition and set the last portion to nil instead of 'function-name.
(define-key minibuffer-local-map (kbd "<f6>") 'help-for-help)
Here is an excerpt from Emacs Trunk .../lisp/bindings.el:
(let ((map minibuffer-local-map))
(define-key map "\en" 'next-history-element)
(define-key map [next] 'next-history-element)
(define-key map [down] 'next-history-element)
(define-key map [XF86Forward] 'next-history-element)
(define-key map "\ep" 'previous-history-element)
(define-key map [prior] 'previous-history-element)
(define-key map [up] 'previous-history-element)
(define-key map [XF86Back] 'previous-history-element)
(define-key map "\es" 'next-matching-history-element)
(define-key map "\er" 'previous-matching-history-element)
;; Override the global binding (which calls indent-relative via
;; indent-for-tab-command). The alignment that indent-relative tries to
;; do doesn't make much sense here since the prompt messes it up.
(define-key map "\t" 'self-insert-command)
(define-key map [C-tab] 'file-cache-minibuffer-complete))
To add to what #lawlist said (which was to bind the key in minibuffer-local-map):
There are multiple minibuffer keymaps, depending on what is being read in the minibuffer, and how. And which of those keymaps you might want to use can depend on which Emacs version you are using.
In addition, there is also the keymap for interaction with buffer *Completions*: completion-list-mode-map.
For completion in the minibuffer, the main keymap is minibuffer-local-completion-map.
Here is a list of the minibuffer keymaps. Some of these might not be available (used) in your Emacs version.
minibuffer-local-map
minibuffer-local-ns-map
minibuffer-local-isearch-map
minibuffer-local-completion-map
minibuffer-local-must-match-map
minibuffer-local-filename-completion-map
minibuffer-local-filename-must-match-map
minibuffer-local-must-match-filename-map
In addition, you can use minibuffer-with-setup-hook (or minibuffer-setup-hook directly) to add key bindings on the fly, for the duration of a single minibuffer reading.
I will add this info, since it can be really helpful when you are manipulating minibuffer keymaps: You can use C-h M-k (command describe-keymap), from library help-fns+.el, to see all of the bindings of a given minibuffer keymap in human-readable form.
I've been using emacs for a little while now and am still trying to get the hang of elisp. In my init.el, I have the following lines:
(define-key evil-insert-state-map (kbd "RET") 'newline-and-indent)
(add-hook 'org-mode-hook (lambda () (define-key evil-insert-state-map (kbd "RET") 'newline)))
The intended effect of these two lines of elisp is to disable automatic indentation in org-mode only, but keep automatic indentation for every other mode. However, while this code does disable automatic indentation for org-mode, it has the unintended effect of disabling it for everything else as well. Does anyone know of a way to achieve the desired effect?
You're looking for evil-define-key:
(evil-define-key 'insert org-mode-map (kbd "RET") 'newline)
This will define return to call newline in insert state only in org-mode. What your hook was doing was redefining the global insert state map every time you opened an org buffer.
I have this little problem, I have some key bindings like so C-. C-x or C-. C-m. After I activate the flyspell-mode, I cannot use these commands. In my .emacs file I have the next 2 lines before
(global-unset-key (kbd "C-."))
(define-key (current-global-map) (kbd "C-.") nil)
(global-set-key (kbd "C-. C-l") 'global-linum-mode)
Then, my C-. C-l works, but it does not when the flyspell-mode is activated. The command bound to C-. is flyspell-auto-correct-word. I tried to deactivate it as follows:
;; first try
(defun flyspell-auto-correct-word-disable() (define-key (current-local-map) (kbd "C-.") nil))
(add-hook 'flyspell-mode-hook 'flyspell-auto-correct-word-disable)
;; second try
(define-key (current-global-map) [remap flyspell-auto-correct-word] nil)
None of the tries work, what can I do? I tried in Emacs 23 and 24 and I have the same issue.
What about:
(eval-after-load "flyspell"
'(define-key flyspell-mode-map (kbd "C-.") nil))
Your first solution is almost correct, but you have to remember that the current local map is set up by the major mode, not minor modes. The best option you have it to directly access flyspell-mode-map and modify it (another option would be to find it in minor-mode-map-alist but I think it would be needlessly complicated).
Also, I prefer putting such mode-specific settings within eval-after-load (which means they will be evaluated once) rather than in a hook (in which case the settings are evaluated multiple times: each time one buffer activates flyspell-mode). But this is a matter of preference and either way is fine.
I have two problems which are somewhat related I believe:
1) In IDO I'd like to change ido-restrict-to-matches to samething else than C-SPC or C-#. Unfortunately I do not know how to tell emacs that I want a different shortcut (say C-0).
2) I'd like to protect my C-; but whenever flyspell-mode is running it overtakes C-;. My definition is in .emacs as:
(global-set-key (kbd "C-;") 'mark-paragraph)
but apparently flyspell overwrites this... (although even then, if I look in the help M-h k C-; it does say mark-paragraph)
Could somebody please tell me how to bind/unbind keys in these conditions? It has to work without modifying ido.el and flyspell.el and re-building, right?
Thanks very much!
Flyspell provides a customization for the C-; binding, so you can either M-x customize RET flyspell-auto-correct-binding RET or put something like this in your ~/.emacs:
(setq flyspell-auto-correct-binding (kbd "C-~")) ; or a binding of your choice
As for ido, your question is slightly confusing, because it implies there are times when you're using ido outside the minibuffer...
The documentation in ido.el contains the following advice:
;; 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)
;; )
Using that knowledge, you can change the key bindings like this in your own "ido-my-keys" function:
(define-key ido-completion-map (kbd "C-SPC") nil)
(define-key ido-completion-map (kbd "C-#") nil)
(define-key ido-completion-map (kbd "C-0") 'ido-restrict-to-matches)
There's an additional ido hook specifically for the minibuffer, too, but it's not clear why you would need that: ido-minibuffer-setup-hook.
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.