reddit / emacs

How can I bind a custom key like C-j to a prefix key like C-x?

brightlystar · 2022-08-09 09:51
I have tried these two lines. These two lines are the only lines in my `~/init.el`: (global-unset-key (kbd "C-j")) (global-set-key (kbd "C-j") (kbd "C-x")) Now I edit a file `hello.txt` (so it's fundamental-mode). I type `C-j C-f` hoping it will behave like `C-x C-f` and trigger `find-file` minibuffer. But when I type `C-j C-f`, the cursor moves by one place forward. The mapping of `C-j` to `C-x` has no effect. I check `C-h k C-j` and this help is shown: C-j runs the command "" (found in global-map), which is a keyboard macro. It is bound to C-j. Macro: C-x Keyboard macro. Obviously I am doing something wrong. Can I somehow remap `C-j` to behave exactly like `C-x`?
flexibeast · 2022-08-09 10:04
Try: (global-set-key (kbd "C-j") 'ctl-x-map) More generally: (define-prefix-command 'personal-map) (global-set-key (kbd "<prefix>") 'personal-map)
brightlystar · 2022-08-09 10:23
I now have only these two lines in my `~/init.el`: (global-unset-key (kbd "C-j")) (global-set-key (kbd "C-j") 'ctl-x-map) I edit a file `hello.txt` in fundamental-mode and type `C-j C-f` but it does not behave like `C-x C-f`. Pressing `C-j` causes this error: command-execute: Wrong type argument: commandp, ctl-x-map
flexibeast · 2022-08-09 10:29
Oh. If you do `C-h v ctl-x-map`, does Emacs report that it has a value? Does it make any difference if you (a) just remove the `global-unset-key` line; or (b) replace it with the line: (define-prefix-command 'ctl-x-map) ?
brightlystar · 2022-08-09 10:43
With empty `~/init.el` file, output of `C-h v ctl-x-map` is: ctl-x-map is a variable defined in ‘subr.el’. Its value is shown below. Default keymap for C-x commands. The normal global definition of the character C-x indirects to this keymap. This variable may be risky if used as a file-local variable. Value: (keymap #^[nil nil keymap #1= ... more than 100 more lines with a bunch of key mappings .... ### First test with this `~/init.el`: (global-set-key (kbd "C-j") 'ctl-x-map) Typing `C-j` caused this error: command-execute: Wrong type argument: commandp, ctl-x-map ### Second test with this `~/init.el`: (define-prefix-command 'ctl-x-map) (global-set-key (kbd "C-j") 'ctl-x-map) Typing `C-j` alone does treat it like a prefix key and shows `C-j-` in the bottommost status line to show that it is waiting for the prefix key to be completed. But typing a full key like `C-j C-f` shows this error: C-j C-f is undefined ### Third test with this `~/init.el`: (global-unset-key (kbd "C-j")) (define-prefix-command 'ctl-x-map) (global-set-key (kbd "C-j") 'ctl-x-map) I get the same result as the second test.
flexibeast · 2022-08-09 10:47
So `define-prefix-command` creates an empty `ctl-x-map` keymap. What does `C-h v ctl-x-map` report when you have only (global-set-key (kbd "C-j") 'ctl-x-map) in your `init.el`?
brightlystar · 2022-08-09 10:56
> So define-prefix-command creates an empty ctl-x-map keymap. Yes. I put this `~/init.el`: (define-prefix-command 'ctl-x-map) Then I edit `foo.txt` and type `C-h v ctl-x-map` and get this: ctl-x-map is a variable defined in ‘subr.el’. Its value is (keymap) Default keymap for C-x commands. The normal global definition of the character C-x indirects to this keymap. This variable may be risky if used as a file-local variable. > What does C-h v ctl-x-map report when you have only (global-set-key (kbd "C-j") 'ctl-x-map) in your init.el? Now I have only this in `init.el`: (global-set-key (kbd "C-j") 'ctl-x-map) Now `C-h v ctl-x-map` shows this: ctl-x-map is a variable defined in ‘subr.el’. Its value is shown below. Default keymap for C-x commands. The normal global definition of the character C-x indirects to this keymap. This variable may be risky if used as a file-local variable. Value: (keymap #^[nil nil keymap #1= #^^[3 0 pop-global-mark nil list-buffers save-buffers-kill-terminal list-directory eval-last-sexp find-file nil nil indent-rigidly dired-jump kmacro-keymap downcase-region (keymap (108 . set-language-environment) (99 . universal-coding-system-argument) (28 . set-input-method) followed by more than 100 more lines of key mappings.
crzjp · 2022-08-09 10:59
try this ```(global-set-key (kbd "C-j C-f") 'find-file)```
flexibeast · 2022-08-09 11:01
Well, i'm obviously missing something / misunderstanding something about how things related to the `C-x` prefix works, sorry! Certainly in my own config, i have: (define-prefix-command 'personal-global-map) (global-set-key (kbd "C-x x") 'personal-global-map) ... (define-key personal-global-map (kbd "g") 'magit-status) so i can do `C-x x g` to call `magit-status`. Hopefully someone else will be able to be more help! (Emacs 28 actually annexed `C-x x` for other bindings, which fortunately aren't bindings i particularly need, so i'm happy to override them.)
_viz_ · 2022-08-09 11:05
Dont quote the symbol. It should be (define-key (kbd "C-j") ctl-x-map). Reading the error message tells you the problem.
[deleted] · 2022-08-09 11:11
[deleted]
brightlystar · 2022-08-09 11:14
That was it! Thanks u/_viz_! Now I have: (global-set-key (kbd "C-j") ctl-x-map) Now `C-j C-f`, `C-j C-s`, `C-j C-c` all work fine!