Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
755 views
in Technique[技术] by (71.8m points)

emacs - kill-word / forward-word should stop at newline

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 ? "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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...