From b08b5cca09e1534b7ec28f516c891065eff80a9f Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Tue, 13 Feb 2024 10:51:22 +0100 Subject: [PATCH] Add 'kill-region-or-word' command * lisp/simple.el (kill-region-or-word): Add it. * etc/NEWS: Document it. --- etc/NEWS | 6 ++++++ lisp/simple.el | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/etc/NEWS b/etc/NEWS index 6fae64728f2..e7e516d61a1 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -223,6 +223,12 @@ executable, if it exists. This should remove the need to change its value when installing GNU coreutils using something like ports or Homebrew. +--- +** New command 'kill-region-or-word'. +This command will kill a region, if it is active, or delete the last +word. As such, it is a convenient alternative binding for C-w, +providing a DWIM behaviour for both Emacs and Unix users. + +++ ** cl-print diff --git a/lisp/simple.el b/lisp/simple.el index 9a33049f4ca..bf4080fcf2d 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -8798,6 +8798,15 @@ backward-kill-word (interactive "p") (kill-word (- arg))) +(defun kill-region-or-word () + "Call `kill-region' if there is an active region. +Otherwise kill the last word, just like Unix." + (interactive) + (call-interactively + (if (use-region-p) + #'kill-region + #'backward-kill-word))) + (defun current-word (&optional strict really-word) "Return the word at or near point, as a string. The return value includes no text properties. -- 2.43.0