Quickly switching between virtual sessions (screen?)

19

4

I'd like to have few virtual session in one putty window and change between them quickly. What's the best way to do that? screen? I know I can detach with ctrl+ad and then reattach to a different session, but it's seems like a lot of typing.

zzz

Posted 2012-09-18T19:11:11.587

Reputation: 279

Answers

22

Just to expand on what Ansgar said in case you aren't very familiar with gnu screen, Having multiple "windows" in a single screen session will likely accomplish what you need.

ctrl+a, c will create a new "window" in your active screen session. You can switch between multiple windows (as Ansgar indicated) with ctrl+a, n for the next window, and ctrl+a,p for the previous window.

ctrl+a," will give you a list of all your open windows.

Using these easy shortcuts will let you switch between different terminals very quickly and easily, accomplishing your goal without using multiple screen sessions.

If you add the following line to ~/.screenrc it will always display your open windows and which one is selected, so you don't need to keep track of which window you're on by remembering.

hardstatus alwayslastline '%{= kg}[ %{G}%H %{g}][%= %{= kB}%?%-Lw%?%{+b r}(%{G}%n*%f %t%?(%u)%?%{r})%{-b B}%?%+Lw%?%?%= %{g}%]'

This is the hardstatus line that I like to use, but it's completely customizable (see man page or search google for "gnu screen hardstatus").

There are a ton of other things you can do with it (naming the different open windows for example), so it's worth reading some tutorials.

Here are a couple I found with a quick search:

http://news.softpedia.com/news/GNU-Screen-Tutorial-44274.shtml

http://www.ibm.com/developerworks/aix/library/au-gnu_screen/

And of course the most useful of all:

# From your terminal:
$ man screen

Hope that helps.

Matthew

Matthew

Posted 2012-09-18T19:11:11.587

Reputation: 966

2

Yes, screen is most likely the way to go. Ctrl+a,n will take you to the next window, Ctrl+a,p to the previous one. Ctrl+a,0..9 will allow you to switch to a particular screen.

Ansgar Wiechers

Posted 2012-09-18T19:11:11.587

Reputation: 4 860

You can set up much faster keybindings, like F11 for prev, F12 for next window. And change the screen escape character from ^a to something else, so it's not ridiculously annoying to use emacs-style line editting in bash, anything else. I use ^t. – Peter Cordes – 2015-02-06T13:24:13.823

1

You can do this with a simple shell script like

    for s in `screen -ls | grep Detached | grep <regex to your screen names>`; do screen -r $s; done 
  1. Lets assume you have few screens open. Lets also assume you have them categorised as work and data.

    for i in {10..13}; do screen -S `printf data%02d $i`; done;
    for i in {0..5}; do screen -S `printf work%02d $i`; done;
    ...
    
  2. Applying screen -ls, you should have your screens like this

    There are screens on:
        81592.data13    (Detached)
        81539.work04    (Detached)
        81527.work02    (Detached)
        81586.data12    (Detached)
        81574.data10    (Detached)
        81533.work03    (Detached)
        81488.work00    (Detached)
        81607.ttys014.Mo    (Attached)
        81545.work05    (Detached)
        81580.data11    (Detached)
        81521.work01    (Detached)
        81515.work00    (Detached)
    12 Sockets in /var/folders/qs/cvlbhpr54fnb96vtx01bs9k00000gn/T/.screen.
    
  3. Now lets isolate the detached screens using grep.

    screen -ls | grep Detached
    

You should get something like this

        81592.data13    (Detached)
        81539.work04    (Detached)
        81527.work02    (Detached)
        81586.data12    (Detached)
        81574.data10    (Detached)
        81533.work03    (Detached)
        81488.work00    (Detached)
        81545.work05    (Detached)
        81580.data11    (Detached)
        81521.work01    (Detached)
        81515.work00    (Detached)
  1. Now lets select the ones you want (e.g. data), again using grep.

    screen -ls | grep Detached | grep data
    

