It is easy to leave unnecessary spaces at the end of a line, or empty lines at the end of a buffer, without realizing it. In most cases, this trailing whitespace has no effect, but sometimes it can be a nuisance.
You can make trailing whitespace at the end of a line visible by
setting the buffer-local variable
t. Then Emacs displays trailing whitespace, using the face
This feature does not apply when point is at the end of the line containing the whitespace. Strictly speaking, that is “trailing whitespace” nonetheless, but displaying it specially in that case looks ugly while you are typing in new text. In this special case, the location of point is enough to show you that the spaces are present.
Type M-x delete-trailing-whitespace to delete all trailing
whitespace. This command deletes all extra spaces at the end of each
line in the buffer, and all empty lines at the end of the buffer; to
ignore the latter, change the variable
nil. If the region is active, the command instead deletes
extra spaces at the end of each line in the region.
On graphical displays, Emacs can indicate unused lines at the end of
the window with a small image in the left fringe (see Fringes).
The image appears for screen lines that do not correspond to any
buffer text, so blank lines at the end of the buffer stand out because
they lack this image. To enable this feature, set the buffer-local
indicate-empty-lines to a non-
nil value. You
can enable or disable this feature for all new buffers by setting the
default value of this variable, e.g.,
Whitespace mode is a buffer-local minor mode that lets you
“visualize” many kinds of whitespace in the buffer, by either
drawing the whitespace characters with a special face or displaying
them as special glyphs. To toggle this mode, type M-x
whitespace-mode. The kinds of whitespace visualized are determined
by the list variable
whitespace-style. Here is a partial list
of possible elements (see the variable's documentation for the full