29

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?

27

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:

:%s/<1b>\[[0-9;]*m//g

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

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.

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.