Emacs & whitespace

by Theresa O’Connor on 2 January 2009

A lot of people aren’t familiar with some of the nice little ways Emacs helps you keep your text clean. Here are some handy commands related to manipulating whitespace that I use every day.

Trailing whitespace

Setting show-trailing-whitespace to t causes Emacs to highlight any trailing whitespace in the current buffer in the trailing-whitespace face. Use the command delete-trailing-whitespace to delete such whitespace.

English spacing

The command canonically-space-region can be used to fix up the number of spaces between sentences, based on the value of sentence-end-double-space.

Whitespace around point

The command delete-horizontal-space, to which M-\ is bound by default, obliterates whitespace around point, leaving none. A very similar command, just-one-space (available on M-SPC), obliterates whitespace around point, leaving exactly one space.