How do I display the current “session” name or “sockname” of a screen session in the status bar?

血红的双手。 提交于 2019-11-27 20:04:58

问题


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?


回答1:


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.




回答2:


The easiest way to display the sessionname is

  C-a : 
  sessionname

(without specifying a name after sessionname)

See the "CUSTOMIZATION" section in man screen




回答3:


In current versions of screen, there is a flag %S for the hardstatus line.

MWE (.screenrc):

hardstatus on
hardstatus alwayslastline
hardstatus string "%S"

This displays the session name without the ID (like ${STY#*.}).

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




回答4:


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




回答5:


paraphrased from https://superuser.com/a/212520/151988, put this into your ~/.screenrc;

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



回答6:


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"



回答7:


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.



来源:https://stackoverflow.com/questions/2479683/how-do-i-display-the-current-session-name-or-sockname-of-a-screen-session-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!