Subject: Setting window title in ssh'ed host

From: Gokdeniz Karadag <[email-masked]>
Date: Sat, 12 Jul 2008 03:10:00 +0300

Hi,

This is more like a story, I am trying to  automatically set 
"GNU screen window title" equal to the hostname that is connected by ssh.

I am using the following for hardstatus, which presents a list of all open windows
hardstatus lastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<"

Also my .screenrc connects to some machines,
screen -t hostname
stuff "ssh hostname\015
....

Sometimes I logout of the connected machine and connect to another.  I don't
want to set title each time I do this.  If there is no screen in the way, the
following bash variable shows the hostname in xterm title.
PROMPT_COMMAND='echo -ne "\033]0;${HOSTNAME}\007"'

If I use this, and change %t->%h in hardstatus string, the hostname shows in
the "currently selected window only", but not the others. As there is no
equivalent of "-w" "+w" for xterm title(which is an actual "hard" status), this
path was blocked.

Setting the following in all machines achieves what I want, 
it sets screen title to hostname,
PROMPT_COMMAND='echo -n -e "\033k${HOSTNAME}\033\\"'
but when I connect to the machine out of screen, It prints the hostname
literally, so the prompt becomes like "hostname user@hostname $ " , which is
ugly and redundant.

To ensure that only ssh sessions within a GNU screen get the "title setter
prompt command", environment variables are useful, I can use the TERM variable,
but in stable debian, the default TERM=screen breaks vim editor, it goes crazy
when you press home, or pageup buttons.  Using TERM=xterm fixes this, so I
cannot check for TERM being equal to "screen".

I put "setenv INSCREEN yes" into .screenrc, and in the target shell, I set
PROMPT_COMMAND if this variable is set, but then,(surprise, surprise!) ssh
would not pass variables to target machine.  At least, does not pass unless you
use AcceptEnv in ssh server config, but I don't want to use it for security
reasons. Ok, maybe not because of security, but changing "ssh server
configuration" for only setting screen's window title seems absurd.

A solution that I imagined was to extend the "stuff" line; it does the ssh
connection, then puts the PROMPT_COMMAND to set GNU screen window title.
escaping this correctly was a chore, but I managed after some experimentation.
stuff "ssh hostname\015export PROMPT_COMMAND='echo -n -e "\\033k\${HOSTNAME}\\033\\\\\"'\015"

But of course, the connections made after these do not set the title. If only
there was a way to set GNU screen window title to the "hard status" set by the
escape sequences in PROMPT_COMMAND of bash ...

[[ rant: Why, why, why this great software gets the most generic name of "screen" ?
What's next, "computer", which is a shell that works by reading your mind,
and "earth" which gives the answer to life, the universe, and everything; and does 
not only say '42' ? So that you search the web for them and find nothing :-/

Please use "GNU screen" all the time, to be search engine friendly,
like I did in this message :-S

/rant ]]

-- 
Gokdeniz Karadag

_______________________________________________
screen-users mailing list
[email-masked]
http://lists.gnu.org/mailman/listinfo/screen-users

---

From: "Trent W. Buck" <[email-masked]>
Date: Tue, 15 Jul 2008 09:46:22 +1000

On Sat, Jul 12, 2008 at 03:10:00AM +0300, Gokdeniz Karadag wrote:
> This is more like a story, I am trying to automatically set "GNU
> screen window title" equal to the hostname that is connected by ssh.

In my .profile, which exists on all my machines and is evaluated for
all interactive logins (i.e. "ssh fooserv", but not "ssh fooserv
pwd"), I have

    type uname >/dev/null 2>&1 &&
    test -n "${HOSTNAME:=`uname -n`}" &&
    case "$TERM" in
        screen*) printf %bk%s%b%b \\033 "${HOSTNAME%%.*}" \\033 \\0134;;
    esac

Note that the "type" builtin isn't portable, if you use .bash_profile
you can instead just use

    case "$TERM" in
        screen*) printf %bk%s%b%b \\033 "${HOSTNAME%%.*}" \\033 \\0134;;
    esac

I use this in preference to doing it inside screen because I couldn't
work out how to have an "alias" or "function" in screen such that

    C-a x fooserv RET

would expand to something like

    C-a :screen -t fooserv ssh fooserv

If anyone knows how to do that, I'd prefer it (since customer machines
don't have my .profile installed).

> Sometimes I logout of the connected machine and connect to another.
> I don't want to set title each time I do this.  If there is no
> screen in the way, the following bash variable shows the hostname in
> xterm title.  PROMPT_COMMAND='echo -ne "\033]0;${HOSTNAME}\007"'

