2

I wrote a terminal output to a plain text file and opened it in Emacs, and I see "^[" here and there. I'd like to change it to tabs in terminal before writing its output to the file, but I don't know how to refer to them. What does "^[" represent?

They have "[1m" or "[4m" right after them.

Drew
  • 75,699
  • 9
  • 109
  • 225
stacko
  • 1,577
  • 1
  • 11
  • 18

3 Answers3

1

^[ is the ASCII escape character, aka ESC. It is:

  • octal: \033
  • hexadecimal: \x1B or ^[
  • decimal: 27

The color codes (and other control codes) you mention each begin with the ASCII escape character.

Drew
  • 75,699
  • 9
  • 109
  • 225
1

Characters represented with ^ followed by another character (an uppercase letter or one of @[\]^_?), with both characters in a different color (depending on the color scheme), are control characters. ^[ is the escape character. You can get more information by pressing C-x = (what-cursor-position) while the cursor is on the character and even more information with C-u C-x =.

Here, in output meant for a terminal, the escape character starts an escape sequence, specifically an ANSI escape sequence which is how the application tells the terminal to do something other than “display this character at the cursor location and move one position to the right”. Specifically, ^[[…m (also known as “CSI … m” because the two-character sequence ^[[ is known as CSI) change the text attributes for subsequet output. ^[[1m turns bold on, ^[[4m turns underline on, and ^[[0m turns all attributes off.

Changing those characters to tabs makes no sense whatsoever.

You can use this script to remove most terminal escape sequences.

0

It turned out that they were color codes in Terminal. I managed to get rid of them by

sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"

http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed

stacko
  • 1,577
  • 1
  • 11
  • 18