Replace ANSI color code from string in vimscript

vim

Solution

The special atom `\e` matches the `<Esc>` = `^[`:

substitute(my_variable, '\e\[[0-9;]\+[mK]', '', 'g')

You could also use `\%d27` (`<Esc>` is decimal 27) or `\%x1b` (hexadecimal). The pattern should match (most) ANSI escape sequences.

Problem

I have a string variable in vimscript that contains some ANSI escape characters used for highlighting purposes. The string looks like, ``` ^[[32m MyStringBody ^[[0m ``` I've put the escape code literally as vim displays it, it is the escape sequence - Ctrl-v-[. I want to replace all occurences of such escape characters with a `substitute` command. ``` substitute(my_variable, pattern, '', 'g') ``` Can someone help me with a regex pattern that would remove these escape characters. Thanks.

Original source