0

I want to test out a few alternative keybinding setups but I’m not sure what the simplest/most effective way to do that is.

I would like to have it so that instead of C-d being forward delete ("delete-char" which is what it is by default), whenever I hold down C-d I can use the left (kbd <left>?) and right (kbd <right>?) arrow keys to delete backwards and forwards.

I can can’t even get unsetting C-d to work: When I put (global-set-key (kbd "C-d") 'nil) in my init.el and eval-last-sexp, C-d seems to actually be disabled. But if I save-buffer and close emacs, when I open a new instance, C-d is back to being a forward delete.

Once I get past that problem, the next problem is figuring out how to even express "while C-d is pressed, <left> and <right> arrow keys trigger delete-backwards-char and delete-char and if either are held down they will repeatedly call their functions" in the init file in elisp.

If I can get this resolved the proper way, I can apply the solution to repurpose other default keybindings, but at this point I can't imagine solving this single case without some real guidance.

3
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Commented Jul 5, 2022 at 15:03
  • @Drew My mistake, thanks for informing me.
    – Nika
    Commented Jul 5, 2022 at 15:05
  • No problem at all. And yeah, it's not obvious. Thx.
    – Drew
    Commented Jul 5, 2022 at 17:08

2 Answers 2

2

To find the key representation you can do this:

  1. C-x b *scratch*
  2. Type whatever key you are interested in. Don't bother about what Emacs does or doesn't do when you type those keys. In your case you would type Ctrl-d, followed by Right Arrow.
  3. Do C-h l. You will see the following entries
 C-d     ;; delete-char
 <right> ;; right-char
 C-h l   ;; view-lossage

  • Portion before the ;; says what keys you pressed. The portion after ;; says the command they run. Based on the above information you know that Ctrl-d followed by Right Arrow is same as C-d <right>. So, to bind a command to Ctrl-d RightArrow you need to use (kbd "C-d <right>")

  • You want C-d <right> to run what C-d does. The lossage says C-d is mapped to delete-char. So, you would do

(global-set-key (kbd "C-d <right>") 'delete-char)
  1. When you put the above code in .emacs and re-start Emacs you will see the following error message
global-set-key: Key sequence C-d <right> starts with non-prefix key C-d

The above message is a fancy way of saying that you need to unbind C-d. You already know C-d is written as (kbd "C-d"). So, you add

(global-unset-key (kbd "C-d"))

before you global-set-key.


This is the lossage information for all the keys that are relevant to the question

 C-d         ;; delete-char
 <backspace> ;; backward-delete-char-untabify
 C-d         ;; delete-char
 <right>     ;; right-char
 C-d         ;; delete-char
 <left>      ;; left-char
 C-h l       ;; view-lossage

If you follow the above process, this is what you will end up with in your ~/.emacs.

(global-unset-key (kbd "C-d"))
(global-set-key (kbd "C-d <right>") 'delete-char)
(global-set-key (kbd "C-d <left>") 'backward-delete-char-untabify)
1
  • How would this make C-d act as a modifier key similar to Shift? Unless I am using this wrong, here I have to [C-d, then remove my hand from C-d and press <left>] each time I want to backward delete one character; so with the snippet above, I would have to write C-d <left> C-d <left> C-d <left> to delete "hey". Ideally I have it so that I can hold down C-d and while holding down C-d, I can press <left> <left> <left> or just hold down <left> do backwards delete the "hey".
    – Nika
    Commented Jul 5, 2022 at 14:51
1

You need to save the configuration in your ~/.emacs.

  1. C-x C-f ~/.emacs
  2. Copy the following elisp snippet in to your ~/.emacs
(global-unset-key (kbd "C-d"))

(defvar my-Ctrl-d-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "<right>") 'delete-char)
    (define-key map (kbd "<left>") 'delete-backward-char)
    map))
    
(fset 'my-Ctrl-d-map my-Ctrl-d-map)

(map-keymap
 (lambda (key cmd)
   (put cmd 'repeat-map 'my-Ctrl-d-map))
 my-Ctrl-d-map)

(repeat-mode -1)
(repeat-mode 1)

(global-set-key (kbd "C-d") 'my-Ctrl-d-map)

  1. Restart Emacs.

Note that repeat-mode is available in Emacs >= 28.1

3
  • This does just about what I was hoping, but not quite: This snippet allows me to press C-d and then after releasing my hand from the C-d chord, I can press any combination from (<left>*<right>*)* or hold down in one direction and it will delete in that direction. I was hoping that I could get it so that while C-d is held down, the functionality is active, but as soon as I remove my hand from C-d, the <left> and <right> arrow keys go back to just being regular backward and forward movement. I am thinking similar to how the Shift key works. (Upvoted anyway for all your help)
    – Nika
    Commented Jul 5, 2022 at 14:22
  • Based on what you say, you are looking for the other solution emacs.stackexchange.com/a/72443/31220. The actual elisp snippet is at the bottom of that link. FWIW, when you are in repeat-mode, when you press any key other than the repeated keys, you will exit the repeat-mode. Try doing C-d <left> C-g <left> <right>, see what happens. You can replace C-g with any key, as long as it is different from <left> and <right>. Try doing C-d <left> a b <right> <left>.
    – user31220
    Commented Jul 5, 2022 at 14:39
  • I replied to your other post. I think your other post moves further away from what I was originally looking for than this one. I'm sorry if I am not explaining clearly what kind of behavior I am looking for. Did my other comment clarify anything?
    – Nika
    Commented Jul 5, 2022 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.