In Emacs, with the idea of binding a keychord to another keychord, why is this elisp code not working?
(global-set-key (kbd "C-f") (kbd "C-s"))
(The idea being, in this example, to get "C-f" to do the same as "C-s".)
On
I don't know why it not work either, but the usual way to achieve your purpose is just binding a key to a interactive command, instead of a string or keyboard macro, in this case (kbd "C-s"):
(global-set-key (kbd "C-f") #'isearch-forward)
or (the above is better)
(global-set-key (kbd "C-f") (key-binding (kbd "C-s")))
According to the documentation, I was expecting the following to be true: What you are attempting doesn't work, because you're binding the result of
(kbd "C-s")to a key, which is an internal Emacs key representation and not the function that is (globally) set to the key. However, as xuchunyang pointed out, that's not entirely correct.global-set-keycallsdefine-keyinternally which has in its documentation the following part:And indeed, xunchunyang's example
(global-set-key [?l] (kbd "C-f")works as you expect.(global-set-key [?l] (kbd "C-s")doesn't work, becauseisearch-forwardexpects an argument (the search regexp) whichinteractivewill ask for. Only if you provide one, this particular keyboard macro will work, i.e.(global-set-key [?l] (concat (kbd "C-s") "foo"))will bind a search for "foo" to the key "l".xuchunyang's answer is absolutely right: the best way is to name the function explicitly via
(global-set-key (kbd "C-f") #'isearch-forward). You can easily figure out the function that is bound to a key by typingC-h kand then the key which you want to "copy": e.g.C-h k C-s) will show you thatC-sis (usually, cf. below) bound toisearch-forward.The approach using
key-bindingcould, in the general case, lead to strange results: depending on where and how you execute this (i.e. interactively in some buffer or in your .emacs), results may differ, becausekey-bindinghonours current keymaps, which might assign different functions to keys.