1

How can I deal with the colored outputs of shell commands when inputing them with :read?

As an example, I have a self-defined git log command, and I would like to see it in a split window. That works well enough using

:new | read !git log-command

Unfortunately, the command outputs has some colours. And thus the result looks like

  • ^[[33mac74ef4^[[m 2018-10-22 [me, N] - some text

I guess I could just use as :substitute command to clean it up afterwards, or maybe pipe it through sed.

But I wanted to know if there was a better way to handle it?

As a minimum, I would like to remove those ^[[m characters. But even better would be to recognise them and adapt it to color the buffer accordingly.

clem steredenn
  • 441
  • 4
  • 15

1 Answers1

2

Best way is to avoid the output of these ANSI escape codes. Some commands to this automatically when the output is not to a terminal, others have a command-line argument, e.g. --no-color or --color=never (even moderately outdated Git versions understand this). Some also react to the terminal type, so prepending TERM=dumb git ... might work, too (it does for me).

Alternatively, you can strip off the codes, e.g. with sed:

git ... | sed 's#\x1b\[[0-9;]*[mK]##g'

You can do the same inside Vim, too:

%substitute#\e\[[0-9;]*[mK]##g

Finally, there are plugins which translate (but not remove) the ANSI escape codes to Vim colors (via syntax highlighting and concealing of the sequences):

Ingo Karkat
  • 17,819
  • 1
  • 45
  • 61