ShowWhiteSpace

This page is about making particular whitespace characters stand out better.

Emacs comes with a standard solution: M-x whitespace-mode. See WhiteSpace.

Some Details

GNU Emacs 24 has internal variable ‘nobreak-char-display’ (turned on by default), which highlights hard space and hard hyphen characters. But the face is hardwired, and you cannot highlight only one of these chars.

GNU Emacs 22 or later has library whitespace.el, which lets you highlight whitespace in several ways.

GNU Emacs 21 or later provides the option ‘show-trailing-whitespace’ to highlight trailing whitespace in face ‘trailing-whitespace’.

CPerl

CPerl shows trailing whitespace by default. You can specify the face to use for trailing whitespace. You can use face ‘default’ for invisible whitespace, if you don’t care about trailing whitespace:

(setq cperl-invalid-face 'default)

highlight-chars.el

(Library highlight-chars.el replaces obsolete library show-wspace.el.)

Library highlight-chars.el lets you easily highlight any sets of characters that you choose, including whitespace characters. There are commands and functions to use on hooks.

You can highlight whitespace and other easily confused characters: spaces, tabs, hard spaces, and hard hyphens, as well as trailing whitespace.

For example, command ‘hc-toggle-highlight-tabs’ turns on/off highlighting of tab chars. Or you can turn on tab highlighting by default by adding function ‘hc-highlight-tabs’ to ‘font-lock-mode-hook’ in your init file:

   (add-hook 'font-lock-mode-hook 'hc-highlight-tabs)

You can turn on/off the highlighting provided by library highlight-chars.el:

Not Just Whitespace - Highlight Any Set of Characters

Unlike what is offered by libraries such as whitespace.el, with library highlight-chars.el you are not limited to whitespace highlighting. You can highlight any set of characters. You can define the set using any number of entries of any of these kinds:

You can use any faces to do this.

In addition, you can exclude any set of characters from this highlighting.

You can thus, for example, highlight all characters in character set ‘greek-iso8859-7’ except ‘GREEK SMALL LETTER LAMBDA’. Or all characters in class [:space:] (whitespace) except ‘tab’. Or all Unicode characters in the range ?\u2190 through ?\u21ff (mathematical arrows) except ?\u21b6, ?\u21b7, ?\u21ba, and ?\u21bb (curved arrows). You get the idea.

BlankMode

blank-mode.el (BlankMode) was a precursor to standard library whitespace.el (see WhiteSpace) and has been replaced by it. When blank-mode is on, it looks like this:

BlankModeOn

BlankMode saves and restores the font-lock state when it is turned on and off. So, if BlankMode is turned on and font-lock is off, BlankMode also turns on the font-lock to highlight blanks, but font-lock is turned off when BlankMode is turned off. So turn on font-lock before BlankMode, if you want font-lock to stay on after BlankMode is turned off.

Visible Whitespace Mode

visws.el - Simple and very easy to use. Shows you tabs, spaces and newlines:

VisWS

Red Space

Library redspace.el detects and highlights trailing whitespace.

80-Column Rule

EightyColumnRule provides highlighting of trailing spaces, TABs, and lines past column 80.

Show Whitespace Minor Mode

ShowWhitespaceMode shows whitespace in either of two ways:

Show Tabs in Modes

This code uses font-lock to highlight TABs with face ‘extra-whitespace-face’ in ‘emacs-lisp-mode’ and in ‘text-mode’. You can add an add-hook statement for every other mode you want this for. Put the following in your init file:

 (defface extra-whitespace-face
   '((t (:background "pale green")))
   "Used for tabs and such.")
 (defvar my-extra-keywords
   '(("\t" . 'extra-whitespace-face)))
 (add-hook 'emacs-lisp-mode-hook
	   (lambda () (font-lock-add-keywords nil my-extra-keywords)))
 (add-hook 'text-mode-hook
	   (lambda () (font-lock-add-keywords nil my-extra-keywords)))

To show tabs in the same color as trailing whitespace:

    ;; Draw tabs with the same color as trailing whitespace  
    (add-hook 'font-lock-mode-hook  
              (lambda ()  
                (font-lock-add-keywords  
                  nil  
                  '(("\t" 0 'trailing-whitespace prepend)))))

Show TABs Using a String (^I)

To make TAB display as ^I, you can do this:

 (standard-display-ascii ?\t "^I")

Show TAB with Wide Cursor

Customize ‘x-stretch-cursor’ to non-‘nil’ to make the cursor as wide as the character it is over.

Get Rid of Tabs

If you want to change spaces into TABs or the other way around, use ‘M-x tabify’ or ‘M-x untabify’. See also NoTabs.


CategoryDotEmacs CategoryFaces CategoryDisplay