I'd like to disable whitespace-mode while in evil insert state. The following hooks work, but require refreshing all font colors with (font-lock-fontify-buffer), and even then- sometimes it fails to refresh properly to show trailing whitespace.

While this example uses evil-mode, similar logic applies to any other hook.

(defun i42/enter-insert-mode ()
  (whitespace-mode -1)
(defun i42/exit-insert-mode ()
  (whitespace-mode 1)
(add-hook 'evil-insert-state-entry-hook 'i42/enter-insert-mode)
(add-hook 'evil-insert-state-exit-hook  'i42/exit-insert-mode)

Is there a better way to toggle white space mode?

  • 1
    Just out of curiosity, why don't you want it on in insert state?
    – Dan
    Dec 7, 2016 at 17:30
  • Does redraw-display or redisplay fix this issue? These functions should redraw the display and possibly fix your problem.
    – user12563
    Dec 7, 2016 at 17:38
  • If you manually disable whitespace-mode without using evil, does the screen refresh properly without any extra steps?
    – lawlist
    Dec 7, 2016 at 18:59
  • @Dan, because I have a bright color set for trailing space which is distracting when entering text. Checked redraw-display, redraw & manually toggling whitespace-mode - none of them properly refresh the background color of trailing space, found a workaround, temporarily change the theme color - will post as a possible answer.
    – ideasman42
    Dec 7, 2016 at 20:26

1 Answer 1


One workaround I'd found is to temporarily change the face background color.

;; Don't show trailing space in insert mode.
(defvar i42/whitespace-trailing-bg (face-attribute 'whitespace-trailing :background))
(defun i42/enter-insert-mode ()
  (set-face-attribute 'whitespace-trailing nil :background nil :foreground nil))
(defun i42/exit-insert-mode ()
  (set-face-attribute 'whitespace-trailing nil :background i42/whitespace-trailing-bg :foreground nil))

(add-hook 'evil-insert-state-entry-hook 'i42/enter-insert-mode)
(add-hook 'evil-insert-state-exit-hook  'i42/exit-insert-mode)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.