3

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.

1
  • your vim display ^[[0m as literal text or as key sequence? Commented Aug 7, 2013 at 12:02

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Learn one thing a day and keep the boredom away. Thanks Ingo (and Vim).
3

Just use <C-v><Esc> to input the ^[ character in the following command:

substitute(my_variable, '^[[\d+m', '', 'g')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.