0

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.

toogley
  • 303
  • 2
  • 15
  • 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 http://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 02 '16 at 08: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 02 '16 at 08:58
  • FYI, remapping F8 is forbidden as the keys between F5 and F9 are reserved for users. – wasamasa Jul 02 '16 at 09: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 02 '16 at 09:06
  • @VanLaser isn't your first question the consequence of your second question? But to answer your question, I want to define `F8` (or any other key or key sequence of course) to execute a certain function, exactly like I've defined it. (And for me, this implies that nobody else/no package can prevent me from doing that - which means, my keybinding should overwrite all keybindings defined by other packages) – toogley Jul 02 '16 at 09:36
  • @wasamasa Ah, I think this explains another problem I have - thanks :D (But as I said in another comment, my question applies to all key sequences, like `C-c m` or any other.) – toogley Jul 02 '16 at 09:37
  • 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 02 '16 at 12:01
  • @VanLaser ah, right. Basically, i was searching for a way to prevent an unintended redefinition of `rhs`. I didn't thought about what I want with `lhs` to do first, but now to not restrict modification/redefinition/whatever of `lhs` at all. Good point, i'll explain that in my question. – toogley Jul 02 '16 at 12:22
  • @wasamasa: (1) `` is not "forbidden". But it is, by convention, reserved for users (as you say). (2) The OP might be a user, and the use here might be for a user ("*to define my keybindings*"). It is good to remind library authors that `` to `` are reserved for users, but I wouldn't put this so categorically. Just one opinion. – Drew Jul 02 '16 at 14:30

1 Answers1

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!

toogley
  • 303
  • 2
  • 15
  • Huh? `[C-x b]` doesn't work for me: it binds to ` ` (i.e. non-existent function keys labeled `C-x` and `b`). Pretty sure `(kbd "C-x b")` is correct. – npostavs Jul 02 '16 at 13:16
  • What you probably mean (want) is `[C-x ?b]`. That's the character `b` and not the pseudo-function key ``. – Drew Jul 02 '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 02 '16 at 15:45