Search this site


Metadata

Articles

Projects

Presentations

Find that lost screen session, episode 2.

Like I said, I run screen in all of my xterms...

xterm sets an environment variable in child processes: WINDOWID. This is the X window id of the xterm window. Using this, we can extend upon my last post and come up with a much neater solution. Knowing what screen session you want to bring forward (assuming it's running in an xterm), we can run a command inside that session that grabs the $WINDOWID variable in the shell and uses xdotool to activate the window.

session=$(sh screen-find.sh "#freebsdhelp")
STY=$session screen -X screen sh -c 'xdotool windowactivate $WINDOWID'
Running this causes my IRC window to be activated by xdotool, which means it is now active, focused, and on top.

This isn't entirely optimal, because it assumes the xterm attached to that screen session is the xterm that launched it. If you run 'xterm -e screen -RR' and close the xterm (don't log out of the shell), then rerun 'xterm -e screen -RR' it will attach to that previous screen session, but the WINDOWID will understandably not be correct any longer.

So what do we do? Using the screen session given, we create a new window in that session and set the title of that window to a generated string. We then use xdotool to search for that string and activate the window. Once we get there, we can kill that new screen window we created and we are left with the terminal holding our screen session sitting in front of us.

I wrote a script to do just that tonight: screen-activate.sh. Example usage: screen-activate.sh 24072.pts-25.snack

This has a great benefit of supporting every terminal program that understands how to set the terminal window title when screen changes it's title. I have tested my .screenrc in Eterm, Konsole, gnome-terminal, and xterm - all know when screen changes it's title if you put this in your .screenrc:

hardstatus string "[%n] %h - %t"
termcapinfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen (not title yet)\007'

# Might need this:
termcapinfo  * '' 'hs:ts=\E_:fs=\E\\:ds=\E_\E\\'

3 responses to 'Find that lost screen session, episode 2.'

Showing last 3 comments... (Click here to view all comments)

Dan Fitch wrote at Fri Jun 6 05:36:41 2008...
Cool stuff.

What I do to make this work for screen sessions that are running over ssh from elsewhere is:

defhstatus ">^E0` ^Ew"
backtick 0 60 60 screentitle

(screentitle is a script which figures out what to "name" the current session... hostname, or hostname+pid)

Then there's a long hairy Ratpoison ruby script that searches for ">hostname" in the title, parses the "w" hardstatus output and ends with:

if window and window.screen_session
  screen = window.screen_session.detect { |x| regex.match x.title }
  exit 1 if window == current_window and screen.number == window.screen_session.number
  Ratpoison::ScreenSession.select screen.number if screen
end


This lets me have a "type to select window" that can cycle through xterms AND individual screen sub-windows, and also quick selects for things like "irssi on host X", "vim on host Y", etc...

Jordan Sissel wrote at Fri Jun 6 08:06:34 2008...
my .zshrc has this, which is run every time a command is run (or the prompt is echoed):

print -Pn "\ek${value}\e\\"  # screen title (in windowlist)
print -Pn "\e_${location}\e\\"  # screen location

'value' is given as the command or the prompt value. The 'location' is automatically set with the hostname of the machine. My xterm title, then becomes "[0] {hostname} - {command}"

So, if I have two machines, 'foo' and 'bar'. Foo is my workstation, bar is a remote server I ssh to. If I run screen locally and get a shell, the title is '[0] foo - zsh'. If I ssh to 'bar' and get a shell, my prompt changes to '[0] bar - zsh' assuming the same .zshrc file is present.

This lets me query for terminals to remote machines.

Matt Simmons wrote at Fri Jun 6 11:32:24 2008...
One thing that I really like about screen, and I didn't realize until I accidentally tried it, was that you don't have to give the full name of the screen session to restore. You only have to give as much as will uniquely identify it.

I'm sure you probably knew that, but I was clueless about it, so maybe it will help someone else.


Leave a reply

You need javascript enabled to use this form. Anti-spam efforts ongoing. Also, if the comment doesn't show up, it's because the form expired. Go back and copy your comment, reload the form, and resubmit. Apologies if this is a hassle, I'm just playing with antispam methods right now. If this insists on not working, please email me about it.

Name (required)
E-mail (optional, if you want me to be able to email you back)
URL (also optional)
Comment: