This page describes package Lisp:whitespace.el, by ViniciusJoseLatorre.
See also: ShowWhiteSpace.
The screenshots below show when WhiteSpace is turned on using the default setting.
This package is a minor mode to visualize blanks (TAB, (HARD) SPACE and NEWLINE).
WhiteSpace uses two ways to visualize blanks: Faces and display table.
?\xB6
), at SPACEs (?\xB7
) and at TABs (?\xBB
).The ‘whitespace-style’
variable selects which way blanks are visualized.
Note that when WhiteSpace is turned on, WhiteSpace saves the font-lock state, that is, if font-lock is on or off. And WhiteSpace restores the font-lock state when it is turned off. So, if WhiteSpace is turned on and font-lock is off, WhiteSpace also turns on the font-lock to highlight blanks, but the font-lock will be turned off when WhiteSpace is turned off. Thus, turn on font-lock before WhiteSpace is on, if you want that font-lock continues on after WhiteSpace is turned off.
When WhiteSpace is on, it takes care of highlighting some special characters over the default mechanism of ‘nobreak-char-display’
(which see) and ‘show-trailing-whitespace’
(which see).
The trailing spaces are not highlighted while point is at end of line. Also the spaces at beginning of buffer are not highlighted while point is at beginning of buffer; and the spaces at end of buffer are not highlighted while point is at end of buffer.
There are two ways of using WhiteSpace: local and global.
‘whitespace-global-modes’
variable controls which major-mode will be automagically turned on.You can mix the local and global usage without any conflict. But local WhiteSpace has priority over global WhiteSpace. WhiteSpace is active in a buffer if you have enabled it in that buffer or if you have enabled it globally.
When global and local WhiteSpace are on:
To use WhiteSpace, insert in your ~/.emacs
:
(require 'whitespace)
Or autoload at least one of the commands‘whitespace-mode’
, ‘whitespace-toggle-options’
, ‘global-whitespace-mode’
or ‘global-whitespace-toggle-options’
. For example:
(autoload 'whitespace-mode "whitespace" "Toggle whitespace visualization." t) (autoload 'whitespace-toggle-options "whitespace" "Toggle local `whitespace-mode' options." t)
WhiteSpace was inspired by:
whitespace.el
– Rajesh Vaidheeswarran <rv@gnu.org>whitespace.el
which was replaced by blank-mode.el
(BlankMode). And later blank-mode.el
was renamed to whitespace.el
.show-whitespace-mode.el
– Aurélien Tisné <aurelien.tisne@free.fr>whitespace-mode.el
– LawrenceMitchellvisws.el
– MilesBaderFor good performance, be sure to byte-compile WhiteSpace, e.g.
M-x byte-compile-file <give the path to whitespace.el when prompted>
This will generate whitespace.elc, which will be loaded instead of Lisp:whitespace.el.
WhiteSpace was tested with GNU Emacs 22 and 23.
There is no problem if you mix local and global minor mode usage.
LOCAL WhiteSpace:
M-x whitespace-toggle-options RET
C-u 1 M-x whitespace-mode RET
C-u 0 M-x whitespace-mode RET
M-x whitespace-mode RET
GLOBAL WhiteSpace:
M-x global-whitespace-toggle-options RET
C-u 1 M-x global-whitespace-mode RET
C-u 0 M-x global-whitespace-mode RET
M-x global-whitespace-mode RET
You can also bind ‘whitespace-mode’
, ‘global-whitespace-mode’
, ‘whitespace-toggle-options’
and ‘global-whitespace-toggle-options’
to some key, like:
(global-set-key "\C-c_w" 'whitespace-mode) (global-set-key "\C-c_t" 'whitespace-toggle-options) (global-set-key "\C-c=w" 'global-whitespace-mode) (global-set-key "\C-c=t" 'global-whitespace-toggle-options)
There are also the following useful commands:
‘whitespace-newline-mode’
"nl"
on modeline).‘global-whitespace-newline-mode’
"NL"
on modeline).‘whitespace-report’
‘whitespace-report-region’
‘whitespace-cleanup’
‘whitespace-cleanup-region’
The problems, which are cleaned up, are:
‘whitespace-style’
includes the value ‘empty’
, remove all empty lines at beginning and/or end of buffer.‘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.‘whitespace-style’
includes the value `indentation::tab’, replace 8 or more SPACEs at beginning of line by TABs.‘whitespace-style’
includes the value `indentation::space’, replace TABs by SPACEs.‘whitespace-style’
includes the value ‘space-before-tab’
: replace SPACEs by TABs, if ‘indent-tabs-mode’
is non-nil; otherwise, replace TABs by SPACEs.‘whitespace-style’
includes the value `space-before-tab::tab’, replace SPACEs by TABs.‘whitespace-style’
includes the value `space-before-tab::space’, replace TABs by SPACEs.‘whitespace-style’
includes the value ‘trailing’
, remove all SPACEs or TABs at end of line.”‘whitespace-style’
includes the value ‘space-after-tab’
: replace SPACEs by TABs, if ‘indent-tabs-mode’
is non-nil; otherwise, replace TABs by SPACEs.‘whitespace-style’
includes the value `space-after-tab::tab’, replace SPACEs by TABs.‘whitespace-style’
includes the value `space-after-tab::space’, replace TABs by SPACEs.WhiteSpace has the following hook variables:
‘whitespace-mode-hook’
‘global-whitespace-mode-hook’
‘whitespace-load-hook’
Below it’s shown a brief description of WhiteSpace options, please, see the options declaration in the code for a long documentation.
‘whitespace-style’
‘whitespace-space’
‘whitespace-hspace’
‘whitespace-tab’
‘whitespace-newline’
‘whitespace-trailing’
‘whitespace-line’
‘whitespace-space-before-tab’
‘whitespace-indentation’
‘whitespace-empty’
‘whitespace-space-after-tab’
‘whitespace-space-regexp’
‘whitespace-hspace-regexp’
‘whitespace-tab-regexp’
‘whitespace-trailing-regexp’
‘whitespace-space-before-tab-regexp’
‘whitespace-indentation-regexp’
‘whitespace-empty-at-bob-regexp’
‘whitespace-empty-at-eob-regexp’
‘whitespace-space-after-tab-regexp’
‘whitespace-line-column’
‘whitespace-display-mappings’
‘whitespace-global-modes’
‘whitespace-action’
To set the above options you may:
a. insert the code in your ~/.emacs
, like:
(setq whitespace-space 'underline)
This way always keep your default settings when you enter a new Emacs session.
b. or use ‘set-variable’
in your Emacs session, like:
M-x set-variable RET whitespace-space RET underline RET
This way keep your settings only during the current Emacs session.
c. or use customization, for example:
In Emacs 21 or lower:
Help
option,Customize
,Browse Customization Groups
,Data
group,Whitespace
groupIn Emacs 22 or higher:
Options
option,Customize Emacs
,Browse Customization Groups
,Data
group,Whitespace
groupThrough this way, you may choose if the settings are kept or not when you leave out the current Emacs session.
d. or see the option value:
C-h v whitespace-space RET
and click the customize
hypertext button.
Through this way, you may choose if the settings are kept or not when you leave out the current Emacs session.
Thanks to DavidReitter for suggesting a ‘whitespace-newline’
initialization with low contrast relative to the background color.
Thanks to Stephen Deasey <sdeasey@gmail.com> for the ‘indent-tabs-mode’
usage suggestion.
Thanks to Eric Cooper <ecc@cmu.edu> for the suggestion to have hook actions when buffer is written as the original whitespace package had.
Thanks to nschum for the idea about highlight “long” lines tail. See EightyColumnRule.
Thanks to JuriLinkov for suggesting:
‘define-minor-mode’
.‘global-whitespace-*’
name for global commands.Thanks to Robert J. Chassell <bob@gnu.org> for doc fix and testing.
Thanks to DrewAdams for toggle commands suggestion.
Thanks to Antti Kaihola <antti.kaihola@linux-aktivaattori.org> for helping to fix ‘find-file-hooks’
reference.
Thanks to Andreas Roehler <andreas.roehler@easy-emacs.de> for indicating defface byte-compilation warnings.
Thanks to TimOCallaghan for the idea about highlight “long” lines. See EightyColumnRule.
Thanks to Yanghui Bian <yanghuibian@gmail.com> for indicating a new NEWLINE character mapping.
Thanks to Pete Forman <pete.forman@westgeo.com> for indicating whitespace-mode.el
on XEmacs.
Thanks to:
whitespace.el
show-whitespace-mode.el
(ShowWhitespaceMode)whitespace-mode.el
(WhitespaceMode)visws.el
(Emacs 21)And to all people who contributed with them.
The ‘whitespace-style’
variable specifies which way blanks are visualized.
It’s a list containing some or all of the following values:
face
trailing
‘face’
(see above) is present in ‘whitespace-style’
.tabs
‘face’
(see above) is present in ‘whitespace-style’
.spaces
‘face’
(see above) is present in ‘whitespace-style’
.lines
‘whitespace-line-column’
are highlighted via faces.‘lines-tail’
(see below).‘face’
(see above) is present in ‘whitespace-style’
.lines-tail
‘whitespace-line-column’
are highlighted via faces.‘whitespace-line-column’
column.‘lines’
(see above) is not present in ‘whitespace-style’
and if ‘face’
(see above) is present in ‘whitespace-style’
.newline
‘face’
(see above) is present in ‘whitespace-style’
.empty
‘face’
(see above) is present in ‘whitespace-style’
.indentation::tab
‘face’
(see above) is present in ‘whitespace-style’
.indentation::space
‘face’
(see above) is present in ‘whitespace-style’
.indentation
‘indent-tabs-mode’
(which see) is non-nil; otherwise, TABs at beginning of line are visualized via faces.‘face’
(see above) is present in ‘whitespace-style’
.space-after-tab::tab
‘face’
(see above) is present in ‘whitespace-style’
.space-after-tab::space
‘face’
(see above) is present in ‘whitespace-style’
.space-after-tab
‘indent-tabs-mode’
(which see) is non-nil; otherwise, the TABs are visualized via faces.‘face’
(see above) is present in ‘whitespace-style’
.space-before-tab::tab
‘face’
(see above) is present in ‘whitespace-style’
.space-before-tab::space
‘face’
(see above) is present in ‘whitespace-style’
.space-before-tab
‘indent-tabs-mode’
(which see) is non-nil; otherwise, the TABs are visualized via faces.‘face’
(see above) is present in ‘whitespace-style’
.space-mark
tab-mark
newline-mark
Any other value is ignored.
If nil
, don’t visualize TABs, (HARD) SPACEs and NEWLINEs.
There is an evaluation order for some values, if some values are included in ‘whitespace-style’
list. For example, if indentation, indentation::tab
and/or indentation::space
are included in ‘whitespace-style’
list. The evaluation order for these values is:
indentation
indentation::tab
indentation::space
space-after-tab
space-after-tab::tab
space-after-tab::space
space-before-tab
space-before-tab::tab
space-before-tab::space
So, for example, if indentation
and indentation::space
are included in ‘whitespace-style’
list, the indentation
value is evaluated instead of indentation::space
value.
One reason for not visualize spaces via faces (if ‘face’
is not included in ‘whitespace-style’
) is to use exclusively for cleanning up a buffer.
The variable ‘whitespace-action’
specifies which action is taken when a buffer is visited or written.
It’s a list containing some or all of the following values:
nil
cleanup
‘whitespace-cleanup’
and ‘whitespace-cleanup-region’
.report-on-bogus
auto-cleanup
‘whitespace-cleanup’
and ‘whitespace-cleanup-region’
.abort-on-bogus
warn-read-only
‘cleanup’
or ‘auto-cleanup’
is included in ‘whitespace-action’
and the buffer is read-only.Any other value is treated as nil
.
When WhiteSpace is on and a TAB occupies exactly one column, it will display the character ?\xBB
at that column followed by a TAB which goes to the next TAB column. This is due the way the Emacs display engine works.
The situation above is illustrated by the following images:
There are the following possibilities to deal with the situation above:
~/.emacs
file:(setq whitespace-display-mappings '((space-mark ?\ [?\xB7] [?.]) ; space (space-mark ?\xA0 [?\xA4] [?_]) ; hard space (newline-mark ?\n [?\xB6 ?\n] [?$ ?\n]) ; end-of-line ))
whitespace.el
file:(defcustom whitespace-display-mappings '((space-mark ?\ [?\xB7] [?.]) ; space (space-mark ?\xA0 [?\xA4] [?_]) ; hard space (newline-mark ?\n [?\xB6 ?\n] [?$ ?\n]) ; end-of-line ;; WARNING: the mapping below has a problem. ;; When a TAB occupies exactly one column, it will display the character ;; ?\xBB at that column followed by a TAB which goes to the next TAB ;; column. ;; If this is a problem for you, please, comment the line below. ;;(tab-mark ?\t [?\xBB ?\t] [?\\ ?\t]) ; tab ) ......
tab-mark
value from whitespace-style
variable, that is, don’t use TAB display table mapping.Please, put here your comments, suggestions, doubts, improvements, etc.
indentation::space - bug or am I a newbie?
I have indentation::space in my whitespace-style, and expected it would cause leading TAB characters to be highlighted, but it seems to highlight 8 or more leading spaces. I thought that would only be done by indentation::tabs ??? Other style settings seem to work as I expected from the documentation.
------------------------------------------------------------------- Fri, 15 Feb 2008 08:26:15 +0100
Hi,
whitespace doesn't work with emacs-22.1.90, the pretest version, because the c function characterp is only part of the cvs trunk.
Cheers
Martin -- parozusa at web dot de
-------------------------------------------------------------------
Hi. I really like this new and improved Blankmode. But there are a couple things to clarify: 1) How do I completely disable the use of tab characters, and use 4 spaces instead? 2) How do I turn off a single option? For example, how can I disable the 'newline' representation only?
Finally, as well as asking questions, I have one suggestion for the 80-column rule. Try drawing a box around the chars on a line > 79 chars in length. IMHO, that makes the problem more obvious than highlighting the whole line. Example:
(setq my-long-line-face (make-face 'my-long-line-face)) (set-face-foreground 'my-long-line-face "red") (set-face-background 'my-long-line-face "grey3") (set-face-attribute 'my-long-line-face nil :box"green")
then for usage:
(defun c-mode-add-keywords (mode) (font-lock-add-keywords mode '(("\t+" (0 'my-tab-face append)) ("^.\\{79\\}\\(.+\\)$" (1 'my-long-line-face append))))) Thanks, -Tennis
tab-width
variable should be set to 4
.whitespace-style
variable. For example, to disable newline visualization, just eliminate the newline
symbol from whitespace-style
, or call whitespace-toggle-options
. lines-tail
symbol. Please, see whitespace-style
documentation.‘indent-tabs-mode’
and ‘tab-width’
settings from user are honored.Hello, whitespace fail to show newlines on ubuntu with :“GNU Emacs 23.0.60.1 (i486-pc-linux-gnu, GTK+ Version 2.12.0) of 2008-03-07 on iridium, modified by Debian”. All the content of my files is diplayed on one line until i turn off newline with the “n” option. It work fine on my other machine (gentoo) with same files and emacs 23.0.60.1 also. Some thing is missing (not in whitespace) but i can’t figure what. Any idea ? Thank you.
I have been successfully using (fromerly blank-mode.el and now) whitespace.el for a while now on Windows-based GNU Emacs, including 22.3. I appreciate it very much. Thank you!
I just tried using whitespace.el on x86-64 Fedora 9-based GNU Emacs 22.3 & 22.2. All space and tab characters are displayed as a rectangle, which is apparently one tab-width without regard to where the next tab-stop would naturally occur. The perimeter of each rectangle is the foreground color, surrounding the core of the rectangle/tab-width which is the background color. AFAIK, I have it installed and configured and loaded-via-.emacs the same way as I have successfully used on my Windows-based GNU Emacs 22.3. The main installation act that I use is brute force: I replace the entirety of the Rajesh Vaidheeswarran whitespace.el in /usr/local/share/emacs/22.3/lisp that was provided with GNU Emacs with yours and byte-compile to whitespace.elc. (I am having a different problem with blank-mode.el that I have described over on that wikipage.) – Dan’l Miller
~/.emacs
file for Fedora environment:(setq whitespace-display-mappings '((space-mark ?\ [?.]) (newline-mark ?\n [?$ ?\n]) (tab-mark ?\t [?\\ ?\t])))
'.'
and TAB as '\'
.Hello, I am trying to use whitespace-mode under Mac OS X 10.5.6. I am getting the following horrible display while using Carbon Emacs(22.3.1) or Aquamacs(22.3.1):
However, it works well under Emacs.app (23). Therefore, the modification to make it works under Emacs 21 and 22 seem to have some problem which I don’t know how to solve. Regards. – Gustavo Delfino
I’m having the same display issue with whitespace-mode under Mac OS X 10.5.6 using Carbon Emacs. Anyone know how to fix it? – Eddie
Display of centered dot, etc. did not work on my Unicode terminal until I replaced all glyphs in whitespace-display-mappings with their Unicode equivalents: e.g. [?\xB7] becomes [?\u00B7]. Otherwise it tried to send a literal #\xB7 to the terminal, which resulted in a rectangle. – JimUrsetto
(setq whitespace-display-mappings '((space-mark ?\ [?\u00B7]) (newline-mark ?\n [?$ ?\n]) (tab-mark ?\t [?\u00BB ?\t])))
Hi Vinicius,
I tested these settings no Mac OS X 10.4 and 10.6 and they seem ok.
I am using GNU Emacs 23.1.1 and I have the following setting for whitespace-mode.
;; Whitespace mode. (require 'whitespace) (setq whitespace-style '(empty lines-tail tabs tab-mark trailing))
I have issues with the ‘empty’ marker. When am I writing a new source code, whitespace mode marks the ‘empty’ line with yellow background face as I press have an empty line after the last line. That is fine. However, when I start typing the first non-empty line after the prvious empty line, I expect the mark to go away. But, it doesn’t and it stays forever until I go to the beginning of the buffer and edit something. This is a big distraction for me. I am not sure if this is a whitespace mode issue, or I am doing something wrong. Any help is appreciated. Thanks!
I am making Whitespace mode honor the setting for fill-column with:
(add-hook 'whitespace-mode-hook
(lambda () (set (make-local-variable 'whitespace-line-column)
fill-column)))
I don’t think there is a solution to this on a local, per-buffer basis with ‘global-whitespace-mode’
.
‘whitespace-line-column’
documentation.In GNU Emacs 23.2.1, cursor movement in c++-mode with whitespace-mode is sloppy. When I keep pressing the arrow keys, or C-f/C-b; C-p/C-n, the cursor doesn’t move smoothly. It just jumps discretely. However, when I turn off the whitespace-mode, the cursor movement is smooth again. Does anyone else face this issue?
I also experience the problem that cursor movement becomes very unresponsive in cc-mode or LaTeX-mode (auctex) in combination with whitespace-mode. This is so serious that it prevents me from turning whitespace-mode on permanently. After changing to fundamental-mode the cursor becomes responsive again in the same buffer, even though whitespace-mode is still active. I experience this problem with whitespace.el 12.1 in both emacs 23.1 and 23.2. Any ideas?
– Christoph
The slowdown in 12.1 seems to come from the new whitespace-post-command-hook function, which looks like it triggers refontification when moving around in a buffer. I’ve temporarily overriden the hook by throwing
(defun whitespace-post-command-hook() nil)
into my .emacs.
– Jeremy
I’m using global-whitespace-mode on emacs 23.2.1 but it seems to only work when I open files. If I create a new buffer, or manually change my highlighting mode, whitespace mode no longer has any effect. I need to disable it and re-enable it for it to work. I’m also using LineNumbers, and whitespace mode affects the spaces next to the numbers. Is there a way I could disable that?
whitespace-global-modes
variable. For example, if a C++ buffer is opened (C-x C-f test.cpp RET
) the global WhiteSpace highlighting works. NOTE: the buffer test.cpp
in the previous example does not have a file associated.emacs -Q
?linum.el
file). One possible solution for the blank visualization next to the numbers could be to use one of the following settings:(setq linum-format (format "%%05d%c" ?\x200B))
(setq linum-format (format "%%d%c" ?\x200B))
?\x200B
is a zero width space which is not highlighted by WhiteSpace.(add-hook 'linum-before-numbering-hook (lambda () (let ((w (length (number-to-string (count-lines (point-min) (point-max)))))) (setq linum-format `(lambda (line) (propertize (concat (truncate-string-to-width "" (- ,w (length (number-to-string line))) nil ?\x2007) (number-to-string line)) 'face 'linum))))))
I have a file containing zero width space characters (unicode code point U+200B). Any idea how I can get a visual feedback where they are? I tried to customize whitespace-display-mappings of whitespace.el like this:
(defcustom whitespace-display-mappings ‘( (space-mark ?\x200B [?\xB7] [?.]) …
But I still don’t see it. Normal spaces are displayed as intented by whitespace.el.
– Flo
whitespace.el
source file. So, you should terminate the current Emacs session (C-x C-c
), start a new Emacs session, open the modified whitespace.el
source file, evaluate the buffer (M-x eval-buffer RET
), open the file which contains ?\x200B
characters or insert these characters in a file, and, finally, turn on WhiteSpace (M-x whitespace-mode RET
). The ?\x200B
characters should be marked.setq
instead of defcustom
, reevaluate the region containing setq
(M-x eval-region RET
), go to the file containing ?\x200B
characters, turn off WhiteSpace (because it is using the old whitespace-display-mappings
value), and turn on WhiteSpace (now use the new whitespace-display-mappings
value).Hi! I have what I think is a bug with whitespace-newline-mode in Emacs 24. When I start it with -Q and then do M-x whitespace-newline-mode twice, it does not get disabled. I discovered this because I tried to duplicate the source of whitespace-newline-mode to make a whitespace-longline-mode mode, and originally thought it was my own bug. It appears it isn’t. – Sarah
Yup, looks like a bug. Please file a report: ‘M-x report-emacs-bug’
. – DrewAdams
If you also use Desktop.el to save and restore your sessions, then a word of warning about using global-whitespace-mode: Because the enabled modes for each buffer are saved in the desktop file and those modes are re-enabled at restore time, global-whitespace-mode is evaluated for each buffer which is restored; and because running global-whitespace-mode is an expensive operation which processes every current buffer, this becomes an o(n^2) operation over the course of the desktop restore, and absolutely kills the performance for large sessions.
whitespace.el should probably provide some custom support for the desktop library. See the help for the desktop-minor-mode-handlers variable, and the commentary in desktop.el.
One option would be to enable whitespace-mode for each buffer instead of global-whitespace-mode. Another would be to just register that global-whitespace-mode was enabled, and use desktop-after-read-hook to enable it once only. I would think the latter is probably the preferable approach.
CategoryCode, CategoryModes, CategoryFaces, ProgrammingModes