0

This question already has an answer here:

vim has a really great feature for defining keybindings.

from the vim docs:

:cno[remap] {lhs} {rhs}     |mapmode-c| *:cno* *:cnoremap*
            Map the key sequence {lhs} to {rhs} for the modes
            where the map command applies.  Disallow mapping of
            {rhs}, to avoid nested and recursive mappings.  Often
            used to redefine a command.

Basically, the :cnoremap command creates a keybinding, but forbids any further redefinitions of it. This made defining keybindings in vim always easy for me. However, in emacs i don't know such a command - currently I use commands like (global-set-key [f8] 'my/copy-to-clipboard) to define my keybindings. Unfortunately, this is really a pain, because other packages can easily remap that (in my case, the evil package).

How can i define a keybinding in emacs, which can't be modified, once it is set?


Important edit: I don't want to restrict what can be done to lhs - I want just prevent a redefinition/modification of rhs.

marked as duplicate by Gilles Sep 30 '16 at 18:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    I'm pretty sure that you can't. All you can do is use a keymap which has a higher priority than most other keymaps. For many use-cases stackoverflow.com/q/683425 is a good solution. In the case of a specific keymap like helm-find-files-map, I expect you can make helm use in its place a custom keymap which inherits from the original keymap. Other code will modify the original by name, while you can define custom bindings in the new map where they will always take precedence. – phils Jul 2 '16 at 8:51
  • @phils Thanks for that link, i'll try it. (Btw. I just added an keybinding which I actually use - the former was just copied from another question of this site.) – toogley Jul 2 '16 at 8:58
  • FYI, remapping F8 is forbidden as the keys between F5 and F9 are reserved for users. – wasamasa Jul 2 '16 at 9:02
  • 1
    The question is confusing (for me) - the vim help you quote states that the rhs part is taken into account "as is" - but anyone can create another remap and override the lhs (i.e. the command you created). So, what do you want in Emacs, to know that F8 is mapped to a certain sequence, sequence that has a certain/fixed meaning, or that nobody else shouldn't be able to remap F8? These are two different things, as I see it. – VanLaser Jul 2 '16 at 9:06
  • 2
    @toogley - with the vim help you quoted - it doesn't stop anybody to remap your lhs to some other command. I'm saying, you want X, but quote Y, and that is confusing. Basically, one can make F8 run another command, yes, but also one can simply re-write the command F8 is calling (so it has the same name, but it does something else) - and that is a 2nd problem, not related to the 1st, and closer to the original vim help quote (!). – VanLaser Jul 2 '16 at 12:01
0

Thanks to the comment of @phils (especially for this stackoverflow answer), i found now a solution:

(defvar my-keys-minor-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map [f8] 'my/copy-to-clipboard)
    (define-key map  [C-x b] 'helm-mini)
    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")

The keybinding style using (define-key map (kbd "C-x b") 'helm-mini) didn't work unfortunately - but (define-key map [C-x b] 'helm-mini) does now. (C-x b was before bound to an evil command)

Thanks!

  • Huh? [C-x b] doesn't work for me: it binds to <C-x> <b> (i.e. non-existent function keys labeled C-x and b). Pretty sure (kbd "C-x b") is correct. – npostavs Jul 2 '16 at 13:16
  • What you probably mean (want) is [C-x ?b]. That's the character b and not the pseudo-function key <b>. – Drew Jul 2 '16 at 14:30
  • Well, i'm a bit confused. both (kbd "C-x b") and [C-x b] .. I can even reproduce the same behavior with [C-x ?b] - therefore i guess i've made somewhere an error in reproducing the error. But unfortunately, i don't have the time to figure out what exactly was my mistake. – toogley Jul 2 '16 at 15:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.