Differences between revisions 6 and 8 (spanning 2 versions)
Revision 6 as of 2009-09-11 16:40:25
Size: 643
Editor: GreyCat
Comment: How can I set the contents of my terminal's title bar?
Revision 8 as of 2011-04-03 15:07:58
Size: 797
Editor: adsl-75-61-109-235
Comment: Changed \e to \x1b, which works with other Posix shells. Considered leaving a note about \e as an option but don't think it's needed.
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
settitle() { printf '\e]2;%s\a' "$*"; } settitle() { printf '\x1b]2;%s\a' "$*"; }
Line 9: Line 9:
If you want to set the title bar to the currently-running process's title every time you type a command, then this solution approximates it: If you want to set the title bar to the currently-running command line every time you type a command, then this solution approximates it:
Line 12: Line 12:
trap 'printf "\e]2;%s\a" "$(HISTTIMEFORMAT='' history 1)"' DEBUG trap 'printf "\e]2;%s\a" "$(HISTTIMEFORMAT='' history 1)" > /dev/tty' DEBUG
Line 16: Line 16:

Or to use just the name and arguments of the current simple command:

{{{
trap 'printf "\e]2;%s\a" "$BASH_COMMAND" > /dev/tty' DEBUG
}}}

How can I set the contents of my terminal's title bar?

If you have a terminal that understands xterm-compatible escape sequences, and you just want to set the title one time, you can use a function like this:

settitle() { printf '\x1b]2;%s\a' "$*"; }

If you want to set the title bar to the currently-running command line every time you type a command, then this solution approximates it:

trap 'printf "\e]2;%s\a" "$(HISTTIMEFORMAT='' history 1)" > /dev/tty' DEBUG

However, it leaves the command history number in place, and it doesn't trigger on explicit subshells like (cd foo && make).

Or to use just the name and arguments of the current simple command:

trap 'printf "\e]2;%s\a" "$BASH_COMMAND" > /dev/tty' DEBUG

BashFAQ/093 (last edited 2016-10-22 16:49:46 by politkovskaja)