suppose I want to bind a key to a partial key sequence (i.e. a key sequence that is a prefix of some other bound keybindings).
I've tried the obvious
(global-set-key (kbd "C-x C-x") (kbd "C-c"))
But that doesn't work for some reason.
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityIf you want to no longer have the prefix key be a prefix key, and you want to instead bind it to a single command, you can do that - no problem: just bind it.
If you want to have the prefix key act as both a single command and as a prefix key then that is of course impossible - a key is bound to either a command or a keymap (or to a command that is a keymap).
But if you want to bind a different prefix key to the same keymap that is bound to some other prefix key, that is not a problem. What you tried to do suggests that this is maybe what you really want: to make the key sequence C-x C-x
act as a prefix key the same way that C-c
does.
To do that, you bind C-x C-x
not to C-c
but to the binding of C-c
. (lookup-key global-map (kbd "C-c"))
tells you that C-c
is bound to command mode-specific-command-prefix
(which is in fact a keymap). So if you bind C-x C-x
to that then it will behave like C-c
:
(global-set-key (kbd "C-x C-x") (lookup-key global-map (kbd "C-c")))
or just:
(global-set-key (kbd "C-x C-x") 'mode-specific-command-prefix)
(global-set-key [f1] (lookup-key global-map (kbd "C-h")))
Feb 12, 2017 at 12:22
For completeness, here's an alternative to Drew's answer. It may or may not be completely equivalent.
(defun simulate-key-press (key)
"Pretend that KEY was pressed.
KEY must be given in `kbd' notation."
`(lambda () (interactive)
(setq prefix-arg current-prefix-arg)
(setq unread-command-events (listify-key-sequence (read-kbd-macro ,key)))))
(global-set-key (kbd "C-x C-x") (simulate-key-press "C-c"))
,key
instead of (read-kbd-macro ,key)
, then simulate-key-press
is just consistent with all the global-set-key
/define-key
/etc functions, and you can do any of (simulate-key-press (kbd "C-c"))
, (simulate-key-press "\C-c")
, or (simulate-key-press [?\C-c])
.
OPTION # 1:
(global-unset-key "\C-x\C-x")
(global-set-key (kbd "C-x C-x C-c") 'help-for-help)
OPTION # 2:
The following example is taken from: .../lisp/textmodes/page-ext.el
and modified slightly:
(global-unset-key "\C-x\C-x")
(defvar ctl-x-ctl-x-map (make-sparse-keymap)
"Keymap for subcommands of C-x C-x, which are for PythonNut.")
(define-key ctl-x-map "\C-x" 'ctl-x-ctl-x-prefix)
(fset 'ctl-x-ctl-x-prefix ctl-x-ctl-x-map)
(define-key ctl-x-ctl-x-map "\C-c" 'help-for-help)
Example creating C-z
as a new global prefix key:
(global-unset-key "\C-z")
(defalias 'ctl-z-keymap (make-sparse-keymap))
(defvar ctl-z-map (symbol-function 'ctl-z-keymap)
"Global keymap for characters following C-z.")
(define-key global-map "\C-z" 'ctl-z-keymap)
(define-key ctl-z-map "\C-c" 'help-for-help)
ctl-x-4-map
leads us tosubr.el
where there is(defvar ctl-x-4-map (make-sparse-keymap) . . .
ctl-x-
will give you every example in Emacs of how to define keys using all of the maps mentioned in the previous comment. And by doing that, we see that I missed one: ctl-x-ctl-p-mapi
case insensitive;n
print line number;I
ignore binary files;E
extended regular expressions; and,r
recursive. I primarily use Emacs on OSX with all the*.el
andsrc
directory packaged into an*.app
directory. After building Emacs, I temporarily change the time on my computer to an hour or so before the build. Then I use another tool to find and extract all of the*.el
files from the*.gz
compressed files into the same directories they are presently in.