Is it possible to change the behavior of backward-kill-word (bound to M-DEL by default) to stop at special characters? Quite often it deletes parentheses and line breaks against my will.

  • 2
    The following link is a custom function that I use which has its own syntax table for the duration of the function. I have set it up to kill the whole word or the whole whitespace depending upon where the cursor is. It will not delete blank lines. You can adjust the syntax table to make parentheses belong to any syntax group -- the default for this function is punctuation. stackoverflow.com/a/20456861/2112489
    – lawlist
    Commented Jan 11, 2015 at 17:32
  • 1
    Here's a similar question: emacs.stackexchange.com/questions/10644/…
    – user2005
    Commented Jun 10, 2015 at 12:19

2 Answers 2


That command uses forward/backward word internally, i.e. it finds the next word in buffer if not within - and deletes region between. From there it's rather difficult to tweak.Try this:

(defun my-backward-kill-word ()
  (interactive "*")
  (let ((orig (point)))
    (skip-syntax-backward "\sw")
    (delete-region (point) orig)))

I did it like this:

(defun dwim-backward-kill-word ()
  "DWIM kill characters backward until encountering the beginning of a
word or non-word."
  (if (thing-at-point 'word) (backward-kill-word 1)
    (let* ((orig-point              (point))
           (orig-line               (line-number-at-pos))
           (backward-word-point     (progn (backward-word) (point)))
           (backward-non-word-point (progn (goto-char orig-point) (backward-non-word) (point)))
           (min-point               (max backward-word-point backward-non-word-point)))

      (if (< (line-number-at-pos min-point) orig-line) (progn (goto-char min-point) (end-of-line) (delete-horizontal-space))
        (delete-region min-point orig-point)
        (goto-char min-point))

(defun backward-non-word ()
  "Move backward until encountering the beginning of a non-word."
  (search-backward-regexp "[^a-zA-Z0-9\s\n]")
  (while (looking-at "[^a-zA-Z0-9\s\n]")

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.