How do I make Emacs show blank spaces?

2019-03-09 05:53发布

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).

3条回答
Animai°情兽
2楼-- · 2019-03-09 06:26

WhiteSpace mode is an Emacs minor mode for visualizing all white space characters in the current buffer.

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

whitespace mode in action

Note: WhiteSpaceMode has now replaced BlankMode

查看更多
ら.Afraid
3楼-- · 2019-03-09 06:33

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.

查看更多
ら.Afraid
4楼-- · 2019-03-09 06:39

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)
  )
)
查看更多
登录 后发表回答