How to keep windows split after detaching/reattaching GNU screen

35

29

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?

Cesar

Posted 2009-11-12T23:37:08.140

Reputation: 475

Answers

15

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

Darren Hall

Posted 2009-11-12T23:37:08.140

Reputation: 6 354

See https://superuser.com/a/516822/113250 for the "new" built-in layout feature

– Thiago Figueiro – 2019-01-20T23:53:40.640

1Having spent the past year or two configuring and heavily using nested screen sessions, I recommend it. Over time, the pain of added complexity decreases, while the usefulness of the more flexible setup increases. – funroll – 2012-08-30T18:51:48.180

14The new feature, i.e., layout, invalidates this answer! – Kadir – 2013-11-19T14:21:43.110

68

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

Troy

Posted 2009-11-12T23:37:08.140

Reputation: 681

5Much better solution. This should be the accepted answer. (I know why I always scroll down XD) – BrainStone – 2015-04-13T21:32:00.677

1Unfortunately it looks like the current version on redhat screen 4.00.03 (FAU) 23-Oct-06 does not have this feature. But it should! – David Dombrowsky – 2017-05-02T16:13:04.990

This is a better answer, but there is an even better answer below which automatically saves the layout every time using .screenrc. Upvote that answer – Mark Lakata – 2019-08-29T16:38:16.403

14

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.

yoshikaw

Posted 2009-11-12T23:37:08.140

Reputation: 1 286

anyone knows what submissions are required to get this (i'n nerdy enough to apply patches but i will never run trunk code(have bad karma :P)) – borrel – 2012-08-23T07:05:34.797

8

Simply add this line to your ~/.screenrc:

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

Rafael Quirino Castro

Posted 2009-11-12T23:37:08.140

Reputation: 431

6

It seems that tmux does. This is a new multiplexer project, inspired by screen, but coded from scratch and under a BSD license.

(Sorry if my English is uncomfortable to read :-°)

Pantoufle

Posted 2009-11-12T23:37:08.140

Reputation: 69

-1

I've read the whole screen man recently, and there's no common way to do it if screen itself can't remember it automatically.

kolypto

Posted 2009-11-12T23:37:08.140

Reputation: 2 861