I run multiple screen sessions each created with 'screen -S name' and I would like to be able to display in the status bar the name I used to create the current screen session.
However, I cannot seem to accomplish this. Any ideas?
Join Stack Overflow to learn, share knowledge, and build your career.
I run multiple screen sessions each created with 'screen -S name' and I would like to be able to display in the status bar the name I used to create the current screen session.
However, I cannot seem to accomplish this. Any ideas?
screen
has two status bars, the caption bar and the hardstatus bar, both of which use the string escapes specified in the "STRING ESCAPES" section of man screen. Unfortunately, there is no escape that directly refers to the session name.
However, there is a hack that will allow you to do this.
screen
passes the session name to the shell using the $STY
variable. When the shell attempt to set the window title (using one of these methods) screen
captures that attempt, and stores it in something it confusingly calls "the window hardstatus," which does have an escape that you can use: %h
.
So if you have either the caption or hardstatus bar set to include %h
and have the shell attempt to set the window title to $STY
, then the %h
will be replaced with the session name when the bar is displayed.
man ssh
's ENVIRONMENT section, if the sshd of the remote host allows it (default is not), you can set environment vars (like STY
) in your local .ssh/environment
file (so in your local .bashrc
, just cat STY=$STY > .ssh/environment
) and when you ssh to the remote machine that will set the appropriate environment vars.
– rampion
Apr 9 '10 at 15:47
sshd
settings, then you could have your local .bashrc
dump the STY value to a file, alias ssh to first scp that file to the remote host, and have your remote .bashrc
source that file if STY is not set (that way the same .bashrc can be used remotely and locally).
– rampion
Apr 9 '10 at 15:49
alias screen='screen -s -/bin/bash'
and export PS1='$STY> '
in my .bash_profile, so wtih the alias, screen executes my .bash_profile in each session and with the export I can see which session I'm working with.
– i love mistaking
Oct 2 '20 at 7:19
The easiest way to display the sessionname is
C-a :
sessionname
(without specifying a name after sessionname
)
See the "CUSTOMIZATION" section in man screen
Ctrl-A
then :
then sessionname
(Ctrl-A should be substituted with whatever you have set for the screen control keystroke, if you have changed it from the default of Ctrl-A.) You will then see output similar to: This session is named '31438.Perlrocks'
.
– Medlock Perlman
May 3 '17 at 10:36
paraphrased from https://superuser.com/a/212520/151988, put this into your ~/.screenrc;
screen
screen $SHELL -c 'screen -X caption always "$STY"'
Super User has an answer to this that does not require $STY
, instead using the backtick
screen config command and screen -ls
: https://superuser.com/a/212520
If nothing else works (as for me), as a workaround you can create a window with number 0 and set title to your screen name:
screen -S myscreen
C^a :title "myscreen"
As max_cantor says in the SuperUser Answer, an escape character for the session name should be added to version 4.1.0. It looks like the escape character function was added with a relatively small patch back in 2008. So if you're feeling brave, you can git yourself a copy of the development version 4.1.0 and try it out.
I'll try this with the development version when I get a chance.