When I run the test feature of Rust in Emacs compilation mode, with ansi-color mode loaded, somehow the character ^[(B is always displayed like below:

enter image description here

It looks quite annoying.

Does anyone know how to remove these characters from the compilation buffer?

  • Don't put an answer in the question. Post it separately as an answer. If you want, you can accept your own answer, e.g., if it answers your question better or more completely than other answers.
    – Drew
    Dec 1, 2022 at 16:26
  • @Drew: Thanks! I just posted an answer instead
    – Trung Ta
    Dec 2, 2022 at 2:26

2 Answers 2


Make sure that your shell init files (~/.bashrc or similar) are not setting the TERM variable to anything. TERM will then keep its default value (which is probably dumb while M-x compile is running things), and your compiler will know that it is not supposed to print any escape sequences. If it still does, file a bug report on the compiler. Or in this case perhaps your test suite.

  • Thanks a lot for the tip! Previously, I set TERM to xterm-256color. Now, I configure it to (setenv "TERM" "xterm-color") and the compilation mode works just fine!
    – Trung Ta
    Dec 1, 2022 at 3:20
  • 1
    You shouldn’t set it to anything at all. A program that runs other programs (such as Emacs running your compiler) should be free to set it to the appropriate value.
    – db48x
    Dec 1, 2022 at 3:23
  • Thanks for the advice. Using the default dumb shell, the color will not be displayed. I want to see the color too, so I wrote a quick hack only the change the TERM variable during the compilation (I updated this part in my question too)
    – Trung Ta
    Dec 1, 2022 at 4:01

Following @db48x's answer, I wrote a quick function to advise M-x compile to use TERM=xterm-color during its session.

  ;; Advise compile to set TERM variable
  (defun tddsg--advice-compile (func &rest args)
    (let ((saved-term-env (getenv "TERM")))
      ;; Use xterm-color so that the compilation mode can display color properly
      (setenv "TERM" "xterm-color")
      (apply func args)
      (setenv "TERM" saved-term-env)))
  (advice-add 'compile :around #'tddsg--advice-compile)

Here is the result:

enter image description here

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.