ChatGPT解决这个技术问题 Extra ChatGPT

How can I enable line wrap on word boundaries only in Emacs?

How do I configure Emacs so that line wrapping does not break in the middle of a word?

Do you want to change the display, or where newline characters are inserted?
Just the display. Similar to the behavior of MS Notepad.
Not sure what version of Notepad you're using, but certainly as of version 6.1 (comes with Windows 7) it definitely does NOT save the line break. I've never actually known it to do this. While I've never defended notepad for anything before, in terms of word wrap, it implements this feature exactly perfectly (and no internet discussion!) whereas each query into how emacs does word wrap always gives you like 15 different ways of doing it on 100 different websites, blogs, posts, etc., when the poster 99% of the time just wants it to work like every other modern text editor/viewer.
For the original misleading title (that may turn up in search engine results), see e.g. How can I turn off Emacs's auto line wrapping for the current session?. Alt + X, toggle-truncate-lines (tab completion works) will turn word wrapping off for the current document. To make it permanent, vi ~/.emacs and add this line: (set-default 'truncate-lines t) (yes, only one single quote).

A
Anton Tarasenko

If you want to emulate the behavior of an editor like Notepad, you might want to turn on visual line mode. While setting word-wrap will cause line wrapping at word boundaries, any action you take on a line (e.g., moving up/down or killing) will still respect the newline character. Visual line mode will treat each display line as though it had a newline at the end.

(visual-line-mode t)

Line to add in .emacs file:

(global-visual-line-mode t)

I was using visual-line-mode but was getting annoyed by the way kill-line then only killed up as far as the wrap, so for long lines of text (ie paragraphs!) I had to call it repeatedly to delete the line. So now I use (setq-default word-wrap t) instead; this enables wrapping but kill-line still kills the whole line. You may not agree that this is the preferred behaviour, of course, but for me it certainly is.
@gimboland Thanks. Your recommendation was exactly what I was looking for.
P
Peter Mortensen

M-x toggle-truncate-lines disable allows you to disable visually line breaking.

M-x auto-fill-mode + M-q allows you to word wrap for real a pre-existing paragraph.


When you say "for real" does that mean that emacs will put in a newline?
I noticed it works for the new lines in an existing org file. But it doesn't wrap the existing lines that are long. Any idea how to wrap the existing lines at a certain col (i.e 80) in an org file? Thanks
M-q runs fill-paragraph, which will wrap already existing lines of text. The variable fill-column determines the line length of the wrapped text.
h
huaiyuan

Add this to your init file:

(setq-default word-wrap t)

Alternatively, press C-h vword-wrap in Emacs and follow the "customize" link near the end.


This is the best answer and without using any modes.
Where is the init file located? Can you provide an example location? Is it file ~/.emacs?
P
Peter Mortensen

I discovered longlines-mode only recently (I think I was spelunking through the Emacs Info documentation). It wraps as you would expect in other UI editors' word-wrap feature. It's especially useful when I'm reading or writing free text with no newlines (a la Microsoft Word) without the ugly mid-word wrapping that happens when you use M-x toggle-word-wrap.

See LongLines.

My configuration:

(setq longlines-wrap-follows-window-size t)
(global-set-key [(control meta l)] 'longlines-mode)

According to the wiki, long lines mode is deprecated in favor of visual line mode (mentioned in Anton's answer below.
In which file (incl. location) should the configuration be placed?