This will isolate only the data screens. You can change the grep keyword with regular expressions to search to customise your search.

        81592.data13    (Detached)
        81586.data12    (Detached)
        81574.data10    (Detached)
        81580.data11    (Detached)
  1. Luckily the result is well structured and tab delimited. Now lets split the columns into fields and choose the 2nd field using cut.

    screen -ls | grep Detached | grep data | cut -d'       ' -f2
    

You will need to press ctrl+v then tab to insert a tab after the delimiter parameter -d. The results should be like this.

81592.data13
81586.data12
81574.data10
81580.data11
  1. Now pack this into a command using the ... quotes and loop over the results like this. Always check the formulated commands 1st before actually running it. I used echo screen -r

    for s in `screen -ls | \
    grep Detached | grep data | \
    cut -d'     ' -f2`; do \
        echo screen -r $s; \
    done
    
  2. If all goes good, then try it without echo.

    for s in `screen -ls | \
    grep Detached | grep data | \
    cut -d'     ' -f2`; do \
        screen -r $s; \
    done
    
  3. Voila. Each screen you detach from takes you to the next one.

Happy screening!!

Mo Hossny

Posted 2012-09-18T19:11:11.587

Reputation: 119

This is a REALLY nice answer and it raises the overall quality of this site. – 123456789123456789123456789 – 2017-11-30T01:00:50.380

1

I wrote a node script to allow user to select screen session to attach to from a list, using arrow keys. I’ve made it an npm package. You can check it out here: screen-command. Or just npm i -g screen-command to try it out, assuming you have node.js installed already. After installing the package, use command sc to summon the list of screens.

I hope this makes switching to the exact screen you want easier.

hi94740

Posted 2012-09-18T19:11:11.587

Reputation: 11

1

What the others say is correct. I just want to mention tmux is an alternative to screen. They have very similar commands.

Both screen and tmux offer multiple windows for a running session, and this is probably what you want. tmux additionally offers jumping between multiple sessions (each having their own windows). You'd need that very rarely.

Here are some links to the documentation of screen and a crash course about tmux.

tkruse

Posted 2012-09-18T19:11:11.587

Reputation: 111

1

After so much time I made a Script to work around this completely missing feature. First of all it needs a fifo:

mkdir ~/.screen
mkfifo ~/.screen/pipe

This named pipe is useful for the communication between the detatched session and the "Main-without-screen" session.

File sc ( in $PATH ):

#!/bin/bash
CONFIGFILE=~/.screen/"$1""rc"

if [ ! -r $CONFIGFILE ] ; then
   echo "Configurazione per $1 Assente" >&2
   exit 1
fi

exec 3<> ~/.screen/pipe

if [ "$STY" != "" ] ; then
   screen -d "$STY"
   echo "$1" >&3
else
   screen -r "$1" 2>/dev/null || screen -S "$1" -c $CONFIGFILE
   while true ; do
      read line <&3
      screen -r "$line" 2>/dev/null || screen -S "$line" -c ~/.screen/"$line""rc"
   done
fi

An Example of a "CONFIGFILE" is: ~/.screen/Monitorrc

layout new Monitor

screen -t "bash" bash --login
split -v
focus next
split 
focus bottom
screen -t "cv" sh -c "watch /usr/bin/cv -w"
split
focus bottom
screen -t "sys.log" sh -c "tail -F /var/log/sys.log"
focus up
focus up
resize 25
screen -t "top" sh -c "top"
focus left

The result is: when you want to launch screen type:

sc Monitor

or another sessionrc you like to invent, I use the Session Work for various stuff

Ex: ~/.screen/Workrc

layout new Work  
screen -t "bash" bash --login  

Now we are in the Session Monitor, when we type:

sc Work

the session Monitor detaches itself and write "Work" to the namedpipe. Consequently the first sc script goes forward and attach the session "Work".
The sc invoked from the session Monitor close.

When we detach all Sessions we are in a infinite loop so we have to do Ctrl-c to exit.

Thomas8

Posted 2012-09-18T19:11:11.587

Reputation: 113