Emacs: Delete Trailing Whitespace

By Xah Lee. Date: . Last updated: .

How to delete trailing whitespaces?

Whitespaces Commands
Command NameArea of ActionAction
delete-trailing-whitespacebuffer or selectiondelete all trailing whitespaces
whitespace-cleanupbuffer or selectiondelete many situations of whitespaces. Complex behavior.

delete-trailing-whitespace

delete-trailing-whitespace deletes trailing whitespace, but also deletes trailing blank lines if delete-trailing-lines is t.

delete-trailing-whitespace is an interactive compiled Lisp function in
‘simple.el’.

(delete-trailing-whitespace &optional START END)

Delete trailing whitespace between START and END.
If called interactively, START and END are the start/end of the
region if the mark is active, or of the buffer’s accessible
portion if the mark is inactive.

This command deletes whitespace characters after the last
non-whitespace character in each line between START and END.  It
does not consider formfeed characters to be whitespace.

If this command acts on the entire buffer (i.e. if called
interactively with the mark inactive, or called from Lisp with
END nil), it also deletes all trailing lines at the end of the
buffer if the variable ‘delete-trailing-lines’ is non-nil.

whitespace-cleanup

whitespace-cleanup has complex behavior.

whitespace-cleanup is an interactive autoloaded Lisp function in
‘whitespace.el’.

(whitespace-cleanup)

Cleanup some blank problems in all buffer or at region.

It usually applies to the whole buffer, but in transient mark
mode when the mark is active, it applies to the region.  It also
applies to the region when it is not in transient mark mode, the
mark is active and C-u was pressed just before
calling ‘whitespace-cleanup’ interactively.

See also ‘whitespace-cleanup-region’.

The problems cleaned up are:

1. empty lines at beginning of buffer.
2. empty lines at end of buffer.
   If ‘whitespace-style’ includes the value ‘empty’, remove all
   empty lines at beginning and/or end of buffer.

3. 8 or more SPACEs at beginning of line.
   If ‘whitespace-style’ includes the value ‘indentation’:
   replace 8 or more SPACEs at beginning of line by TABs, if
   ‘indent-tabs-mode’ is non-nil; otherwise, replace TABs by
   SPACEs.
   If ‘whitespace-style’ includes the value ‘indentation::tab’,
   replace 8 or more SPACEs at beginning of line by TABs.
   If ‘whitespace-style’ includes the value ‘indentation::space’,
   replace TABs by SPACEs.

4. SPACEs before TAB.
   If ‘whitespace-style’ includes the value ‘space-before-tab’:
   replace SPACEs by TABs, if ‘indent-tabs-mode’ is non-nil;
   otherwise, replace TABs by SPACEs.
   If ‘whitespace-style’ includes the value
   ‘space-before-tab::tab’, replace SPACEs by TABs.
   If ‘whitespace-style’ includes the value
   ‘space-before-tab::space’, replace TABs by SPACEs.

5. SPACEs or TABs at end of line.
   If ‘whitespace-style’ includes the value ‘trailing’, remove
   all SPACEs or TABs at end of line.

6. 8 or more SPACEs after TAB.
   If ‘whitespace-style’ includes the value ‘space-after-tab’:
   replace SPACEs by TABs, if ‘indent-tabs-mode’ is non-nil;
   otherwise, replace TABs by SPACEs.
   If ‘whitespace-style’ includes the value
   ‘space-after-tab::tab’, replace SPACEs by TABs.
   If ‘whitespace-style’ includes the value
   ‘space-after-tab::space’, replace TABs by SPACEs.

See ‘whitespace-style’, ‘indent-tabs-mode’ and ‘tab-width’ for
documentation.

Whitespace Topic

  1. Emacs: Clean Empty Lines
  2. Emacs: Delete Trailing Whitespace
  3. Emacs: Delete Whitespace around Cursor
  4. Emacs: Tabs, Space, Indentation Setup
  5. Emacs: Make Whitespaces Visible
  6. Emacs: Newline Representation ^M ^J ^L
  7. Emacs: Batch Convert Line Ending in Dired
Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs