How To Use And Setup Emacs's whitespace-mode

Xah Lee, 2009-08-14, 2009-10-06

This pages tells you how to setup emacs's whitespace-mode, and how to use it.

Emacs 23 has this whitespace-mode feature. It renders spaces, tabs, newlines characters with a visible glyph.

This feature is useful for working with “tab separated values” (CSV) that's a common format for importing/exporting address books or spreadsheets. It's also important in whitespace-significant langs such as Python.

To use it, type 【Alt+x whitespace-mode】 to toggle it on and off, for current file. Type 【Alt+x global-whitespace-mode】 to toggle it globally for current emacs session. There is also whitespace-newline-mode and global-whitespace-newline-mode. They only show newline chars.

Setting Up whitespace-mode

emacs whitespace

The default rendering of whitespace-mode. Different placement and mix of whitespaces are rendered with different colors. Also, long lines are colored dark purple. (download whitespace_sample_file.txt)

emacs whitespace clean

A clean setup for whitespace-mode.

How to reduce colors in whitespace-mode?

Put the following in your emacs init file:

; make whitespace-mode use just basic coloring
(setq whitespace-style (quote
  ( spaces tabs newline space-mark tab-mark newline-mark)))

How to make whitespace-mode use the pilcrow sign “¶” for newline instead of the dollar sign?

Put the following in your emacs init file:

;; make whitespace-mode use “¶” for newline and “▷” for tab.
;; together with the rest of its defaults
(setq whitespace-display-mappings
 '(
   (space-mark 32 [183] [46]) ; normal space
   (space-mark 160 [164] [95])
   (space-mark 2208 [2212] [95])
   (space-mark 2336 [2340] [95])
   (space-mark 3616 [3620] [95])
   (space-mark 3872 [3876] [95])
   (newline-mark 10 [182 10]) ; newlne
   (tab-mark 9 [9655 9] [92 9]) ; tab
))

In the above, the numbers are unicode char code in decimal. Depending on your choice of font, some glyphs may not show up with your font. If so, you can try the following choices for your line return char or tab char.

GlyphUnicode
Code Point
(Decimal)
Unicode Char Name
·183MIDDLE DOT
182PILCROW SIGN
8629DOWNWARDS ARROW WITH CORNER LEFTWARDS
8617LEFTWARDS ARROW WITH HOOK
9166RETURN SYMBOL
9655WHITE RIGHT POINTING TRIANGLE
9654BLACK RIGHT-POINTING TRIANGLE
8594RIGHTWARDS ARROW
8614RIGHTWARDS ARROW FROM BAR
8677RIGHTWARDS ARROW TO BAR
8680RIGHTWARDS WHITE ARROW

Common Questions For Editing Whitespaces

How to delete all whitespaces?

Select a text then type 【Alt+x whitespace-cleanup】. This will delete spaces or tabs in a smart way. (For detail on exactly which whitespace it deletes, type 【Alt+x describe-function】, then “whitespace-cleanup”.)

Type 【Alt+x delete-trailing-whitespace】. This will delete all trailing whitespaces in the whole buffer.

Type 【Alt+x delete-whitespace-rectangle】. It acts on the vertical section of the beginning and ending position of your text selection.

For more fine control of deleting whitespaces, you can use use query-replace, or query-replace-regexp, to replace whitespaces.

How to insert Tab or Newline char?

The following methods works everywhere, including when you are in minibuffer.

To insert a literal tab char, press 【Ctrl+q Tab】.

To type a newline char, type 【Ctrl+q Ctrl+j】.

You need to use the above method to insert these characters, because for example in minibuffer, pressing Tab does name completion and pressing Return finishes the prompt. In most programing language modes, pressing Return or Tab also does some auto indenting and formatting.

Note: doesn't matter if your file uses Unix or Windows or classic Mac's Newline convention, emacs always uses Line Feed (ascii 10) to represent them in the buffer.

Was this page useful? If so, please do donate $3, thank you donors!
Home
Terms of Use
About
Advertise
Subscribe
Google
2006-07
© 2006 by Xah Lee.