Endless Parentheses

Ramblings on productivity and technical subjects.

profile for Malabarba on Stack Exchange

ANSI-colors in the compilation buffer output

Countless build tools and shell scripts use ANSI escape codes to colorize their output. This provides impressive improvements to readability when running from a terminal that supports them, but tends to cause a catastrophic mess anywhere else. Emacs’ compilation buffer is one such place. It doesn’t support ANSI colors by default, but that’s very easy to fix.

Emacs already has a library for interpreting ANSI escape. All we need is to hook it onto compilation-mode.

(require 'ansi-color)
(defun endless/colorize-compilation ()
  "Colorize from `compilation-filter-start' to `point'."
  (let ((inhibit-read-only t))
    (ansi-color-apply-on-region
     compilation-filter-start (point))))

(add-hook 'compilation-filter-hook
          #'endless/colorize-compilation)
comments powered by Disqus