15.17 Useless Whitespace

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 show-trailing-whitespace to t. Then Emacs displays trailing whitespace, using the face trailing-whitespace.

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 delete-trailing-lines to 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 Window 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 variable 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., (setq-default indicate-empty-lines t).

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. Individual elements in that list can be toggled on or off in the current buffer by typing M-x whitespace-toggle-options. Here is a partial list of possible elements (see the variable’s documentation for the full list):


Enable all visualizations which use special faces. This element has a special meaning: if it is absent from the list, none of the other visualizations take effect except space-mark, tab-mark, and newline-mark.


Highlight trailing whitespace.


Highlight tab characters.


Highlight space and non-breaking space characters.


Highlight lines longer than 80 columns. To change the column limit, customize the variable whitespace-line-column.


Highlight newlines.


Highlight the final character if the buffer doesn’t end with a newline character.


Highlight empty lines at the beginning and/or end of the buffer.


Highlight too-deep indentation. By default any sequence of at least 4 consecutive tab characters or 32 consecutive space characters is highlighted. To change that, customize the regular expression whitespace-big-indent-regexp.


Draw space and non-breaking characters with a special glyph.


Draw tab characters with a special glyph.


Draw newline characters with a special glyph.

Global Whitespace mode is a global minor mode that lets you visualize whitespace in all buffers. To toggle individual features, use M-x global-whitespace-toggle-options.