Set screen-title from shellscript

69,094

Solution 1

You can set the screen / xterm title using the following lines:

#!/bin/bash

mytitle="Some title"
echo -e '\033k'$mytitle'\033\\'

[UPDATE] - by request I'm also including the solution proposed by @Espo below:

Depending on your xterm version or your linux distribution the line above may or may not work and you can try the xterm-defaults:

#!/bin/bash

mytitle="Some title"
echo -e '\033]2;'$mytitle'\007'

For more on the details see: http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#s3 or refer to the answer by @Espo below.

Solution 2

From http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#s3

xterm escape sequences

Window and icon titles may be changed in a running xterm by using XTerm escape sequences. The following sequences are useful in this respect:

  • ESC]0;stringBEL -- Set icon name and window title to string
  • ESC]1;stringBEL -- Set icon name to string
  • ESC]2;stringBEL -- Set window title to string

where ESC is the escape character (\033), and BEL is the bell character (\007).

Printing one of these sequences within the xterm will cause the window or icon title to be changed.

Note: these sequences apply to most xterm derivatives, such as nxterm, color-xterm and rxvt. Other terminal types often use different escapes; see the appendix for examples. For the full list of xterm escape sequences see the file ctlseq2.txt, which comes with the xterm distribution, or xterm.seq, which comes with the rxvt distribution.

Printing the escape sequences

For information that is constant throughout the lifetime of this shell, such as host and username, it will suffice to simply echo the escape string in the shell rc file:

    echo -n "\033]0;${USER}@${HOST}\007"

should produce a title like username@hostname, assuming the shell variables $USER and $HOST are set correctly. The required options for echo may vary by shell (see examples below).

For information that may change during the shell's lifetime, such as current working directory, these escapes really need to be applied every time the prompt changes. This way the string is updated with every command you issue and can keep track of information such as current working directory, username, hostname, etc. Some shells provide special functions for this purpose, some don't and we have to insert the title sequences directly into the prompt string. This is illustrated in the next section.

Solution 3

The following are other ways to script the renaming of screen titles:

Adding the following settings to .ssh/config sets the screen title automatically upon logging in to a system using SSH:

Host *
  PermitLocalCommand yes
  LocalCommand [ "$TERM" == 'screen' ] && echo -ne "\033k%h\033\\" 

Instead of %h, which represents the hostname of the machine you are connecting with, you may use %n, which is the actual name / alias you used to connect to the machine.

NOTE: You need OpenSSH >= v5.1 to be able to use the Localhost %n and %h parameters. Check out 'man ssh_config' for more info on LocalCommand.

To automatically revert the title, back to that of the hostname of the localhost, after closing the SSH session, you can add an escape sequence to you prompt variable PS1 in .bashrc :

export PS1='you_favorite_PS1_here'
if [ "$TERM" == 'screen' ]; then
    export PS1=${PS1}'\[\033k\h\033\\\]'
fi

These tricks are especially useful when using a .screenrc config that shows you in what screen 'tab' you are currently working. Add something like the following to .screenrc to get this working:

caption always "%{= kY}%-w%{= Yk}%n %t%{-}%+w%{ kG} %-= @%H - %LD %d %LM - %c"

Solution 4

Try the below commands, no need to edit any file or configuration like ~/.bashrc, Can be used at runtime.

Set static text as title: (My Title)

export PS1='\[\e]0;My Title\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

Set local/global variable as title: ($USER)

export PS1='\[\e]0;$USER\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

Set command output as title: (hostname)

export PS1='\[\e]0;`hostname`\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

Set to default (Revert back):

export PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

Solution 5

set_screen_title ()
{
    echo -ne "\ek$1\e\\"
}
Share:
69,094
Beerweasle
Author by

Beerweasle

Updated on December 24, 2020

Comments

  • Beerweasle
    Beerweasle over 3 years

    Is it possible to set the Screen Title using a shell script?

    I thought about something like sending the key commands ctrl+A shift-A Name enter

    I searched for about an hour on how to emulate keystrokes in an shell script, but didn't find the answer.