开发者

delete only one character when region is highlighted

I cannot find the setting that prevents deletion of an entire region when you have it highlighted and you hit backspa开发者_如何学编程ce... (I just want it to delete one character even if a region is highlighted.) I already have

(delete-selection-mode 0)

and in custom-set-variables (I have cua-mode enabled for its rectangle functions),

'(cua-delete-selection nil)

but that is the behavior I get. Am I missing something?


Found it:

(setq delete-active-region nil)

is the answer. Thanks to all for toughing it out with me!


Emacs has different behavior depending on whether the highlighting was done with mouse or the keyboard.

Even in transient-mark-mode, if you set the mark and move the point, using backspace will not delete the region. delete-selection-mode is a minor mode that changes this behavior.

When using the mouse to highlight a region, regardless of delete-selection-mode, using backspace will delete the region that was highlighted with the mouse. From Section 25.1.1 of the manual:

"While the region remains active, typing or deletes the text in that region and deactivates the mark; this behavior follows a convention established by other graphical programs, and it does not apply when you set the region any other way, including shift-selection (*note Shift Selection::)."

Based on this, it sounds like you're selecting the region using the mouse. Is that correct? Does the same behavior arise when using shift-select-mode or simply using mark and point?


In ELisp, 0 is considered as true.

Try by setting

(setq delete-selection-mode nil)


As has been noted, cua-mode doesn't handle this nicely. The simplest way to retain that mode without this problem would seem to be to redefine the cua keymap (see cua--init-keymaps) after initialising the mode, to unbind the various delete keys from cua-delete-region:

(add-hook 'cua-mode-hook 'my-cua-mode-hook)
(defun my-cua-mode-hook ()
  (define-key cua--region-keymap [remap delete-backward-char] 'delete-backward-char)
  (define-key cua--region-keymap [remap backward-delete-char] 'backward-delete-char)
  (define-key cua--region-keymap [remap backward-delete-char-untabify] 'backward-delete-char-untabify)
  (define-key cua--region-keymap [remap delete-char] 'delete-char))


Apparently, this makes it work fine for the backspace. Even in cua mode:

;; Let backspace and delete be defined as usual if
;; nothing is highlighted, otherwise the marked region is deleted
(defun kill-something() (interactive)
  (if (and mark-active transient-mark-mode)
      (kill-region (point) (mark)) 
    (backward-delete-char-untabify 1)))
;;(global-set-key [delete] 'kill-something)
(global-set-key [backspace] 'kill-something)

This code was found here.


Add (delete-selection-mode nil) to your configuration file.

From the documentation inside Emacs:

delete-selection-mode is an interactive compiled Lisp function in `delsel.el'.

(delete-selection-mode &optional ARG)

Toggle Delete Selection mode. With prefix ARG, turn Delete Selection mode on if ARG is positive, off if ARG is not positive.

When Delete Selection mode is enabled, Transient Mark mode is also enabled and typed text replaces the selection if the selection is active. Otherwise, typed text is just inserted at point regardless of any selection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