1

Have text file with contents like:

^[[31morigin/some-branch^[[m
^[[31morigin/some-other-branch^[[m

These ^[ sequences are highlighted and treated as one character as they are ascii escape sequence. How do I use regex/replace on them?

For example I can do :%s/origin/potato/g to replace all origin with potato, but how do I replace the escaped sequence?

I tried :%s/<1b>\[[0-9;]*m//g but it gave E486: Pattern not found: <1b>\[[0-9;]*m

xxjjnn
  • 123
  • 5
  • You should also be able to accomplish this by using :h i_CTRL-V. For instance, you can demonstrate by typing /^V^[ where that means these three keys: slash, Ctrl-V, Escape. Then hit Enter and you'll now hop from one instance of embedded Escape characters in your text to the next. – B Layer Jun 10 '21 at 20:52

1 Answers1

3

You can use \%x1b to match the ESC character in a pattern:

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

The \%x sequence allows you to match a character by its hexadecimal code, which is 1b in the case of the ESC character, which is usually rendered as ^[.

By the way, you can use the ga command with the cursor on top of a character to be able to tell its ASCII code or Unicode code point in hexadecimal, decimal, octal, which should help determine which code you should use to match it in a pattern.

filbranden
  • 28,785
  • 3
  • 26
  • 71