Backward-kill-word-or-join-lines

posted on 2022-05-08

I don’t remember where I encountered it, or what editor had this feature built-in, but I always felt that backward-kill-word should not kill the word on the previous line if executed and there is no word to delete on the current line. It should rather join the lines.

Couldn’t find any ready-made customization to borrow, like any self respecting Igor from the Terry Pratchett’s novells would do, so I have written one after inspecting the Emacs built-in.

(defun backward-kill-word-or-join-lines ()
  "Backward-kill-word that will join lines if no word on the current line to kill."
  (interactive)
  (let ((orig-point (point))
         (start-line (line-number-at-pos)))
    (backward-word)
    (if (> start-line (line-number-at-pos))
          (forward-word))
    (kill-region (point) orig-point)))

Putting it here just for reference. Happy hacking!