12

I'm using the *compilation* buffer to build a C++ application on Centos 5 and Centos 6. The buffer shows the build and includes references to files that are part of the build. However, I'm getting control characters like ^[ appear in the output and it is hard to read and emacs is getting corrupted filenames when you click one in the output. I think these characters are some kind of coloring or other annotation on the compiler output but I don't know where they are coming from. Here is sample output:

cc -c -g -I../machind -I. -DRELEASE_VERSION -g -O2 -m64 -D__BITS64 -pipe -DARCH_64 -DARCH_INTEL -DARCH_X86 -DARCH_X86_64 -DOS_LINUX -pthread -DLM_INTERNAL -DFLEXLM_KITBUILD -DFLEX_STATIC -DRELEASE_VERSION -DGPLATFORM=\\"x64_lsb\\" -DLINUX -DLSB3_0 -D__FD_SETSIZE=65535 -DGLIBC -DLINUX64 -DAMD64 -DREDHAT -DREL -DRHLINUX64 -DPLATFORM_AMD64_RHLINUX   -DNO_ACTIVATION_SUPPORT ../machind/lmcrypt.c
^[[01m^[[K../machind/lmcrypt.c:^[[m^[[K In function  ^[[01m^[[Kmain^[[m^[[K :
^[[01m^[[K../machind/lmcrypt.c:156:47:^[[m^[[K ^[[01;35m^[[Kwarning: ^[[m^[[Kcast to pointer from integer of different size [-Wint-to-pointer-cast]
     lc_set_attr(lm_job, LM_A_MAX_LICENSE_LEN, (LM_A_VAL_TYPE)max);
^[[01;32m^[[K                                               ^^[[m^[[K

How can I get rid of the non-text output in my compilation buffer?

WilliamKF
  • 363
  • 5
  • 15
  • 2
    Those look like VT100 Color and formatting escapes. You might try disabling colors by passing `-fdiagnostics-color=never` to `gcc` (assuming that `gcc` is your `cc`) – PythonNut Feb 10 '15 at 21:24
  • As the `ansi-color-apply-on-region` could be slow, I have written up this snippet few weeks back: [de-ansi.el](https://github.com/kaushalmodi/.emacs.d/blob/master/de-ansi.el). I did not intend it to be a package and so it has dependencies on the key-chords package and on my temporary minor mode [temp-mode.el](https://github.com/kaushalmodi/.emacs.d/blob/master/temp-mode.el). You also need to be on emacs 24.4 or later for the file-notify feature. – Kaushal Modi Feb 10 '15 at 21:49

1 Answers1

22

These are indeed escape sequences which the terminal should interpret as orders to change the text color. Normally they shouldn't be used when the compiler is invoked from Emacs (the terminal type should be set to dumb, which should cause the compiler to refrain from using any escape sequence). There may be something wrong in your configuration that causes colors to be used when they shouldn't. But you can make Emacs recognize the escape sequences with the ansi-color package.

Something like this should make compilation buffers handle ANSI escape sequences instead of displaying them as raw strings.

(require 'ansi-color)
(defun my/ansi-colorize-buffer ()
  (let ((buffer-read-only nil))
    (ansi-color-apply-on-region (point-min) (point-max))))
(add-hook 'compilation-filter-hook 'my/ansi-colorize-buffer)
François Févotte
  • 5,917
  • 1
  • 24
  • 37
  • 2
    Extremely useful. Works in Emacs 26.1. – Lalylulelo Nov 30 '18 at 10:36
  • 1
    This re-colors the whole buffer each time it runs (and resulted in coloring all text to the last foreground color for me). This hook sets "compilation-filter-start" to the start of the region inserted with point moved to the end. So I would use those instead of (point-min) and (point-max). – gct Jan 28 '19 at 17:26