10

Is there a way to present the GNU Screen session name and window title in the prompt of the shell (let us say, the Bash prompt defined by PS1)?

13

Screen supplies some environment variables (from screen(1) manpage):

STY              Alternate socket name.
WINDOW           Window number of a window (at creation time).

The "at creation time" means that if you renumber a window (using screen's number command), the shell will not be told about the change and $WINDOW will still be the same as the first window number.

You could use something like:

PS1='\u@\h(${STY}:${WINDOW}):\w$ '
  • 1
    Perfect! This is what I was looking for. BTW, there is a way to get the window title, instead of the window number? – brandizzi Sep 19 '11 at 1:13
  • Not that I know of, there is no api for screen, and when you issue commands there is no output - it goes to the current window in the screen. For example screen -xr 2466 -X windows. – Arcege Sep 19 '11 at 1:26
  • Although it probably falls under the renumbering case, is there a way to access the window's name if it's set with Ctrl-a A ? – Thalis K. Jul 5 '15 at 12:44
  • 1
    If GNU screen has been compiled with the -Q (query) option, then you can run some commands, such as title and windows and have the data display to stdout, however, without a trailing newline. – Arcege Jul 6 '15 at 17:38
2

I always use precise screen session names. Then I can add screen's STY env var, with the numeric id stripped out (thanks dimo414), to PS1. I don't decorate it with curly braces or anything because I'm not always in a session. Simple example:

PS1='\u@\h ${STY#[0-9]*.} \w$ '
1

I use this:

(`echo ${STY} | sed -e 's/[0-9]*\.//g'`:${WINDOW}:`screen -Q title`)

removes the process number from ${STY}
includes the window number (as mentioned "at creation time")
includes the window title (as returned from screen -Q title)

notes:

  • first time it runs, screen -Q waits for a return keystroke
  • i set the window title with C-a C-A and then source my .profile to update the prompt
  • 4
    Using ${STY#[0-9]*.} avoids needing to pipe through sed. – dimo414 Jan 10 '17 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.