2

So I'm quite confused with the concept of escape sequences in a terminal (or terminal emulator to be more precise) and how they relate to the screen bindkey command. I was looking at some sample .screenrc file that I found on the internet where I saw the following configuration:

# switch windows with F3 (prev) and F4 (next)
bindkey "^[OR" prev
bindkey "^[OS" next

# switch layouts with Ctrl+F3 (prev layout) and Ctrl+F4 (next)
bindkey "^[O1;5R" layout prev
bindkey "^[O1;5S" layout next

I have no idea why, for example, the sequence ^[0R means F3 and the sequence ^[01;5R means Ctrl+F3. Is there a table for such sequences?

Also these sequences remind me of the sequences used to configure the colors in a terminal prompt (e.g using the $PS1 env variable) like \[\033[0m\] for reset and \[\033[30m\] for foreground black. Is there actually any relationship between these two "code sequences" or is it just my pure imagination?

jvb
  • 3,196
eciii
  • 131

1 Answers1

1

Good question.

Yes, these are ANSI escape codes.

You can see a list of ANSI escape codes for VT100 here: http://www.braun-home.net/michael/info/misc/VT100_commands.htm but there is a quicker way - in your terminal emulator press Control-v and then Control-F3 for example and you will see this:

$ ^[[1;5R

In man bash it says:

   quoted-insert (C-q, C-v)
          Add the next character typed to the line verbatim.  This
          is how to insert characters like C-q, for example.

Notice that ^[ itself is Escape in the output of quoted-insert and you can study this answer https://unix.stackexchange.com/a/108014/72304 to learn where this notation comes from.

To understand it better - as you already know there are some ANSI escape codes for selecting color that use Escape key. There are several methods to enter a literal Escape - we can for example use octal notation as we know that Escape is \033 in ASCII table:

printf "\033[1;34mThis is a blue text.\n\033[0m"

We can also use \e backslash escape that some implementations of printf understand:

printf "\e[1;34mThis is a blue text.\n\e[0m"

But you can also use quoted-insert to insert a literal Escape - point your cursor in place of \e, remove it, press Control-v and then press Escape. You'll get this:

printf "^[[1;34mThis is a blue text.\n^[[0m"

Notice that when you do that in your terminal emulator ^[ will be treated as a single character. Copying the above line and pasting into your terminal emulator will not work though as you will get 2 separate characters instead of one.