17

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.

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.

  • 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
  • I posted my solution script below. should be a close match what @nye17 did. unix.stackexchange.com/a/341277/112190 – phyatt Jan 30 '17 at 19:03
5

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.

  • 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
3

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'"
  • 1. 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 4:02
  • The prompt command simply emits the necessary escape sequence for your terminal to rename its title bar. :) – Kuba Ober Jun 15 '12 at 4: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 5:23
  • 1
    So then put the desired title in a variable, and output that. – Ignacio Vazquez-Abrams Jun 15 '12 at 6:08
  • 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
3

ADD these lines to ~/.bashrc

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"

    ;;
*)
    ;;
esac

works well with debian based linuxes

  • 1
    IT doesn't set the title though... – Bananguin Jul 29 '14 at 19:55
  • It usually works better to use PROMPT_COMMAND instead of using PS1, since you don’t have to include the \[ \] bracketing and it can eliminate some quoting. – Chris Page Oct 25 '14 at 0:41
1

One solution is to change the terminal variable to vt100 before ssh'ing, e.g. run

export TERM=vt100

This works because usually initialization scripts look at the terminal variable and only change it title it it's xterm. You lose alternate screens, colors and maybe some other fancy features you may or may not appreciate.

If you want to keep the local terminal as xterm, alias ssh to change the terminal variable on the fly, like

alias ssh='TERM=vt100 /usr/bin/ssh'
0

I fixed this issue by adding this to my PS1 in my .bashrc file.

\[\e]2;\u@\H:\w\a\]

Therefore the whole line looks like this(I've customized my PS!:

export PS1='\[\e]2;\u@\H:\w\a\]\n($LOGNAME@$HOSTNAME) \t\n[$PWD]: '

I hope that helps others.

  • If you use this terminal escape sequence to set a null title (e.g., export PS1='\[\033]0;\007\]\h:\W \u\$ '), 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, 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:54
0

Other answers wouldn't work for me when I exit SSH, but got me close. Adding setw automatic-rename works when I exit remote ssh session so it auto renames and don't have to exit the window anymore to reset. [Jan. 2018 - Debian 9]

ssh() {
tmux rename-window "$*"
command ssh "$@"
tmux setw automatic-rename
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.