ANSI Color Codes in VIM

前端 未结 4 1210
小蘑菇
小蘑菇 2020-12-23 16:38

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-1         


        
相关标签:
4条回答
  • 2020-12-23 17:00

    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 .

    0 讨论(0)
  • 2020-12-23 17:02

    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
    
    0 讨论(0)
  • 2020-12-23 17:20

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

    0 讨论(0)
  • 2020-12-23 17:23

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

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

    Before:

    before

    :AnsiEsc<CR>

    after

    0 讨论(0)
提交回复
热议问题