2

I'm not sure if this is package-specific (example here is olivetti-mode), but when I try to "remap" with statement:

(define-key olivetti-mode-map (kbd "<f7> ]") 'olivetti-shrink) (for some reason you have also wrap this in eval-after-load, but omitted here)

The old keybinding for the function olivetti-shrink (C-c ]) stays in place, so that I now get two keybindings for the same function. I only want the new key-binding. As a general rule, do you have to map to nil whenever you want to change the key binding:

(define-key some-mode-map (kbd "old-key") nil)
(define-key some-mode-map (kbd "new-key") function)

I'm wondering if this is really so.

Answer?: According to this tutorial the answer is YES: you have to do it every time. One workaround is to use use-package which allows you to configure a bunch of options after loading, including keybindings.

  • Drew has mentioned it in his answer, but in Emacs the term "remap" has a particular meaning which is distinctly different to what you are doing here. – phils May 1 '17 at 3:30
  • 2
    Also FYI the "for some reason" regarding eval-after-load is that you can't make changes to a keymap that doesn't exist yet. – phils May 1 '17 at 3:32
  • 1
    I don't understand how use-package is a workaround, you would still have to unbind the old key if you don't want it to do anything. – npostavs May 1 '17 at 3:50
4

Yes. A given command can have multiple bindings in the same map.

You are asking to add a key binding for the command, and you are asking to remove a key binding for the command.

You say "every time", but it is not that common to want to replace a key binding with another. In any case, if you want to do that a lot you can easily define a macro that does it: carries out a replacement by an addition and a deletion.

(On the other hand, it is common to want to replace the command that is bound to a given key by another command. For that Emacs lets you either (1) use [remap COMMAND] in define-key or global-set-key or (2) use substitute-key-definition.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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