Many CLI programs output nicely-highlighted text. While most of them check if the output is going to a file, some include the terminal escape codes. I like the output with the escape codes, but viewing these files in Vim is painful.

Can Vim interpret these escape codes into the expected colors? If not, can Vim be set to ignore these escape codes?


There are various plugins which allow you to view ANSI colours through escape codes:

If you want to remove all escape codes, you could use:


Note: <1b> is not literal text, it is the escape character, use Ctrl+v followed by Esc to insert it (it may also show up as ^[, depending on your display setting).

Or you can remove them with sed; for example:

$ sed 's|\x1b\[[;0-9]*m||g' somefile | vi -
| improve this answer | |
  • You can also use \e in vim's regular expressions and vimscript strings instead of typing <C-v><Esc>. – rampion Feb 7 at 18:31

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.

| improve this answer | |
  • Note: Terminal buffers are by default unmodifiable, and any modification will make the colours disappear. – preferred_anon Mar 5 at 17:22

Building on the previous answer, with this bash function you can open vim and the required file will be displayed with colours instead of codes in a new window "a little bit later" (depending on the size of $1).

ansivim ()                               
    vim -c ":term ++hidden ++open cat $1"


ansivim build.log
| improve this answer | |

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.