In Spacemacs (or Emacs), is it possible to bind keys to other keys?

644 Views Asked by At

Here is one thing that has been hard to find out. In Spacemacs (or Emacs), is it possible to have a given key combination set to execute whatever another key combination is supposed to do?

In other words, is it possible, for example, to have the F5 key always execute whatever the combination C-c C-c is supposed to do? Something like:

(global-set-key [remap (kbd "F5")] (kbd "C-c C-c")) 

Let me give a concrete example for clarity. The key combination C-c C-c is often used to send the current buffer's entire code to interpreter/console, be it to send the buffer's code to the Python interpreter in case the code resides in a .pyfile, or to send it to a R console with package ESS in a .R file.

Now, suppose that one wants to have the F5 key set to do that for these two languages and maybe even more. One obvious solution would be to simply implement key bindings that are different for each package. However, it would be much easier if it was possible to simply say: whenever F5 is pressed, it should result in whatever command associated to `C-c C-c being called.

Is it possible to have such a thing? I am particularly interested in doing that for Spacemacs, but of course a more generic answer for Emacs is acceptable.

1

There are 1 best solutions below

2
On

Try

(define-key key-translation-map [f5] (kbd "C-c C-c"))

It should solve your problem.