71

How can I make Emacs show blank spaces (like a space, a tab, a line-jump, etc). Many other editors like Kate and Eclipse have this feature and I find it very useful to see when code is indent-broken because of mix of spaces and tabs (particularly Python).

scunliffe
  • 62,582
  • 25
  • 126
  • 161
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

3 Answers3

72

WhiteSpace mode is an Emacs minor mode for visualizing all white space characters in the current buffer. It can be activated with M-x whitespace-mode.

Here is a screenshot of WhiteSpace in action taken directly from the Emacs wiki,

whitespace mode in action

Note: WhiteSpaceMode has now replaced BlankMode

Community
  • 1
  • 1
Justin Tanner
  • 14,062
  • 17
  • 82
  • 103
  • 23
    Thanks for the screenshot and the link, but you forgot to actually tell us how to turn that feature on. (It's `M-x whitespace-mode` btw.) – bug Aug 15 '13 at 15:54
5

All the possible settings to do that seem to be summarized here (blank-mode) and here and here (ShowWhiteSpace)

also:

(if (>= emacs-major-version 22)
  (progn
    ;; Mode to use with Emacs 22
    ;; http://emacswiki.org/cgi-bin/wiki/BlankMode
    (require 'blank-mode)
    ;; Mode not active by default: let's activate it
    (global-blank-mode t)
    ;; ... activate it when text mode where color syntax is not active by default
    (add-hook 'text-mode-hook 'blank-mode-on)
    ;; All invisible chars are shown, except newline char.
    (setq blank-chars '(tabs spaces trailing lines space-before-tab))
    ;; Show only for one color, no mark inserted
    (setq blank-style '(color))
    ;; Use for normal space (not shown)
    (set-face-background 'blank-space-face nil)
    (set-face-foreground 'blank-space-face "black")
    ;; used for non breakable space
    (set-face-background 'blank-hspace-face "PaleGreen")
    (set-face-foreground 'blank-hspace-face "black")
    ;; Used for spaces left of a tab
    (set-face-background 'blank-space-before-tab-face "orange")
    (set-face-foreground 'blank-space-before-tab-face "black")
    ;; Used for tab
    (set-face-background 'blank-tab-face "lemonchiffon")
    (set-face-foreground 'blank-tab-face "black")
    ;; used for extra space at the end of a line
    (set-face-background 'blank-trailing-face "gold")
    (set-face-foreground 'blank-trailing-face "black")
    ;; Used for line too long
    (set-face-background 'blank-line-face "snow2")
    (set-face-foreground 'blank-line-face "black")
  )
  (progn
    ;; For older Emacs prior to version 22.
    ;; http://www.emacswiki.org/cgi-bin/wiki/show-wspace.el
    (require 'show-wspace)
    (add-hook 'font-lock-mode-hook 'show-ws-highlight-tabs)
    (add-hook 'font-lock-mode-hook 'show-ws-highlight-hard-spaces)
    (add-hook 'font-lock-mode-hook 'show-ws-highlight-trailing-whitespace)
  )
)
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
4

indent-broken? - never use tabs in your code - disk space is cheap these days.

Put (setq-default indent-tabs-mode nil) in your .emacs file. Get used to typing C-x h M-x untabify to untabify the entire buffer. To search for tabs type C-s C-i. If you have obscure control characters in your buffers you can see them with M-x hexl-mode.

Also C-x h M-x indent-region will indent the entire buffer. Some modes like vhdl-mode have a beautify region command.

user37248
  • 257
  • 2
  • 4
  • 10
    Different developers on my team have different preferences for tabwidth. Setting everything to spaces screws up the rest of the team. Thanks for playing, though. – warfangle Aug 27 '10 at 16:03
  • Just for the record: it should be `C-h f untabify` instead of `C-x h M-x untabify` – To1ne Feb 07 '11 at 21:04
  • 2
    Emacs is good for other kinds of files (like, say, tab-delimited data files), not just code! Neat, huh? – Anne Aug 09 '11 at 15:59
  • 3
    @To1ne `C-h f untabify`: help about `untabify`. `C-x h M-x untabify`: mark the buffer, then run `untabify`. – Jerome Baum Jan 19 '12 at 17:18