Oh, have a screen window with a shell, and you run "ssh fooserv" in
the shell.  I *think* my approach still works in that case.

> If I use this, and change %t->%h in hardstatus string, the hostname
> shows in the "currently selected window only", but not the
> others. As there is no equivalent of "-w" "+w" for xterm title(which
> is an actual "hard" status), this path was blocked.

I didn't understand any of that paragraph.

> To ensure that only ssh sessions within a GNU screen get the "title
> setter prompt command", environment variables are useful, I can use
> the TERM variable, but in stable debian, the default TERM=screen
> breaks vim editor, it goes crazy when you press home, or pageup
> buttons.  Using TERM=xterm fixes this, so I cannot check for TERM
> being equal to "screen".

Then you should fix vim, not train Screen to work with the wrong
settings.  Does vim work correctly (with TERM screen) if you tell it
not to evaluate your .vimrc and such?  If so, then the problem is with
your .vimrc, not with vim itself.

> I put "setenv INSCREEN yes" into .screenrc, and in the target shell,
> I set PROMPT_COMMAND if this variable is set, but then,(surprise,
> surprise!) ssh would not pass variables to target machine.  At
> least, does not pass unless you use AcceptEnv in ssh server config,
> but I don't want to use it for security reasons.

You need to configure ssh to do so with the SendEnv directive in
~/.ssh/config.  See ssh_config(5) manpage.  Dunno about AcceptEnv, I
guess you've tried it more than I have.

> Ok, maybe not because of security, but changing "ssh server

I agree.

> stuff "ssh hostname\015export PROMPT_COMMAND='echo -n -e "\\033k\${HOSTNAME}\\033\\\\\"'\015"

Instead of using :stuff, why not just set the title when you create
the screen window, running ssh instead of the default shell?

C-a :screen -t fooserv ssh fooserv

> But of course, the connections made after these do not set the title.

In the approach I propose, each subsequent ssh would get a fresh
window, so this would work.

> [[ rant: Why, why, why this great software gets the most generic

I agree.

---

From: Christian Ebert <[email-masked]>
Date: Tue, 15 Jul 2008 09:54:46 +0100

* Gokdeniz Karadag on Saturday, July 12, 2008 at 03:10:00 +0300
> Setting the following in all machines achieves what I want, it sets 
> screen title to hostname,

Does checking for $STY work for your purposes?

if [ -n "$STY" ]
   <screen specific stuff>
fi

c
-- 
  Was heißt hier Dogma, ich bin Underdogma!
[ What the hell do you mean dogma, I am underdogma. ]

_F R E E_  _V I D E O S_  -->>  http://www.blacktrash.org/underdogma/

---

From: Gokdeniz Karadag <[email-masked]>
Date: Tue, 15 Jul 2008 22:54:37 +0300

Hi,

First of all thanks for your answer. I have solved the question -- or the 
question suddenly disappeared :)

My replies are between quotations below.

Trent W. Buck demis ki::
.......
....
> the shell.  I *think* my approach still works in that case.

Yes, your approach is the proper solution to my problem, but another problem 
with "vim" made me use xterm as screen's $TERM during my previous attempts.

> I didn't understand any of that paragraph.

This is not much important but, my screen's hardstatus was like this;
hardstatus lastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%<"
Example:  0$* machineA  1-$ machineB  2$ machineC

I was able to set the "xterm title" using "xterm hardstatus" escapes "in the 
connected machine", while using TERM=xterm.
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}\007"'

I could display this "xterm hardstatus text"  in "screen hardstatus", by using 
"%h" in screen's hardstatus line. But, as you can see in my example ALL 
connected machine names can be seen, thanks to %-w and %+w in screen's 
hardstatus string. When "xterm hardstatus" is displayed using %h, there is no 
%-h and %+h to display all machines "xterm hardstatus" info on the last line.

If I couldn't made myself clear, feel free to ignore this as the problem is 
solved now.

> your .vimrc, not with vim itself.

My previus tests showed that setting TERM from "screen" to "xterm" solved 
problems with vim. Today, I intended to fix this issue once and for all, 
strangely, vim worked without problems using TERM=screen. As a result I can 
check TERM in servers and use screen specific escapes to set title.

> C-a :screen -t fooserv ssh fooserv

With stuff in screenrc, I can logout from the initially connected machine and 
that screen window does not close itself, just a convenience. Also, stuff can 
be extended to "cd" into relevant directory, run a non-login shell, etc.

Thanks again for the answer.

-- 
Gokdeniz Karadag

---

From: Gokdeniz Karadag <[email-masked]>
Date: Tue, 15 Jul 2008 22:59:26 +0300

