how to include screen's session name in hardstatus?

17

8

I use different screen sessions for different projects. Starting screen like screen -S project1. Now, I'd like to mention 'project1' in hardstatus line.

Session name can be obtained from environment variable STY: STY=13539.project1.
But how to get this into screen? I've tried backtick command and %` in hardstatus, but I can't seem to get it right.

What I did:

.screenrc:

hardstatus string '%H:%`'
backtick 0 30 30 echo $STY

no luck, empty %`.

backtick 0 30 30 sessionname

still no luck, sessionname: Not found

fungusakafungus

Posted 2010-11-18T17:11:01.420

Reputation: 1 313

Answers

18

You can include this string (with additional information, if desired) in your $PS1:

\[\e]0;$STY\007

inside single quotes in order to delay evaluation of the variable. Then add this to your ~/.screenrc:

hardstatus string '%H:%h'

Unfortunately, screen doesn't set $STY in the environment of the commands it spawns for the backtick feature.

Another option, instead of the one above:

hardstatus string '%H:%`'
backtick 0 30 30 sh -c 'screen -ls | grep --color=no -o "$PPID[^[:space:]]*"'

The advantage of this one is that it follows changes made by using the sessionname command. The first option doesn't.

Edit:

From here:

Since $STY is not set yet when screen sources .screenrc, you can use this trick in your .screenrc:

    screen 
    screen $SHELL -c 'screen -X caption always "$STY"' 

I.e. send a screen command to the first window.

Paused until further notice.

Posted 2010-11-18T17:11:01.420

Reputation: 86 075

1For the solution that starts with backtick: (1.) is backtick a bash builtin? a gnu-screen command? a binary executable? is there a man page? (2.) I know it works because I tried it but how do you get a two line command to execute when it seems like the gnu-screen .screenrc syntax requires one line for the hardstatus command? (3.) how would I modify your command to allow appending some text after the session name? – Trevor Boyd Smith – 2015-11-12T21:17:39.073

@TrevorBoydSmith: backtick is a screen command. Search for it in the man page for screen. It might be best if you post a new question regarding the rest of what you're asking. I don't think hardstatus output can occupy two lines on-screen. You could use sed or awk instead of grep to do what grep is doing in my example and also append some extra text. – Paused until further notice. – 2015-11-12T22:27:14.157

When I said "two line" let me restate what I meant, I don't want a two line display for hardstatus. I was confused by the %` and the backtick being on separate lines... they didn't look related but now that I read the documentation for backtick I see that the two are related/necessary/valid-syntax (albeit rather strange syntax IMO).

– Trevor Boyd Smith – 2015-11-13T14:33:58.480

3

For me this easily works with inserting %S in the hardstatus.

MWE (.screenrc):

hardstatus on
hardstatus alwayslastline
hardstatus string "%S"

However, this displays the session name without the ID (like ${STY#*.}); in your example: project1.

(Same answer to other questions here and here for completeness).

Scz

Posted 2010-11-18T17:11:01.420

Reputation: 263

2FYI, The "%S" does work on newer versions of gnu-screen (works on Fedora 21 version for me) but not older versions (does not work on CentOS 6 version for me). – Trevor Boyd Smith – 2015-11-12T21:14:20.890