9

Using VI/Vim how can I remove color coding? I found this: Removing ANSI Color Codes from a Stream

answer from @adam-katz @olibre

cat colored.logs | sed 's/\x1b\[[0-9;]*m//g'

I thought I could translate this to command line in vim, but it fails with no match found

>vim colored.logs
: s/\x1b\[[0-9;]*m//g

3 Answers3

12

The \x1b escape sequence (which works in sed, also C source code and shells like bash) corresponds to an ESC (like the ESC key.)

See the manpage for ascii(7):

Oct   Dec  Hex  Char
------------------------------
033   27   1B   ESC (escape)

The C-style \xNN sequence recognized by sed takes the hexadecimal representation of the character, which in the case of ESC is 1B, so that's where \x1b comes from.

Vim doesn't recognize \xNN C-style sequences, but it actually does recognize similar sequences with a slightly different syntax: \%xNN, so one way you can translate that ESC sequence to \%x1b. The following works on Vim:

:s/\%x1b\[[0-9;]*m//g

But you can simplify it. Since ESC is a somewhat frequently used symbol, there is a special shortcut escape sequence for it: \e. So this will also work:

:s/\e\[[0-9;]*m//g

Finally, as you noticed, it's possible to insert a literal ESC character in your match.

In Vim, it's usually possible to insert a literal character by preceding it with the Ctrl+V combination.

So using Ctrl+V, ESC is one way to enter this sequence. It will be displayed as ^[, but usually in a different color, to indicate it's representing a single character. Also, when moving the cursor around, it will only stop on top of the ^ and not the [, to be consistent with that being a single character.

You might have noticed that using Ctrl+[ after the Ctrl+V is also possible, that's because Ctrl+[ is the same as ESC. In fact wherever you press the ESC key (such as to exit insert mode), you could equivalently press Ctrl+[ instead, that would work. So much so, that the character produced by a literal ESC, ^[, is actually a representation of Ctrl+[ (the ^ symbol is often used for Ctrl, for instance try Ctrl+V, Ctrl+A in insert mode to see a ^A for a literal Ctrl-A character.)

In general, using the backslash sequences (such as \e or slightly inferior \%x1b) should be better, since it's easier to store those in text files (such as your .vimrc), rather than literal special characters such as the literal ESC.

filbranden
  • 1,701
  • 9
  • 15
2

from search-and-replace-control-characters vi I found that I can use CTRL-V followed by CTRL-[ to substitute for

\x1b

which leads to :

: %s/^[\[[0-9;]*m//gc

this successfully strips ^[[35m, ^[[39m, ^[[37m ... etc.

0

I stumbled across a similar problem where terraform show was including ANSI color codes and when it came to redirecting the output into vim it would be completely messed up (e.g. it had a bunch of ^[[0m and ^[[1m).

My solution was to use the trick mentioned by @mancocapac (i.e. Ctrl+v then Ctrl+[ to create the initial ^[ followed by a pattern that would cause all variations of the ANSI codes to be matched).

I had originally tried using just a single substitution command, which would match the given pattern but when entering / to start the replacement portion (e.g. :%s/^[[.m/<replacement>/g) the operation would fail to match the pattern and so nothing would be replaced.

I worked around this by first searching the pattern by itself, then doing a substitution but leaving the pattern section blank (as vim will use the last matched pattern automagically)...

  1. /^[[.m
  2. :%s///g

This worked for me.