Globally override key binding in Emacs - emacs

How can I set a key binding that globally overrides and takes precedence over all other bindings for that key? I want to override all major/minor mode maps and make sure my binding is always in effect.
This of course doesn't work:
(global-set-key "\C-i" 'some-function)
It works in text-mode, but when I use lisp-mode, C-i is rebound to lisp-indent-line.
I can go through and override this binding in lisp-mode and in every other mode individually, but there must be an easier way. Every time I install a new mode for a new file type, I'd have to go back and check to make sure that all of my key bindings aren't being overridden by the new mode.
I want to do this because I want to emulate bindings I've already learned and ingrained from other editors.

I use a minor mode for all my "override" key bindings:
(defvar my-keys-minor-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-i") 'some-function)
map)
"my-keys-minor-mode keymap.")
(define-minor-mode my-keys-minor-mode
"A minor mode so that my key settings override annoying major modes."
:init-value t
:lighter " my-keys")
(my-keys-minor-mode 1)
This has the added benefit of being able to turn off all my modifications in one fell swoop (just disable the minor mode) in case someone else is driving the keyboard or if I need to see what a default key binding does.
Note that you may need to turn this off in the minibuffer:
(defun my-minibuffer-setup-hook ()
(my-keys-minor-mode 0))
(add-hook 'minibuffer-setup-hook 'my-minibuffer-setup-hook)

