45

When working in Emacs, I use the compile command (F12 by default) to run programs. When I run Cucumber in Emacs, Cucumber spits out ANSI colors that the Emacs compilation mode doesn't interpret. The result is ugly and hard to read. Here's a snippet of the *compilation* buffer showing the ugly:

^[[31m(::) failed steps (::)^[[0m

The command I'm using:

( cd ~/lab/rails/todolist && rake cucumber:all )

Versions:

  • Emacs 23.1
  • Cucumber 0.8.3
  • Cucumber-rails 0.3.2

The world would be sunshine and birds singing if I could:

  • Get Emacs to interpret ANSI color codes in its compilation buffer, or
  • Get Cucumber to stop spitting out ANSI color codes

Any ideas?

3 Answers 3

77

I use this to turn on ansi color interpretation in my compilation buffer:

(require 'ansi-color)
(defun colorize-compilation-buffer ()
  (let ((inhibit-read-only t))
    (ansi-color-apply-on-region (point-min) (point-max))))
(add-hook 'compilation-filter-hook 'colorize-compilation-buffer)
5
  • Exactly what I was looking for! Unfortunately, in my environment (cygwin-xemacs) it kroaked on (buffer-read-only #<buffer "compilation">) with "Error in process filter", which seems very strange. Any ideas?
    – thoni56
    Jul 25, 2010 at 12:01
  • This worked great for me, too. I also added (linum-mode 0) (and, at first attempt (line-number-mode 0), which didn't work, but I left in anyway, and might work for someone else), after (toggle-read-only), which makes me even that much happier (a bunch of lines that were just barely wrapping now don't).
    – lindes
    Mar 5, 2011 at 10:04
  • I decided to add some colour to my makefiles output and hit this issue in emacs. Great solution! Feb 28, 2014 at 11:30
  • 11
    For modern Emacsen, one should let-bind inhibit-read-only to t, instead of calling toggle-read-only.
    – user355252
    Mar 7, 2014 at 10:26
  • Any way to make this work even as new text is being added? I tested that on the result of ack, but the hook seems to be run before the content fills the buffer, so the content is no interpreted.
    – Gauthier
    May 19, 2016 at 12:14
22

I improve code so it doesn't pollute M-x grep like commands and more efficient:

(ignore-errors
  (require 'ansi-color)
  (defun my-colorize-compilation-buffer ()
    (when (eq major-mode 'compilation-mode)
      (ansi-color-apply-on-region compilation-filter-start (point-max))))
  (add-hook 'compilation-filter-hook 'my-colorize-compilation-buffer))
2
  • Note I test this code only with Emacs 24.x, it may not work in version 23.x or less...
    – gavenkoa
    Jan 9, 2014 at 10:16
  • This works fine, why (add-hook 'compilation-filter-hook 'ansi-color-for-comint-mode-on) does not? Dec 22, 2016 at 18:05
7

As of 2023, the most modern way appears to be the xterm-color Emacs package.

  1. Execute M-x package-install with xterm-color.

  2. Add the following lines to your ~/.emacs or ~/.emacs.d/init.el:

(require 'xterm-color)
(setq compilation-environment '("TERM=xterm-256color"))
(defun my/advice-compilation-filter (f proc string)
  (funcall f proc (xterm-color-filter string)))
(advice-add 'compilation-filter :around #'my/advice-compilation-filter)

(See xterm-color documentation.)

Note that this will provide an error message if xterm-color was not installed properly. This is strongly advised, because on an incomplete Emacs installation it will clearly explain to you what's wrong, instead of leaving you wondering why the colors don't work.

However, if you really prefer to not being informed if xterm-color is missing, use instead:

(when (require 'ansi-color nil t)
  (setq compilation-environment '("TERM=xterm-256color"))
  (defun my/advice-compilation-filter (f proc string)
    (funcall f proc (xterm-color-filter string)))
  (advice-add 'compilation-filter :around #'my/advice-compilation-filter))
4
  • This worked well for coloring my pytest compilation commands, but broke my ag-mode - it lost interactivity and slowed down.
    – simno
    Dec 30, 2020 at 15:02
  • @simno I'd propose to file a bug report: github.com/atomontage/xterm-color/issues
    – vog
    Dec 30, 2020 at 20:06
  • There's one already for rg.el and ag.el packages: github.com/dajva/rg.el/issues/65. Both packages rely on matching terminal color escapes and do not provide option to ignore highlighting. I'd switch off my/advice-compilation-filter for specifically ag-mode, but I'll have to figure it out.
    – simno
    Jan 2, 2021 at 15:35
  • This didn't work for sbt color output in the compilation buffer for me. The escape codes still printed.
    – Jack Viers
    Apr 26, 2022 at 16:14

Your Answer

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

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