84

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

4
  • Do you want to change the display, or where newline characters are inserted? Commented Jul 19, 2010 at 13:57
  • Just the display. Similar to the behavior of MS Notepad. Commented Jul 19, 2010 at 13:59
  • 11
    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. Commented Jul 25, 2012 at 4:36
  • 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). Commented Sep 29, 2021 at 16:39

5 Answers 5

96

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)
Sign up to request clarification or add additional context in comments.

2 Comments

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.
41

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.

3 Comments

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.
33

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.

3 Comments

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?
@PeterMortensen The following init files are tried in this order: ~/.emacs.el, ~/.emacs, ~/.emacs.d/init.el . I prefer the last one since it allows me to put other stuff I may need in a dir dedicated to Emacs
9

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)

2 Comments

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

Enable global-word-wrap-whitespace-mode.

To make it permanent, add (global-word-wrap-whitespace-mode t) to your .emacs file or enable it with its customize-option.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.