How to keep windows split after detaching/reattaching GNU screen

I use screen in a Ubuntu server.

I have my screen splited in four regions each showing a different window.

The problem is when i detach (or maybe reattach) a get the four windows but only one region and i have to make C-a S all over again?

How can i keep my screen splitted when a reattach?


Solution 1:

From the screen FAQ:

(The implied question being, “How do I keep my split windows over a detach?”)

The short is answer is that you can't. The longer answer is that you can fake it.

Splits are a property of your display. The process managing your screen session doesn't really know about them; only the single process that's displaying the session does. Thus, the screen session can't remember the splits because it doesn't know about them, and once you detach, the process that did know about them has exited.

The hack is to use nested screen sessions. Start one session and give it some escape sequence that you won't use much (or just disable its escape character completely). Bind your usual detach key sequence to this screen session. Now, start or attach to your main screen session. All of your work will be done in the inner session, and you can split your display. When you detach, however, it will be the outer session that detaches, so your splits in the inner session will be preserved.

Assuming you use the default escape character, C-a, your alternate screenrc should contain:

escape ""
bindkey ^ad detach

Solution 2:

I found that if you already have some windows set up and you want to save them you can do this:

Ctrl-a
:
layout save default

Then when you detach and re-attach to the session the layout should be restored.

Works with Screen version 4.00.03jw4 (FAU) 2-May-06 (Ubuntu 12.04 & 12.10)

Also you may want to create a deamon session as such:

$> screen -dmSU mysession

and then have it always available by calling:

$> screen -r mysession

Solution 3:

Simply add this line to your ~/.screenrc:

# This line makes Detach and Re-attach without losing the regions/windows layout
layout save default

Solution 4:

In git version, new feature layout was introduced.

from screen(1):

layout new [title]

Create a new layout. The screen will change to one whole region and be switched to the blank window. From here, you build the regions and the windows they show as you desire. The new layout will be numbered with the smallest available integer, starting with zero. You can optionally give a title to your new layout. Otherwise, it will have a default title of "layout". You can always change the title later by using the command layout title.

even if you detached screen session, layout has remember how region was split.

for example, screen start with follow configuration.

layout new
split
split -v
screen 0
focus next
screen 1
focus bottom
screen 2
detach

And reattach this session, you will able to come back into (last selected) layout which has 3 splitted region.