Hi,

That works on the machine where screen is run, but my main use for screen is to 
connect to many machines using ssh.

An environment var. does not get passed to the target shell, if *both* SendEnv 
in the client and AcceptEnv in the server is configured.

When TERM is properly set to "screen", I can use it on the target server 
without extra configuration as I wrote in my answer to Trent Buck.

Thank you for the answer.

-- 
Gokdeniz Karadag

---

From: Micah Cowan <[email-masked]>
Date: Wed, 05 Nov 2008 14:39:13 -0800

Gokdeniz Karadag wrote:
> Hi,
> 
> This is more like a story, I am trying to  automatically set "GNU screen
> window title" equal to the hostname that is connected by ssh.

This has been a recurring need, solved in various ways.

Thomas Adam just pointed out to me the following link:
http://www.tenshu.net/screen_ssh/

which appears to solve a problem by using ssh's LocalCommand config
option (which I hadn't known about) to run a local command whenever it's
invoked.

- --
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer.
GNU Maintainer: wget, screen, teseq
http://micah.cowan.name/

---

From: Robin Lee Powell <[email-masked]>
Date: Wed, 5 Nov 2008 22:13:43 -0800

On Wed, Nov 05, 2008 at 02:39:13PM -0800, Micah Cowan wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Gokdeniz Karadag wrote:
> > Hi,
> > 

I've solved it by prompt hacking; here's the relevant bits from my
.screenrc at work:

# Magic to make PS1 goodness work
hardstatus off
hardstatus string "%t"
chacl :window: -rwx #?
chacl :window: +x title

#####
# With this setup, you can change the current window's title by
# sending an ANSI escape sequence; this will change the window's
# title and the title will show up in the hardstatus line (i.e. the
# title bar in PuTTY).  This sets the title to the current host
# name.  The actual prompt is the hostname followed by $ for bash or
# %# for zsh, which is magic that shows either # if you're root or %
# if you're not.
#
# zsh: PS1="$(echo -e '%{\033]83;title '$(uname -n)'\007%}%m%# ')"
#
# bash: PS1="\[\033]83;title $(uname -n)\a\]$(uname -n)\$ "
#
#####

-Robin

-- 
They say:  "The first AIs will be built by the military as weapons."
And I'm thinking:  "Does it even occur to you to try for something
other than the default outcome?" -- http://shorl.com/tydruhedufogre
http://www.digitalkingdom.org/~rlpowell/ *** http://www.lojban.org/

---

From: Micah Cowan <[email-masked]>
Date: Wed, 05 Nov 2008 22:50:31 -0800

> # bash: PS1="\[\033]83;title $(uname -n)\a\]$(uname -n)\$ "

I don't really see how that does what the thread's talking about;
namely, set the window title to the hostname that's connected by ssh.

Kinda cool about the :window:/OSC 83 command execution hack, though. I
was incredulous that those chacls actually did anything related to the
window title.

As previously mentioned in these threads (IIRC?), the problem with
prompt hacks is you don't always have control over the prompts in remote
shells (and even when you do, it's a bit of a pain to set up in every
host you ever visit).

- --
Micah J. Cowan

---

From: Robin Lee Powell <[email-masked]>
Date: Wed, 5 Nov 2008 22:53:11 -0800

> namely, set the window title to the hostname that's connected by

You set your remote shell's PS1 to tell screen to set the title to
$(uname -n) or whatever.

> prompt hacks is you don't always have control over the prompts in
> remote shells (and even when you do, it's a bit of a pain to set
> up in every host you ever visit).

True enough.  The LocalCommand hack is kinda neat.

-Robin

---

From: Malte Skoruppa <[email-masked]>
Date: Thu, 06 Nov 2008 11:25:00 +0100

Hi,

I solved the problem in bash by editing my ~/.profile file:

ssh() {
 args=$@
 echo -ne "\033k${args##* }\033\\";
 /usr/bin/ssh "$@";
}

It"s a rather simple script, whenever you call ssh, first this script is 
executed, which calls the real ssh in the end, with the same arguments.
Before it does that, however, it sets the screen title to the last 
argument of the ssh command. Usually, this is the hostname, at least for 
the way I enter commands ;-)

Malte

---

From: [email-masked] (Trent W. Buck)
Date: Thu, 06 Nov 2008 23:59:10 +1100

> echo -ne "\033k${args##* }\033\\";

You don't need to hard-code the path to ssh if you change this line to

    command ssh "$@"

the trailing semicolons are also unnecessary because newline acts as a
command delimiter.  Have a nice day.