1

I want to delete char backwards using C-d, not the backspace key. I could do it like that:

(global-set-key (kbd "\C-d") 'delete-backward-char)

However, I have noticed that in fact backspace points to different functions in different major modes. For example, in lisp-mode it is backward-delete-char-untabify function, in C mode c-electric-backspace, in Help mode it is cua-scroll-down. Is it possible to emulate backspace behaviour with C-d without remapping it for every major mode separately? I also tried this:

 (global-set-key (kbd "\C-d") (kbd "<DEL>"))

But it obviously turns every C-d key into <DEL>, so keybindings such C-c C-d stop working, making this not a suitable solution either.

P.S. I am slightly suprised that (as it seems after some googling) not so many people rebind backspace key. You have to take away your hand from home row every time you need to delete a character! This is so uncomfortable...how do you, Emacs people, delete characters?

2
  • I use delete-backward-char instead of rebinding <DEL> and it works so far for me.
    – bertfred
    Commented Oct 29, 2016 at 8:25
  • 1
    You say "it obviously turns every C-d key into <DEL>, so keybindings such as C-c C-d stop working" - are you sure about that? When you press C-c (or C-x, or C-h) another keymap becomes active, in which C-d should NOT be overwritten. As per this doc: gnu.org/software/emacs/manual/html_node/elisp/Prefix-Keys.html
    – VanLaser
    Commented Oct 29, 2016 at 9:49

2 Answers 2

1

You can do this:

(define-key key-translation-map (kbd "C-d") (kbd "DEL"))

to make the C-c C-d work, one can

(define-key key-translation-map (kbd "DEL") (kbd "C-d") )

that is, when you need to press C-d in some key, press backspace instead.

I don't think this remap is a good idea. You trade moving hand and possibly twisting wrist by requiring 2 keys, involving a pinky.

0

Maybe you can try

(bind-key* "C-d" (kbd "<DEL>"))
  • (kbd "<DEL>") is keyboard macro and a command definition. I guess you can also use something like (lambda () (interactive) (call-interactively (key-binding (kbd "<DEL>")))) if you prefer.
  • bind-key* will bind C-d and override any mode-specific binding of C-d. (this function is from bind-key.el, which is part of the use-package project)

By the way, to cancel the effect of the above code

(unbind-key "C-d" override-global-map)

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.