问题
First of all, this is not a dup of
- how to delete region with [delete] key in emacs, which is for Emacs 23, and the answer is Emacs
cua-mode
. - How configure delete-selection-mode to only delete?, which is for Emacs 22, and the issue is not reproducable.
I'm talking about the Emacs Delete Selection Mode, I used to be able to delete selection of text by just pressing Delete key. I found my proof here.
In order to set emacs to delete the selected text when you press DEL, Ctrl-d, or Backspace, add the following to your .emacs file:
(delete-selection-mode t)
However, now, with my Emacs 24, the Delete key doesn't work on the selection anymore (only Backspace does). This is really annoying because for all editors that I use, pressing the Delete key will delete the text selection. I don't understand why Emacs has to be so different and inconvenient. Anyway,
Any easy way to fix it? I don't know whether the comment of the following was a joke or the actual solution:
(setq behave-like-something-actually-usable-by-humans t)
Thanks
PS, my Emacs 24 is from nightly build:
$ emacs --version | head -1
GNU Emacs 24.3.50.1
EDIT: The answer from Drew, if it is unclear to you (I got that on third attempt),
(global-set-key (kbd "<delete>") '(lambda (n) (interactive "p") (if (use-region-p) (delete-region (region-beginning) (region-end)) (delete-char n))))
works for me.
回答1:
The <delete>
key is bound by default to kill-line
. It sounds like you want to bind it instead to delete-region
. Just do it.
(global-set-key (kbd "<delete>") 'delete-region)
回答2:
There appears to have been a change to the behavior of delete-char
. I experienced that as well as I had bound DEL to that command. I had success binding the key to delete-forward-char
instead, which implements the old behavior. See here.
回答3:
In 21.4.22, I had to use the (region-active-p)
function instead of (use-region-p)
(global-set-key `backspace '(lambda (n) (interactive "p") (if (region-active-p) (delete-region (region-beginning) (region-end)) (delete-backward-char n))))
来源:https://stackoverflow.com/questions/20816385/emacs-use-delete-key-to-delete-selection