I want to view (and/or edit) colorized text in emacs, such as is output by ls -l --color=always, tree..., or whatever.

The text I refer to exists in a file. When I open that file in emacs, I want to be able to see it colorized, or toggle it to show ANSI (SGR) escape sequences.

ansi-color.el seems be be what I need, but I haven't been able to get it to do any colorizing of the ANSi escape sequences, but I do see blue-bold for individual contol bytes (eg. ^A and ^[)... I'm not sure if that is a feature of ansi-color, but I think it is.

According to the ansi-color.el comments, it can work with strings and regions, but even that doesn't seem to work.. For example the function ansi-color-apply-on-region is not recognized by M-x. It says, "No match"

I've added (require 'ansi-color) to my .emacs file and even (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on). I get no error or warning when emacs starts, so I'm stuck.

How can I get the standard functions to work, and can it be made to automatically apply when opening a file whose name is suffixed with .col?


I think the piece you're missing is the interactive form. It's how Emacs distinguishes between a function designed to be called by other functions, and a function designed to be called directly by the user. See the Emacs Lisp Intro node

Now if you read the definition of ansi-color-apply-on-region, you'll see that it's not designed for interactive use. "ansi-color" is designed to filter comint output. However it's easy to make an interactive wrapper for it.

(defun ansi-color-apply-on-region-int (beg end)
  "interactive version of func"
  (interactive "r")
  (ansi-color-apply-on-region beg end))

The next bit is you want to turn on ansi colors for the .col extension. You can add a hook function to whatever major-mode you want use to edit those files. The function would be run whenever you turn on the major-mode, so you will have to add a check for the proper file suffix.

Alternatively you can hack a quick derived mode based on "fundamental" mode.

(define-derived-mode fundamental-ansi-mode fundamental-mode "fundamental ansi"
  "Fundamental mode that understands ansi colors."
  (require 'ansi-color)
  (ansi-color-apply-on-region (point-min) (point-max)))

and associate it with that extension.

(setq auto-mode-alist
      (cons '("\\.col\\'" . fundamental-ansi-mode) auto-mode-alist))
  • Fantastic.. as if by magic! Well explained, and your Make a Function Interactive link helped a lot. I'm starting to see the general structure of Emacs. Thanks... – Peter.O Aug 26 '11 at 21:31
  • I wonder what I am doing wrong. I added all text you have above in my .emacs file but the text that I highlight doesn't seems to get colored after invoking ansi-color-apply-on-region-int. – SFbay007 Dec 15 '15 at 1:48

The following solution allow to read (and not save) files containing ANSI color sequences. The filenames must have a .txt extension.

Put the library tty-format in ~/.emacs.d/site-lisp/, then add these lines to your ~/.emacs init file.

(add-to-list 'load-path "~/.emacs.d/site-lisp/")              
(require 'tty-format)
(add-hook 'find-file-hooks 'tty-format-guess)
  • This is a useful option too. It utilizes ansi-color and takes it one step further to, as your link says, "...The aim of tty-format.el is just to make *such files viewable."* .. "such files" being those containing ANSI SGR escape sequences and backspacing.. Thanks.. – Peter.O Aug 26 '11 at 18:31
  • 1
    For what it's worth.. This method vs the fully editable version as posted by event_jr has faster (~25%) continuous paging speed with significanlty better (much! better) refresh frequency.. The test file had 45,000 lines. For the editable version the screen refresh ground to halt at about 75% of the way through the file, but the scroll bar was still scrolling.. I suspect this is because of some overhead of some point/mark undo buffer in the editable version... Both methods have their advantages... (by "continuous", I mean keeping the Page Up/Down key pressed continuously) – Peter.O Aug 26 '11 at 20:11
  • PS.. tty-format pages as smoothly and as fast as less does in the terminal (the times were within a second or two of each other; approx. 50 seconds from top-botton, and bottom-top). – Peter.O Aug 26 '11 at 20:53
  • I wasn't aware of tty-format. It seems to be superior to my wrapper function or 3 line derived-mode. :) Emacs has a hard time dealing with a lot of overlays, which is how ansi-color does the colorization. There is opportunity for further optimization here, because we don't need to colorize the whole buffer, just the part being displayed. – event_jr Aug 29 '11 at 10:09

Thank you so much @enzotib for your solution !

Just as an addition to your solution, here is a way to make it explicitly callable by the user:

;; M-x display-ansi-colors to explicitly decode ANSI color escape sequences                                                                                                                                        
(defun display-ansi-colors ()
  (format-decode-buffer 'ansi-colors))

and automatic for files other than just *.txt or README:

;; decode ANSI color escape sequences for .log files                                                                                                                                                               
(add-to-list 'auto-mode-alist '("\\.log\\'" . display-ansi-colors))

This way you can replace your escape sequences with colors without altering the original file, unlike solutions using ansi-color-apply-on-region.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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