GNU Screen – Using Terminal Escape Sequences

escape-charactersgnu-screenterminal

I have a ruby terminal script that I would like to print out hyperlinks. I achieve that like so:

puts("\e]8;;https://example.com\aThis is a link\e]8;;\a")

This works perfectly fine in a "normal" terminal (gnome-terminal btw) window.

But I need to run this script within GNU screen, where the escape-sequence simply has no effect. Other sequences (like colors for example) work fine, the hyperlink one (which according to the source might be a gnome-terminal-only thing) doesn't. (screen is running inside gnome-terminal)

How can I get screen to acknowledge my link sequence and display it properly?

Best Answer

You can pass through some text to the terminal screen itself runs in by putting it inside an ESC P, ESC \ pair (\033P%s\033\\ in printf format).

So you should bracket inside \eP..\e\\ pairs all the parts of the sequence, except for the text which will appear on the screen ("This is a link"):

printf '\eP\e]8;;https://example.com\a\e\\This is a link\eP\e]8;;\a\e\\\n'
printf '\eP\e]8;;%s\a\e\\%s\eP\e]8;;\a\e\\\n' https://example.com 'This is a link'

Or, from C:

puts("\eP\e]8;;https://example.com\a\e\\This is a link\eP\e]8;;\a\e\\");
printf("\eP\e]8;;%s\a\e\\%s\eP\e]8;;\a\e\\\n", url, title);

Putting the replacement text too inside \eP..\e\\ may result in screen losing track of the cursor position.


This is documented in the GNU screen manual:

ESC P  (A)     Device Control String
               Outputs a string directly to the host
               terminal without interpretation

The "string" should should be terminated with a ST ("string terminator") escape, ie. \e\\ -- thence \eP..\e\\.

Related Question