26

right now I am using an one-line perl code to change titles of my terminal bars,

print("\e]0;@ARGV\7");

but every time after I ssh to another remote machine, the title will be modified by the host (by which I'm not particularly bothered). But after I exit from the connection, the modified title remains. Is there a way to fix this? essentially I want a fixed title for my terminals when operating locally.

I primarily use xfce terminal and terminator under either CentOS or Debian. Thanks.

EDIT

Another subtlety is that, rather having all terminals the same name, I would prefer to have the freedom to edit their titles on-the-fly but only forbid SSH session from modifying what I edited.

nye17
  • 363

11 Answers11

12

Solution: add some functions ~/.bashrc to do something after ssh and su commands

function title()
{
   # change the title of the current window or tab
   echo -ne "\033]0;$*\007"
}

function ssh()
{
   /usr/bin/ssh "$@"
   # revert the window title after the ssh command
   title $USER@$HOST
}

function su()
{
   /bin/su "$@"
   # revert the window title after the su command
   title $USER@$HOST
}

Note: restart bash after edits to ~/.bashrc

Example:

# title is "user1@boxA"
ssh boxB  # auto changes title while on boxB to "user1@boxB"
exit
# title returns back to "user1@boxA" because of our title call in ssh()
su - user2 # auto changes title while switched user to user2: "user2@boxA"
exit
# title returns back to "user1@boxA" because of our title call in su()

Hope that helps.

phyatt
  • 607
  • 2
    slightly inaccurate to say revert as you do not check to see what it was before ... if you queried and saved the prior value tho ;-) – nhed Mar 23 '17 at 20:10
  • If $HOST isn't set, use $HOSTNAME – Rich Nov 16 '20 at 20:07
6

I don't know about window titles, but I have been trying to have my system do something on terminating a ssh session—actually, after terminating a ssh session. In short: it doesn't work like that. Basically you have three choices:

  1. Write a wrapper around ssh, i.e., an executable shell script named ssh that takes precedence over /usr/bin/ssh in your $PATH which contains the line exec /usr/bin/ssh $@ somewhere in its middle. This enables you to have your shell do some stuff before and after the effective ssh binary is run, while keeping th eoverhead to a minimum.

  2. Write a patch against the SSH sources of your choice to provide you a cleanup hook that executes a shell command passed via commandline or some config-setting. That's what we want.

  3. Have PROMPT_COMMAND evaluate the output of history. Basically a more generic and more ugly approach to 1.

Bananguin
  • 7,984
  • basically i just wrote a simple script to grab the current title and save it before SSH session, then recover the title afterwards. – nye17 Jun 15 '12 at 19:04
  • 4
    @nye17 Can you post your solution script that you used ? – Reg Mem Jul 29 '14 at 18:28
  • 1
    I posted my solution script below. should be a close match what @nye17 did. http://unix.stackexchange.com/a/341277/112190 – phyatt Jan 30 '17 at 19:03
4

Configure your local shell dotfile (e.g. $PROMPT_COMMAND in ~/.bashrc) to set the terminal title appropriately, using the same mechanism.

For example:

export PROMPT_COMMAND="printf '\e]0;bash\7\n'"
  • prompt_command has nothing to do the title of your terminal window, right? I have prompt_command set up but only for the prompt. 2. I still need a mechanism to remember and activate my local setting after exiting the ssh logging.
  • – nye17 Jun 15 '12 at 04:02
  • 2
    The prompt command simply emits the necessary escape sequence for your terminal to rename its title bar. :) – Kuba hasn't forgotten Monica Jun 15 '12 at 04:03
  • but I also want the freedom to rename whatever terminal I want, rather than having every single session the same name. In practice, I basically edit the terminal title manual and hope that terminal to stay having fixed title even after SSH login/logout. Does it make sense? – nye17 Jun 15 '12 at 05:23
  • 2
    So then put the desired title in a variable, and output that. – Ignacio Vazquez-Abrams Jun 15 '12 at 06:08
  • 1
    If you use this terminal escape sequence to set a null title (e.g., export PROMPT_COMMAND="printf '\e]0;\7\n'"), then your terminal emulator can set the window title as it sees fit. When you ssh to a remote host that sets the terminal title, your terminal emulator will show that title, and when you logout, it will revert to using its own title. At least, that's how the macOS Terminal app works. – claymation Apr 16 '17 at 18:52
  • This works for me. I am using terminator. – nitin Jul 18 '17 at 06:10