right now I am using an one-line perl code to change titles of my terminal bars,
print("e]0;@ARGV7");
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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 "33]0;$*07"
}
function ssh()
{
/usr/bin/ssh "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="597d19">[email protected]</a>"
# revert the window title after the ssh command
title <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="290d7c7a6c7b69">[email protected]</a>$HOST
}
function su()
{
/bin/su "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8da9cd">[email protected]</a>"
# revert the window title after the su command
title <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="52760701170012">[email protected]</a>$HOST
}
Note: restart bash after edits to ~/.bashrc
Example:
# title is "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3a6a0b6a1e293b1bcab92">[email protected]</a>" ssh boxB # auto changes title while on boxB to "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1762647265265775786f55">[email protected]</a>" exit # title returns back to "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f085839582c1b0929f88b1">[email protected]</a>" because of our title call in ssh() su - user2 # auto changes title while switched user to user2: "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="691c1a0c1b5b290b061128">[email protected]</a>" exit # title returns back to "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="14616771662554767b6c55">[email protected]</a>" because of our title call in su()
Hope that helps.
Method 2
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:
-
Write a wrapper around ssh, i.e., an executable shell script named
sshthat takes precedence over/usr/bin/sshin your $PATH which contains the lineexec /usr/bin/ssh [email protected]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. - 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.
-
Have
PROMPT_COMMANDevaluate the output ofhistory. Basically a more generic and more ugly approach to 1.
Method 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;bash7n'"
Method 4
ADD these lines to ~/.bashrc
# If this is an xterm set the title to <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e99c9a8c9ba981869a9d">[email protected]</a>:dir
case "$TERM" in
xterm*|rxvt*)
PS1="[e]0;${debian_chroot:+($debian_chroot)}<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="750035">[email protected]</a>h: wa]$PS1"
;;
*)
;;
esac
works well with debian based linuxes
Method 5
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 "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e2a4e">[email protected]</a>"
tmux setw automatic-rename
}
Method 6
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'
Method 7
https://unix.stackexchange.com/a/341277/25975 and https://unix.stackexchange.com/a/28520/25975 have been combined for a simple clean solution at https://drone-ah.com/2020/06/25/how-to-terminal-title-after-disconnecting-from-ssh/. It also restores the previous title.
function ssh()
{
# push current title and icon to stack
echo -ne 'e[22t'
# Execute ssh as expected
/usr/bin/ssh "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b7f1b">[email protected]</a>"
# revert the window title after the ssh command
echo -ne 'e[23t'
}
Method 8
I fixed this issue by adding this to my PS1 in my .bashrc file.
[e]2;<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9bc89">[email protected]</a>H:wa]
Therefore the whole line looks like this(I’ve customized my PS!:
export PS1='[e]2;<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97e2d7">[email protected]</a>H:wa]n(<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a0e66656d646b676f6a">[email protected]</a>$HOSTNAME) tn[$PWD]: '
I hope that helps others.
Method 9
If the remote server is Debian, put the below code at end of ~/.bashrc
PROMPT_COMMAND='echo -ne "33]0;${USER}: ${PWD}07"'
Method 10
This might be not exactly the most direct approach, but I use remote command execution in order to execute the command remotely on request in some scenarios, but to stay in my former terminal session where applicable like this
ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="87f5e8e8f3c7f4e2f5f1e2f5">[email protected]</a> 'tail -f /var/log/someApp/appLog.log'
that way my original non-ssh title stays in place, intact
Method 11
I used this custom function in my .bashrc
function s(){
ssh -t $1 "echo -en '33]0;'"$1"'a';bash"
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0