14.22 Visual Line Mode
Another alternative to ordinary line continuation is to use word wrap. Here, each long logical line is divided into two or more screen 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.
That 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
.