Another alternative to ordinary line continuation (see Continuation Lines) is to use word wrap. Here, each long logical line is divided into two or more screen lines, or “visual lines”, like in ordinary line continuation. However, Emacs attempts to wrap the line at word boundaries near the right window edge. (If the line’s direction is right-to-left, it is wrapped at the left window edge instead.) This makes the text easier to read, as wrapping does not occur in the middle of words.
Word wrap is enabled by Visual Line mode, an optional minor mode. To turn on Visual Line mode in the current buffer, type M-x visual-line-mode; repeating this command turns it off. You can also turn on Visual Line mode using the menu bar: in the Options menu, select the ‘Line Wrapping in this Buffer’ submenu, followed by the ‘Word Wrap (Visual Line mode)’ menu item. While Visual Line mode is enabled, the mode line shows the string ‘wrap’ in the mode display. The command M-x global-visual-line-mode toggles Visual Line mode in all buffers.
Since word wrap and line truncation (described in the previous
section) are contradictory, turning on visual-line-mode
disables line truncation.
In Visual Line mode, some editing commands work on screen lines
instead of logical lines: C-a (beginning-of-visual-line
)
moves to the beginning of the screen line, C-e
(end-of-visual-line
) moves to the end of the screen line, and
C-k (kill-visual-line
) kills text to the end of the
screen line.
To move by logical lines, use the commands M-x next-logical-line and M-x previous-logical-line. These move point to the next logical line and the previous logical line respectively, regardless of whether Visual Line mode is enabled. If you use these commands frequently, it may be convenient to assign key bindings to them. See Rebinding Keys in Your Init File.
By default, word-wrapped lines do not display fringe indicators.
Visual Line mode is often used to edit files that contain many long
logical lines, so having a fringe indicator for each wrapped line
would be visually distracting. You can change this by customizing the
variable visual-line-fringe-indicators
.
By default, Emacs only breaks lines after whitespace characters like
SPC and TAB, but does not break after whitespace
characters like EN QUAD. Emacs provides a minor mode called
word-wrap-whitespace-mode
that switches on word wrapping in the
current mode, and sets up which characters to wrap lines on based on
the word-wrap-whitespace-characters
user option. There’s also
a globalized version of that mode called
global-word-wrap-whitespace-mode
.
Only breaking after whitespace character produces incorrect
results when CJK and Latin text are mixed
together (because CJK characters don’t use whitespace to separate
words). You can customize the option word-wrap-by-category
to
allow Emacs to break lines after any character with ‘|’ category
(see Categories in the Emacs Lisp Reference Manual), which
provides better support for CJK characters. Also, if this variable is
set using Customize, Emacs automatically loads kinsoku.el.
When kinsoku.el is loaded, Emacs respects kinsoku rules when
breaking lines. That means characters with the ‘>’ category don’t
appear at the beginning of a line (e.g., U+FF0C FULLWIDTH COMMA), and
characters with the ‘<’ category don’t appear at the end of a line
(e.g., U+300A LEFT DOUBLE ANGLE BRACKET). You can view the category
set of a character using the commands char-category-set
and
category-set-mnemonics
, or by typing C-u C-x = with point
on the character and looking at the “category” section in the
report. You can add categories to a character using the command
modify-category-entry
.