As of 2020, 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`:
<!-- language: lang-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][1].)
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 tell 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:
<!-- language: lang-el -->
(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))
[1]: https://github.com/atomontage/xterm-color#usage
As of 2020, 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`:
<!-- language: lang-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][1].)
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 tell 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:
<!-- language: lang-el -->
(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))
[1]: https://github.com/atomontage/xterm-color#usage