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.
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." (interactive) (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." (interactive) (search-backward-regexp "[^a-zA-Z0-9\s\n]") (while (looking-at "[^a-zA-Z0-9\s\n]") (backward-char)) (forward-char))