Reputation: 36096
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?
Upvotes: 4
Views: 881
Reputation: 207
I used with-syntax-table
to temporarily (and globally) modify the syntax table.
E.g., I used it with syntax-subword
mode:
(defadvice syntax-subword-forward (around testing activate)
(with-syntax-table (copy-syntax-table (syntax-table))
(modify-syntax-entry ?\n ">")
ad-do-it))
Upvotes: 0
Reputation: 2792
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)
Upvotes: 4
Reputation: 28541
Changing the syntax of the ?\n
character will get you quickly into trouble in all kinds of modes. Instead, you want to change the way things like forward-word
behave when called interactively (note that changing the behavior of the function forward-word
can also lead to trouble because it is often used in things like indentation code). You might like to take a look at how subword-mode
works for an example of how this can be done.
Upvotes: 1