there are many ways to set the gnome-terminal tab title or window title using ANSI escape codes in bash. (also xterm, etc.) we describe some of the ways with an ad-hoc context-sensitive grammar.
start = {printer} {quotedstring}
printer = printf | echo -ne | /usr/bin/printf | /bin/echo -ne
quotedstring = {singlequotes} | {doublequotes} | {noquotes}
singlequotes = '{escape}]{osc};YOUR TEXT HERE{terminator}'
doublequotes = "{escape}]{osc};YOUR TEXT HERE{terminator}"
noquotes = {escape}]{osc}\;YOUR\ TEXT\ HERE{terminator}
escape = {backslash}e | {backslash}{octalescape} | {backslash}{hexescape}
backslash(quotedstring = singlequotes) = \
backslash(quotedstring = doublequotes) = \ | \\
backslash(quotedstring = noquotes) = \\
octalescape(printer = printf | /usr/bin/printf) = 33 | 033
octalescape(printer = echo -ne) = 033 | 0033
octalescape(printer = /bin/echo -ne) = 33 | 033 | 0033
hexescape = x1b
osc = 0 | 2
terminator = {bel} | {st}
bel = {backslash}a | {backslash}{octalbel} | {backslash}{hexbel}
octalbel(printer = printf | /usr/bin/printf) = 7 | 07 | 007
octalbel(printer = echo -ne) = 07 | 007 | 0007
octalbel(printer = /bin/echo -ne) = 7 | 07 | 007 | 0007
hexbel = x7 | x07
st = {escape}{backslashchar}
backslashchar(quotedstring = singlequotes) = \ | \\
backslashchar(quotedstring = doublequotes) = \\ | \\\\
backslashchar(quotedstring = noquotes) = \\
the grammar expands to hundreds of possible commands through combinatorial explosion. this post was inspired by other online guides explaining how to do this, but giving different instructions. it turns out many commands are equivalent.
osc = "operating system command". 0 = "set icon name and window title" ; 2 = "set window title". see man 4 console_codes . my window manager seems to ignore the "set icon name" part of 0, so the two are equivalent.
st = "string terminator". see man 4 console_codes
information about \e and \a is in "help echo", "man echo", "man 1 printf", but not "help printf".
with double quotes, backslash=\ and the subsequent code is interpreted by the shell before it gets passed to the printer command or program. backslash=\\ passes a single backslash to the printer, and then the code gets interpreted by the printer.
the reason why backslashchar(singlequotes)=\ and backslashchar(doublequotes)=\\ work is because it is at the end of the string. if there were more characters, e.g., more ANSI escape codes doing subsequent stuff, it would not work.
the backslash character is also octal 0134 and hexadecimal 0x5c, which could have added more possibilities.
some examples:
printf "\e]0;YOUR TEXT HERE\a"
echo -ne '\033]2;YOUR TEXT HERE\x07'
/usr/bin/printf "\\x1b]0;YOUR TEXT HERE\x1b\\"
the commands described by the grammar set the tab or window title as a one-time operation. future commands might again change it to something you don't want. a common hack (not covered here) is to set the PS1 prompt variable in bash, which keeps the title the way you want it, setting it each time a command prompt is displayed. see the section PROMPTING in man bash . incidentally, the section has yet another list of permitted backslash sequences, including \e and \a.
it is also possible to type literal control characters in the bash command line by typing Control-V first. Control-V Escape inserts ESC, and Control-V Control-G inserts BEL. echo then does not need the -e flag. however, omitting -e will affect backslashchar. similar raw characters could be included in a script (e.g., using Control-Q in emacs), making a rather ugly script that might change your tab title merely by viewing it (cat), not executing it.
considering sh (dash) instead of bash opens up a new can of worms. the builtin printf command does not accept \x . echo does not have a -e flag but always interpets backslashes, but not \x or \a.
No comments :
Post a Comment