20

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

Oliver Salzburg
  • 86,445
  • 63
  • 260
  • 306
fungusakafungus
  • 1,344
  • 2
  • 8
  • 14

2 Answers2

19

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.

Dennis Williamson
  • 106,229
  • 19
  • 167
  • 187
  • 1
    For 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 Nov 12 '15 at 21:17
  • @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. – Dennis Williamson Nov 12 '15 at 22:27
  • 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`](https://www.gnu.org/software/screen/manual/html_node/Backtick.html) I see that the two are related/necessary/valid-syntax (albeit rather strange syntax IMO). – Trevor Boyd Smith Nov 13 '15 at 14:33
7

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
  • 423
  • 5
  • 7
  • 2
    FYI, 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 Nov 12 '15 at 21:14