51

I have a script that generates a log file with ANSI color codes in them like so:

[2012-05-14 18:00:02] ^[[0mINF: -- Starting update script --^[[0m
[2012-05-14 18:00:29] ^[[91mERR: Improper date, entry being ignored.^[[0m

Is there any way to get Vim to understand these color codes?

Thanks for any help.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Blake
  • 1,691
  • 2
  • 15
  • 23
  • Possible cross site duplicate of: http://superuser.com/questions/358409/how-can-i-tell-vim-to-show-ansi-escape-codes-properly – Ciro Santilli OurBigBook.com Mar 23 '14 at 14:24
  • See the answer at https://vi.stackexchange.com/a/20496/3324: *“If you have a sufficiently modern vim that has the +terminal feature, you can do `:term cat somefile` and you'll get a buffer with all the terminal codes interpreted. This might work better on large files than e.g. Colorizer, which made my vim unusably slow when I let it loose on a 6000-line file.”* – sideshowbarker Dec 07 '19 at 17:08

4 Answers4

60

I'm not sure about vim, but if you're just viewing a log file (ie you don't need to be able to edit) you could use less:

less -R
John Carter
  • 53,924
  • 26
  • 111
  • 144
45

Use Charles Campbell's (Dr Chip) AnsiEsc plugin:

http://www.vim.org/scripts/script.php?script_id=302

Before:

before

:AnsiEsc<CR>

after

guns
  • 10,550
  • 3
  • 39
  • 36
  • 1
    I had to change `91m` and `93m` to `31m` and `33m`, respectively, but it worked beautifully otherwise. Thanks. – Blake May 15 '12 at 01:40
  • Change them where? (the vba file is pretty hairy...) I'm still getting some escape sequences instead of colors... – fakeleft May 01 '15 at 10:08
  • Nevermind. The colours are applied, but I think I need to recompile vim with the 'conceal' feature to hide the escape sequences. – fakeleft May 01 '15 at 10:23
  • The link [does not seem to be working](http://meta.stackexchange.com/a/8241/311650). Is there a chance for a script to be included in the answer? – Konrad Jul 13 '16 at 07:09
  • 1
    could not get this to work with vim 7.4 -- any chance there is another solution? – serup Aug 19 '16 at 12:05
  • The [Vim.org scripts](http://www.vim.org/scripts/script.php?script_id=302) are all [mirrored on github](https://github.com/vim-scripts). This script would be https://github.com/vim-scripts/AnsiEsc.vim Both sites have code that is 6y old, so your source doesn't matter as much, but the Vim.org version lets you install it as a vimball (.vba.gz), which is easier. This works for me on Vim 8.0. – Adam Katz Dec 03 '16 at 00:06
  • 5
    There is a newer fork of Charles Campbell's (Dr Chip) AnsiEsc by Alex Efros: [Improved AnsiEsc](http://www.vim.org/scripts/script.php?script_id=4979) ([github](https://github.com/powerman/vim-plugin-AnsiEsc)). – dexteritas Dec 22 '17 at 11:55
  • Default `vim` on macOS is not compiled with `conceal` feature, you need to install complete `vim` with `brew install vim` – Woody Huang Sep 25 '19 at 08:50
6

Since the link in the accepted answer doesn't appear to be working, I found this link: https://github.com/vim-scripts/AnsiEsc.vim

ndjensen
  • 129
  • 1
  • 3
6

AnsiEsc works with basic ANSI color codes, but not other codes. The problem is that there are lots of ANSI_escape_code s beyond the basic \033[31m red \033[0m; what is AnsiEsc, what is any program, to do with \033[... unrecognized ? It seems that AnsiEsc just leaves such alone, but differently in Vim and mvim.

Better doc on AnsiEsc would be welcome; links to a small ANSI escape code parser in python would be welcome.

A test case for colored output from gnu ls and grep, with TERM xterm-256color and macosx Terminal version 2.3:

#!/bin/bash
# test https://en.wikipedia.org/wiki/ANSI_escape_code#Colors from gnu ls and grep

mkdir bluedir  2> /dev/null
ls -d --color=always  bluedir  > ls.out

echo red-hot | grep --color=always  red  > grep.out

od -c ls.out
# 0000000  033   [   0   m 033   [   3   4   m   b   l   u   e   d   i   r
# 0000020  033   [   0   m  \n                                            
# Vim 7.3 :AnsiEsc "bluedir" blue, \033[0m as is
# mvim 8.0 :AnsiEsc "bluedir" works

od -c grep.out
# 0000000  033   [   0   1   ;   3   1   m 033   [   K   r   e   d 033   [
# 0000020    m 033   [   K   -   h   o   t  \n                            
# both Vim and mvim display as is

See also the lovely bash script colors256 .

denis
  • 21,378
  • 10
  • 65
  • 88
  • THIS! I read your comment and kept seeing that my 'basic' codes weren't being translated by the script. The problem I had was I considering the 3x and the 9x codes as basic (regular & bright). AnsiEsc ONLY deals with the 3x codes. – Marcel Wilson Jul 03 '19 at 16:39