2

I'm running into an odd case for rebinding keys, and I'm not sure of the solution. Basically, I want to replace a bunch of keybindings and move the original definition for each to a different key. Here's what I have so far:

(mapc (lambda (map)
          (define-key map "K" (lookup-key map "k"))
          (define-key map "k" #'<my new command for "k">)
        (list <a bunch of mode maps>))

The bad part is lookup-key: it will work fine on the first evaluation, but if I re-evaluate it will rebind "K" to my new binding for "k". So, my question is, is there a way to look up the original binding for a keymap? (Basically, I think I'm looking for the equivalent of noremap in vim.)

  • 2
    Why not just check if "k" is already bound to new command in map and do nothing in this case ? – politza Jul 8 '15 at 20:03
  • @politza that seems like the simplest solution, if you make that an answer I'll accept it. – shosti Jul 9 '15 at 15:49
2

I believe you need to maintain your own data structure of modified keys and their original key bindings. So at the time you define the new binding, you also store the current binding somewhere.

That is how bind-key (part of use-package) implements its describe-personal-keybindings command, which shows you all the bindings you have created along with their original bindings.

3

There is no such thing as "the original binding". But you can save the binding at any point, including just after starting an Emacs session. That might be "original" enough for you; dunno.

And yes, as @glucas points out, functions such as define-key act like assignments (e.g., like setq). If you want to later revert to some previous value then you will need to have saved that value and then reassign to it later.

Note too that if you know the command currently bound to a key (finding it using lookup-key or in some other way), you can use (define-key [remap ORIG-COMMAND] 'NEW-COMMAND) to remap all keys bound to that ORIG-COMMAND to your NEW-COMMAND.

You can later restore all of those keys to their original bindings to ORIG-COMMAND by simply using (define-key [remap ORIG-COMMAND] nil), that is, by making that remapping undefined (binding a key to nil makes it undefined).

  • Cool - I didn't realize you could remove a [remap..] binding that way. – glucas Jul 9 '15 at 4:12
  • Interestingly, setq and friends do keep track of the original value as part of the customize machinery. I was wondering if there was some similar thing for keybindings, but I guess not. – shosti Jul 9 '15 at 15:51
  • @shosti: Nonsense. It is Customize, not setq that does that. And Customize records initial values etc. only for options, not variables in general. And if you use setq more than once, even on an option, there will be no history of previous values (other than the last saved and initial default values). – Drew Jul 9 '15 at 16:16
  • @Drew right, I meant when you setq a customizable option. The point stands. – shosti Jul 9 '15 at 22:04
  • @shosti: It doesn't stand, even in that case, if your point is that Customize keeps track of past assignments. It does not. It keeps track of only the initial/default and the last saved. It does not keep a history of settings. – Drew Jul 9 '15 at 23:01

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.