I'm having endless problems with the details of keymap binding syntax.
This style works fine with define-key
, and kbd
on my own keymap name:
;;; make my own keymap, with prefix C-;
(define-prefix-command 'my-prefix)
(define-key global-map (kbd "C-;") 'my-prefix-pairs)
(setq my-prefix-pairs
'(
((kbd "e") function-1)
((kbd "C-f") function-2)
))
(dolist (pair my-prefix-pairs)
(define-key my-prefix (eval (car pair)) (cadr pair)))
This style also works fine, with global-set-key
and mapc
:
;;; Control-C bindings, reserved by Emacs for personal bindings
(mapc
#'(lambda (x) (global-set-key (car x) (car (cdr x))))
'(
("\C-ce" my-function-1)
("\C-c\C-f" my-function-2)
))
But I just can't this style to work for global-set-key
and kbd
:
(setq my-prefix-pairs
'(
("e" function-1)
("C-f" function-2)
))
(dolist (pair my-prefix-pairs)
(global-set-key (kbd (concat "C-c " (car pair))) (cadr pair)))
What am I doing wrong in the last style? What would work, to bind keys globally in the Control-X-prefix map? I've looked at other keybinding postings on SO, but didn't find anything that worked for this particular problem.
Update 1 for comment below
Very strange to hear from a poster below that this code works with emacs -Q
. I can't imagine why other styles work fine, but this style won't work with my init setup.
Anyhow, to answer his question, with the following binding at the top of the binding list,
("w" kill-ring-save)
I get this error from the debugger:
Debugger entered--Lisp error: (error "Key sequence C-c w starts with non-prefix key C-c")
define-key((keymap #^[nil nil keymap ...
#^^[3 0 set-mark-command move-beginning-of-line ...
#^^[1 0 #^^[2 0
global-set-key("^Cw" kill-ring-save)) ;;the ^C is a real ^C
(while --dolist-tail-- (setq pair (car --dolist-tail--))
(global-set-key (kbd (concat "C-c " (car pair))) (cadr pair))
(setq --dolist-tail-- (cdr --dolist-tail--)))
(let ((--dolist-tail-- kwj-ctl-c-pairs) pair)
(while --dolist-tail-- (setq pair (car --dolist-tail--))
(global-set-key (kbd (concat "C-c " (car pair))) (cadr pair))
(setq --dolist-tail-- (cdr --dolist-tail--))))
eval((let ((--dolist-tail-- kwj-ctl-c-pairs) pair)
(while --dolist-tail-- (setq pair (car --dolist-tail--))
(global-set-key (kbd (concat "C-c " (car pair))) (cadr pair))
(setq --dolist-tail-- (cdr --dolist-tail--)))) nil)
And using emacs -Q
and pasting this code into the scratch buffer, I also get a debugger error:
(setq my-prefix-pairs
'(
("e" function-1)
("C-f" function-2)
))
(dolist (pair my-prefix-pairs)
(global-set-key (kbd (concat "C-c " (car pair))) (cadr pair)))
Debugger entered--Lisp error: (wrong-type-argument integer-or-marker-p (concat "C-c " (car pai$
read-kbd-macro((concat "C-c " (car pair)))
#[(keys) "\301^H!\207" [keys read-kbd-macro] 2 1987665]((concat "C-c " (car pair)))
(kbd (concat "C-c " (car pair)))
(global-set-key (kbd (concat "C-c " ...)) (cadr pair))
Update 2 after more comments and hacking
After more hacking and the "your code works for me" comment below, I starting stripping everything down in my init, and trying to build up my keybindings from scratch.
I am running Gnu Emacs 24.5.1 as a .dmg app on a recent Mac OSX machine. (But the problem also occurs with the native OSX Emacs v 22.)
Here is one problematic line, for certain (Actually, I show two syntaxes for it). It causes the error if it is in, and I get no errors if it is out.
;; ("C- " set-mark-command)
;; ("C-SPC " set-mark-command)
Debugger entered--Lisp error:
(error "Key sequence C-c w starts with non-prefix key C-c")
Those lines both cause the error, even though a similar syntax "C-SPC"
in this line does not. (This line uses define-key global-map
, not (global-set-key...
).
(define-key global-map (kbd "C-SPC") 'avy-goto-word-or-subword-1)
And I would swear I've had this line in my init for months, with apparently no problem, but it won't work now. (I say this because my fingers automatically go to ctl-c ctl-spc to set the mark..) Neither version works now, and my finger-memory is protesting... :-)
(define-key global-map (kbd "C-c C-SPC") 'set-mark-command)
(global-set-key (kbd "C-c C-SPC") set-mark-command)
my-prefix-pairs
instead ofmy-ctl-c-pairs
.emacs -Q
. What happens when you do it?kbd
was changed from macro to function.