As an addition to scottfrazer's answer, I've written the following so that my keybindings retain precedence, even if subsequently-loaded libraries bring in new keymaps of their own.
Because keymaps can be generated at compile time, load seemed like the best place to do this.
(add-hook 'after-load-functions 'my-keys-have-priority)
(defun my-keys-have-priority (_file)
"Try to ensure that my keybindings retain priority over other minor modes.
Called via the `after-load-functions' special hook."
(unless (eq (caar minor-mode-map-alist) 'my-keys-minor-mode)
(let ((mykeys (assq 'my-keys-minor-mode minor-mode-map-alist)))
(assq-delete-all 'my-keys-minor-mode minor-mode-map-alist)
(add-to-list 'minor-mode-map-alist mykeys))))

Install use-package, eval and you're done:
(require 'bind-key)
(bind-key* "C-i" 'some-function)

I found this question while searching for "emacs undefine org mode keybindings", because I wanted to unbind the existing C-c C-b behavior to allow my global map to bury-buffer to work in an org buffer.
This ended up being the simplest solution for me:
(add-hook 'org-mode-hook
(lambda ()
(local-unset-key (kbd "C-c C-b"))))

Although scottfrazer's answer is exactly what you asked for, I will mention for posterity another solution.
From The Emacs Manual:
"Don't define C-c letter as a key in Lisp programs. Sequences consisting of C-c and a letter (either upper or lower case) are reserved for users; they are the only sequences reserved for users, so do not block them."
If you bind your personal global bindings to C-c plus a letter, then you "should" be safe. However, this is merely a convention, and any mode is still able to override your bindings.

If you want to "always use the keybinds in the map, unless I explicitly override them for a specific mode-map", and assuming you are using scottfrazier's approach, you want:
(defun locally-override (key cmd)
(unless (local-variable-p 'my-keys-minor-mode-map)
(set (make-variable-buffer-local 'my-keys-minor-mode-map)
(make-sparse-keymap))
(set-keymap-parent my-keys-minor-mode-map
(default-value 'my-keys-minor-mode-map)))
(define-key my-keys-minor-mode-map key cmd))
So
(locally-override "\C-i" nil)
should remove the "\C-i" binding from the minor mode in the current buffer only. Warning: this is completely untested, but seems like the right approach. The point of setting the parent rather than just coping the global value of my-keys-minor-mode-map is so any later changes to the global value are automatically reflected in the local value.

I don't think you can. That is roughly equivalent to saying that you want to define a global variable that cannot be hidden by local variable declarations in functions. Scope just doesn't work that way.
However, there might be a way to write an elisp function to go through the mode list and reassign it in every single one for you.

Unless you really want to do this yourself, you should check around and see if anyone else already has done it.
There is a package for Emacs which gives your windows-like keybindings. You should be able to find it through google.

Related

Define key in prefix keymap for a particular mode

I have all my keybindings in a separate minor mode - firstly, to be sure that keybindings that I consider mandatory wouldn't be overridden by some major mode; and secondly, to be able to disable all of them in one command in case I need too.
Up until now I have been extending prefix keys like so:
(define-key my-minor-mode-map (kbd "M-g f") 'my-goto-file-func)
But then I realized that there is such thing as prefix keymap, and for M-g key it is goto-map. So I'd rather extend this keymap instead of hardcoding the prefix key, so it would be like:
(define-key goto-map (kbd "f") 'my-goto-file-func)
But in this case my minor mode has nothing to do with it, so I lose all my advantages of using it.
One way would be to determine which key is bound to that particular prefix keymap and then use that key in the bindings. But it has a drawback - prefix key wouldn't be remapped for my minor mode if I remap it after loading my minor mode.
So are there any better ways to do that?
Extending keys the way you did is perfectly fine because it keeps complexity to a minimum. So while it is certainly possible to modify a keymap in your minor mode, it is not necessarily better.
In order to use a modified keymap in your minor mode without overriding the original one, simply set the original as your parent, e.g.
(defvar my-goto-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map goto-map)
map))
You can then use a modified version in your minor mode:
(define-key my-goto-map "f" 'find-file)
(define-key my-mode-map (kbd "M-g") my-goto-map)
But the original goto-map is still alive and kicking, and modifying it will reflect in your my-goto-map:
(define-key goto-map "i" 'info)
See "Inheritance and Keymaps" in the elisp manual.
Note that olab b's answer is totally fine, and I gonna accept his answer and use it by myself, but there is one issue with it:
The original goto-map is untouched. Imagine hypothetical situation where some library provides a command which uses goto-map in some way - for example, to provide some fancy interface for commands in it or binds to it inside its own keymap - that is something that helm could do if they were crazy enough. Since all my keys would go to the my-goto-map, they wouldn't be reflected in those extensions.
So here is a weird solution for a made-up problem, just for funzies:
First, note that keymap in elisp can be one of two things:
variable containing list whos first element is symbol 'keymap (and it is usually how keymaps are used in emacs)
symbol whos function cell is set to the keymap variable (and I never saw that anybody used it like that)
So first we are going to define a variable for our keymap:
(defvar my-goto-map (make-sparse-keymap))
Next, write its value to the function cell as well:
(fset 'my-goto-map my-goto-map)
Note that we can't replace goto-map since everything that was already bound to it wouldn't catch the replacement. And manipulating with keybindinds one by one isn't fun. So here is a proper insert:
(setcdr goto-map (cons (make-composed-keymap 'my-goto-map (cons (car goto-map) (cdr goto-map))) nil))
(Of course we could just append my-goto-map to goto-map, but in that case the relation between my-goto-map and original goto-map would be "neighbors" and not "parent-child"; and "neighboring" does not allow for undefining keybindings)
Note that goto-map now depends on the symbol my-goto-map so we can do whatever we want with that symbol without loosing our keymap. So we can easily disable all custom keybindings like so:
(fset 'my-goto-map nil)
And the variable cell of my-goto-map is untouched, so we can easily restore keybindings:
(fset 'my-goto-map my-goto-map)
The main drawback comes from the fact that emacs does not allow "buffer-local functions", only "buffer-local variables". And here we need a local function :( So we can't just make that activation of prefix map in the hook of the minor mode - cause that modification would be reflected globally.
But at least all the original prefix keys for that keymap (which is one and only M-g IIRC) would automatically use all the custom keybindings, so no need to hardcode M-g as a prefix key in the my-goto-map.

File-specific key-binding in emacs

Is it possible to define file specific key-bindings in emacs?
I suppose it would be possible to create a minor mode and have it loaded when the particular file is open but for only one key-binding that seems overkill.
If you combine the code to local-set-key and Buffer-locally overriding minor-mode key bindings in Emacs then you could end up with something like this:
(defun my-buffer-local-set-key (key command)
(interactive "KSet key buffer-locally: \nCSet key %s buffer-locally to command: ")
(let ((oldmap (current-local-map))
(newmap (make-sparse-keymap)))
(when oldmap
(set-keymap-parent newmap oldmap))
(define-key newmap key command)
(use-local-map newmap)))
and then, as per Barmar's answer:
;; Local Variables:
;; eval: (my-buffer-local-set-key (kbd "C-c C-c") 'foo)
;; End:
Note that minor mode maps take precedence over the local map.
Use eval: in the File Local Variables section:
;;; Local Variables:
;;; eval: (local-set-key ...)
It smells like you are doing things wrong --- that's my guess. If you have a particular file buffer for which a given key binding is appropriate, then define a mode especially for it and bind the key in that mode's keymap. Let the mode inherit from any other mode you like.
For example:
(define-derived-mode my-file-mode org-mode "My file mode")
(define-key 'my-file-mode-map (kbd "SPC") #'org-toggle-checkbox)
You don't really describe anything about your context: how do you access this file (C-x C-f something else)?, why only this file -- what is special about it? what is the key used for? So it is hard to give you any helpful advice.
If you really want to have some key act differently for this particular file, then maybe define a command that visits the file (however you want to visit it) and then creates an overlay over all of its text, and uses the overlay property keymap to add your binding everywhere. This of course sounds pretty silly, but as it stands now, so does your question.
Emacs works with buffers. Buffers are in modes. A file has little meaning in this context. Once the file is visited, its buffer is what you want to work with.
That's why #Barmar tried to answer in terms of a buffer and its mode. You apparently don't want this to be for a given mode, unless, I guess, the mode is specific to that one file. In that case, define a mode that applies (only) to that file.
Clarify your question and perhaps we will be able to help you more.
(Sounds like this might be an XY problem.)
Setting up a minor mode and loading it automatically when opening the specific file is actually simpler than I thought.
The mode file is something along these lines:
(define-minor-mode magic-mode
"Provide functions to do magic."
:lighter " !!!"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "M-z") 'xyzzy)
map)
)
(defun xyzzy()
"Use at your own risk"
(message "Nothing happens.")
)
(provide 'magic-mode)
It has to be put somewhere the .emacs will look into and the following line is to be added to the .emacs:
(require magic-mode)
Finally, the followingblock should be added at the end of the file that should use the specific commands:
;; Local Variables:
;; eval: (magic-mode)
;; End:

Binding similar commands from different modes to the same key

I use emacs in multiple modes (ESS, Auctex, Slime, elisp, etc...) all using evil-mode key-bindings. Each of the interaction modes have similar functions for evaluating regions, lines or buffers that I have bound to shortcuts using spacebar as a prefix.
;; bind slime's eval and elisp eval to the key sequence "<SPC>e"
(evil-define-key 'normal lisp-mode-map (kbd "<SPC>e") 'slime-eval-last-expression)
(evil-define-key 'normal lisp-interaction-mode-map (kbd "<SPC>e") 'eval-last-sexp)
I would like to set a default key for a "type" of function, so that I don't need to have an entry like the above for every interaction mode I use and for every command. This would hopefully give a more readable .emacs init file and make it easier to change my key-bindings in the future.
I'm fairly sure that I could do this myself using a series of hooks, but I wonder if there is any existing or built-in support for this?
Thanks
tensorproduct
I don't know anything about Evil, so I'll give the normal Emacs solution:
(global-set-key [?\s ?e] #'my-eval-last-sexp)
(defvar my-eval-last-sexp-command #'undefined)
(defun my-eval-last-sexp ()
(interactive)
(call-interactively my-eval-last-sexp-command))
(add-hook 'emacs-lisp-mode-hook
(lambda () (set (make-local-variable 'my-eval-last-sexp-command) #'eval-last-sexp))
(add-hook 'lisp-mode-hook
(lambda () (set (make-local-variable 'my-eval-last-sexp-command) #'slime-eval-last-expression))
...
As you can see, there's only one mention of the key you want (in this case [?\s ?e]). But you don't save much on the amount of code you have to write. You might improve it by making my-eval-last-sexp a bit more complex (e.g. it could try to guess the command name from the major mode name), or by replacing the hook function with a global alist.
Hopefully, in some future Emacs, all such source-code modes that interact with some interpreter/compiler will share more of their code so that your problem will simply disappear.

Set custom keybinding for specific Emacs mode

Though I know how to set a global key-binding in Emacs, I find it hard to even Google out the code for a local (minor-mode specific) key-binding. For instance, I have this code in my .emacs:
;; PDFLaTeX from AucTeX
(global-set-key (kbd "C-c M-p")
(lambda ()
(interactive)
(shell-command (concat "pdflatex " buffer-file-name))))
I don't want to set it globally. Is there a function like local-set-key?
I use the following:
(add-hook 'LaTeX-mode-hook
(lambda () (local-set-key (kbd "C-0") #'run-latexmk)))
to have a bind defined for LaTeX mode alone.
To bind a key in a mode, you need to wait for the mode to be loaded before defining the key. One could require the mode, or use eval-after-load
(eval-after-load 'latex
'(define-key LaTeX-mode-map [(tab)] 'outline-cycle))
Don't forget either '—eval-after-load is not a macro, so it needs them.
You need to identify the key map for that mode (for example, LaTeX-mode-map) and use the function define-key. As an example, along with activating outline-minor-mode within LaTeX mode, I have:
(define-key LaTeX-mode-map [(tab)] 'outline-cycle))
In this case the major mode (LaTeX) holds the key binding, but there is also an outline-minor-mode-map.
None of the other answers satisfied my needs. So this may help other people. I wanted Tab to jump to the beginning of the line if I'm in Evil's normal mode (basically this means everywhere in Emacs), but I instead wanted it to cycle between org item states if I am in an org-mode document.
One option was to mess around with separate bindings and constant binding-rebinding whenever I switched buffers (because evil allows only one binding per key in its normal state).
But a more efficient option was to make Tab run my own code which runs the required function based on which major mode the current buffer uses. So if I am in a org buffer, this code runs org-cycle, and otherwise it runs evil-first-non-blank (go to the first non-whitespace character on the line).
The technique I used here can also be used by calling your custom function via global-set-key instead, for people who use regular non-evil Emacs.
For those who don't know Emacs lisp, the first line after the "if" statement is the true-action, and the line after that is the false-action. So if major-mode equals org-mode, we run org-cycle, otherwise we run evil-first-non-blank in all other modes:
(defun my/tab-jump-or-org-cycle ()
"jumps to beginning of line in all modes except org mode, where it cycles"
(interactive)
(if (equal major-mode 'org-mode)
(org-cycle)
(evil-first-non-blank))
)
(define-key evil-normal-state-map (kbd "<tab>") 'my/tab-jump-or-org-cycle)

How to minify .emacs configuration file?

I was wondering if anyone can provide me with some help on minifying my .emacs file.
Currently I have it set up where each language I use have a custom tab, for example, if I have a hook for Java, it would look like this.
;; Java Hook
(defun e-java-mode-hook ()
(setq tab-width 4)
(setq indent-tabs-mode t)
(define-key java-mode-map (kbd "") 'java-insert-tab))
(defun java-insert-tab (&optional arg)
(interactive "P")
(insert-tab arg))
(add-hook 'java-mode-hook 'e-java-mode-hook)
And if I were to add another language like CSS or JavaScript, I would add another hook for CSS and another hook for JavaScript. So I was wondering if there's a global way of setting it so it would apply to any and all language?
I am currently running GNU Emacs 23.2.1 on Windows 7.
I agree with Tyler; although it's a bit complicated, you would be better off in the long run if you try to understand and customize the default indentation features. The Emacs Wiki has good resources, and there are other relevant Q&As here on Stack Overflow.
Binding the tab key to insert-tab means you completely lose the benefit of the likes of indent-region, and any other intelligent behaviour that a major mode might offer.
To address your specific questions regardless, however:
1) If you are defining (java-insert-tab) and (css-insert-tab) and (javascript-insert-tab) etc, and they all do exactly the same thing... well, hopefully you can see that you don't actually need more than one of those functions. Just give it a more generic name, and re-use it.
2) (local-set-key ...) does the same thing as (define-key (current-local-map) ...), which means you can also have a single generic function to override the tab keybinding, regardless of the major mode.
(defun my-coding-config ()
(setq tab-width 4)
(setq indent-tabs-mode t)
(local-set-key (kbd "<tab>") 'my-insert-tab))
(defun my-insert-tab (&optional arg)
(interactive "P")
(insert-tab arg))
Then you just need to add my-coding-config to each applicable mode hook variable. If there are a lot of them, you might wrap it up in a list like this:
;; Use my coding hook for all programming modes
(mapcar
(lambda (language-mode-hook)
(add-hook language-mode-hook 'my-coding-config))
'(java-mode-hook
javascript-mode-hook
css-mode-hook
...))
3) If you look at C-h v tab-width RET and likewise for indent-tabs-mode, you'll notice that they both say "Automatically becomes buffer-local when set in any fashion."
As an alternative to the customize interface already mentioned, you can use (set-default 'indent-tabs-mode t) to establish the default value for such variables. In the absence of code which sets a buffer-local value, all of your buffers will use the default, which might help you to avoid writing unnecessary mode hooks.
I'm not sure what you're trying to do. If you want to set the tab-width to 4 spaces globally, then you can do that using the customize command:
M-x customize-variable tab-width <ret>
Any changes you make to tab-width in customize will be applied globally. So you won't need to set it individually for each mode with hooks.
If you have different settings you want to apply to different modes, you will necessarily have to have code specific for each mode in your .emacs.
More generally, it looks like you're trying to build your own custom tab insertion commands - does the built-in indentation not do what you need? I think it will be easier to customize the indentation settings in Emacs than to manually insert tabs where you want them.
If you haven't already, take a look at the manual section on indentation and see if you might be able to do what you need without a lot of extra hooks:
C-h r m Indentation
(that is: h-elp, r-ead the manual, m-enu item Indentation)
or:
(info "(emacs)Indentation")
espect.el is doing exactly what you want.
From the docs:
This mode makes it easy to configure settings for individual
buffers with a concice and extensible mini-language. It abstracts
away common configuration selection tasks, like checking the mode
or filename, into a simple declarative syntax. Declare conditions;
run a function when the new buffer matches them. This makes it
easy to do things like turn on flyspell-prog-mode for your favorite
programming languages, or make all text-mode buffers ending in .mkn
have special properties.