11

I discovered through this answer that I've been setting terminal colours in my prompt in a naive way for years.

I've now modified my .bashrc to use 'tput' commands to colourise various elements of my prompt. The one remaining escape code I have is this:

TITLEBAR="\[\033]2; PROD - \u@\h:\w \007\]"

Does anyone know if there is a tput/terminfo attribute I can use to set the title bar of my terminal window, so that I can eliminate that escape?

Murali Suriar
  • 10,166
  • 8
  • 40
  • 62

4 Answers4

12

The actual capabilities are tsl (To Status Line) and fsl (From Status Line), but of course not all terminals have then. In Linux in particular you need to set your terminal to xterm+sl or something similar.

You can test this with

export TERM=xterm+sl
echo `tput tsl` Hello world `tput fsl`; sleep 10

I wouldn't bother and keep those escapes in .bashrc

codehead
  • 958
  • 5
  • 7
  • 2
    Seems my copy of Ubuntu lacks that terminal info. :( I blame incomplete terminfo files :) – David Pashley Jun 11 '09 at 12:16
  • 1
    screen, however, appaears to support them just fine. Thanks! – Murali Suriar Jun 11 '09 at 12:26
  • 3
    `\033];` is the escape sequence in `xterm` for an ["Operating System Command"](https://gist.githubusercontent.com/lenormf/5c638b8c58b7460140cfa281d452f17e/raw/ecf68a80e8d964fbb9c7a6b45427f27a7a1ffc86/ctlseq2.txt). I don't know how to do that in tput, but if it is possible to do via tput that would be a better method for cross platform terminal title setting. E.g. on xterm, it would result in the sequence `\033]0;Title\007`. See also [here](http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Operating-System-Controls) – Wyatt Ward Mar 23 '18 at 17:09
  • @codehead Your solution is embarrassing because if I type `export TERM=xterm+sl`, then I no longer have syntax highting in vim. – SebMa Apr 26 '19 at 13:49
  • @SebMa: Embarassing? He never _suggested_ to change your `TERM`, he only (correctly) said this is required in Linux to make `tput` output the proper sequences. He even said "I wouldn't bother [to set TERM,] and keep those [hardcoded] escapes" – MestreLion Nov 26 '21 at 07:20
  • 1
    Note that `TERM=xterm+sl tput tsl` will use the sequence `\e]0;` which sets both the window title _and the icon name_, and is probably not what you want. `TERM=xterm+sl-alt`, if you have such terminfo, will properly use `\e]2;` instead, which sets the title only. – MestreLion Nov 26 '21 at 15:18
4

I can't comment, thus an extra answer:

If you use tsl/fsl, be sure to ask the terminal for hs first. hs should be true if tsl/fsl (and other *sl) are supported.

tput hs && { echo ....; }
  • 1
    +1 if you have a reference for this. – l0b0 Mar 21 '12 at 08:28
  • @l0b0 - I guess it could be inferred from `man 5 terminfo`, specifically the `Status Lines` section. See: https://www.man7.org/linux/man-pages/man5/terminfo.5.html Of course, I guess you could also do: `tput tsl && { echo 'hello world'; tput fsl; }` – mpb Jun 20 '20 at 23:37
  • @mpb `tput tsl` and `tput hs` both fail in GNOME Terminal, so I'm not sure what to do with either. – l0b0 Jun 21 '20 at 02:47
  • @l0b0 - It does seem that multiple terminals do support changing the title, but this feature is not published in their terminfo entries. If you are programming conservatively, and you are writing an app that may be used across multiple terminals by third-party users, then the safe thing to do would be to check that `tsl` (or `hs`) existed. If you assume it exists, and it does not, then you would end up writing unintended text at the cursor location. – mpb Jun 21 '20 at 05:29
3

Having looked at the xterm terminfo file, I can't find any code that is identical to this command, so I suspect there is not.

If you're interested, the terminfo man page lists all the commands that termcap and terminfo understand. I decompiled the terminfo database file using:

# infocmp -L xterm 
David Pashley
  • 23,151
  • 2
  • 41
  • 71
1

For more info on xterm's "vt100 mode" special features, see the Xterm Control Sequences document in the Operating System Controls section for all the gory details.

Randall
  • 606
  • 4
  • 4