Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kill-word / forward-word should stop at newline

Tags:

emacs

I find it rather annoying that kill-word and forward-word treat newline character as whitespace and e.g. kill everything up to the end of the word in the next line. I would like it to stop at the end of the line instead.

I tried modifying the syntax table to include newline character into the word definition as follows:

(modify-syntax-entry ?\n "w")

This gives the desired effect, but needs to be specified separately for every syntax table.

Is there a way to achieve this effect globally?

like image 729
Andrzej Pronobis Avatar asked Oct 22 '25 21:10

Andrzej Pronobis


1 Answers

Well, first off I would strongly recommend getting used to the idea of treating newlines as whitespace for the most part. Emacs generally does this consistently and trying to buck such a trend might be a never-ending battle.

Secondly I agree with the answer given by Stefan in that messing with the syntax table, or re-defining forward-word itself, will cause you nothing but trouble and grief.

If you really want the behaviour you describe for M-d and M-f then perhaps it would be best to define a new set of functions that have this desired behaviour, and to which you can bind the M-d and M-f keys to.

Indeed this would be the traditional way to change the default behaviour of some core functionality in any emacs.

Maybe something like this? (barely tested)

(defun forward-word-stop-eol (arg)
  (interactive "p")
  (let ((start (point)))
    (save-restriction
      (save-excursion
        (move-end-of-line 1)
        (narrow-to-region start (point)))
      (forward-word arg))))

(defun kill-to-end-of-word-or-line (arg)
  (interactive "p")
  (kill-region (point) (progn (forward-word-stop-eol arg) (point))))

(global-set-key "\ef" forward-word-stop-eol)
(global-set-key "\ek" kill-to-end-of-word-or-line)
like image 113
Greg A. Woods Avatar answered Oct 24 '25 18:10

Greg A. Woods