How to persist `gnu screen` layout after restart

12

5

I am using the screen split in 3 with vim on the left, and two other screens split horizontally on the right. I know that when detaching from a screen, I can save the layout with :layout save my_layout_name.

I already have layout autosave on in my .screenrc file, but that doesn't help with my issue.

How can I persist a screen's layout between different terminal sessions, and after pc restart? Or maybe as a workaround, is there a way to run screen (parameters) which split it, set up the bash on every window and save the layout?

randunel

Posted 2013-12-12T09:45:25.690

Reputation: 223

Answers

12

Default layout for new screen's

Manage your regions, then execute code inside screen:

  • To save your current layout to file .screen_layout:

    rm -f $HOME/.screen_layout
    screen -X layout dump .screen_layout
    
  • To make it auto-loading with layout saving:

    echo source .screen_layout  >> $HOME/.screenrc
    echo layout save def >> $HOME/.screenrc
    

How it works

  1. You type screen.
  2. Screen reads $HOME/.screenrc:
  3. source .screen_layout - read and execute commands from $HOME/.screen_layout
    Regions now created.
  4. layout save def - save current regions as layout to keep it when you detach.

layout dump .screen_layout

  • Dump (append) current layout to file in $HOME directory

screen -X command

  • Run command in current screen session (if executed inside).
    Similar to ctrla then : command

You may also

  • Disable copyright notice at startup:

    echo startup_message off >> $HOME/.screenrc
  • Increase scroll buffer(def. 100)

    echo defscrollback 1000 >> $HOME/.screenrc
  • Write own .screen_layout (50% / 50% vertical, with 2 shells):

    split -v
    screen -t s1
    
    focus
    screen -t s2
    
    focus
    

befzz

Posted 2013-12-12T09:45:25.690

Reputation: 331

You are correct. This method persists the screen layout by dumping the commands to a file, then repeating them in .screenrc. The .screen_layout file can be edited with all the commands to be run each time screen is run, including layout save default in the end. I suggest other readers to add screen after focus in the .screen_layout file to get bash running. – randunel – 2015-08-26T21:20:27.780

6

Ctrl-a then : then layout save default Provided screen is newer version (with layout feature) Good luck.

UPDATE: Here is the .screenrc file that will make the change permanent, along with adding other nice features.

Mir Dunaev

Posted 2013-12-12T09:45:25.690

Reputation: 61

any idea why the layout autosave on in the .screenrc didn't work? That would be preferable to me. – spinlock – 2015-07-23T18:29:19.640

You are right, it doesn't work. But I have tested .screenrc from here: https://gist.github.com/joaopizani/2718397 and it WORKS.

– Mir Dunaev – 2015-08-25T10:16:04.747

2

"layout autosave on" works only for explicitly named layouts in .screenrc.

Namely, if you specified some layout as ABC, switched to it within Screen session and detached, this last layout will be restored after the session is resumed.

If you did not specify any layout in the configuration file, you have to run internal command "layout save your_layout_name" before detaching.

This is a .screenrc with predefined layouts to choose from:

 0 — one (the only region)
 1 — two-v (the screen is splitted vertically into two regions, default layout)
 2 — two-h (the screen is splitted horizontally into two regions)
 3 — three (three regions — two in the upper part of the screen and one underneath)
 4 — four (four regions)

Predefined layouts

Once selected from within the Screen these layouts are preserved upon detachment until the next resuming of the session (due to "layout autosave on" command).

If you've built your own layout it must be saved with "layout dump filename" command. Then you can use a code from "filename" file to add your layout in #layouts section of this .screenrc to select from.

By default "two-v" layout is loaded with the only window with bash.

Oleg Bolden

Posted 2013-12-12T09:45:25.690

Reputation: 1 507

This is great, though I missed how to switch from one layout to another, then saw the comment that key binding was removed. Just fyi - this keybinding works for me (Ubuntu 16.04) to switch layouts: bind ' ' layout next # <- actually means Ctrl-a + Space – Bachi – 2018-01-24T09:49:59.427

0

According to this source it is not possible due to the way screen works. There is a hack provided there, however. It basically boils down to nesting screen inside of another screen, which might not be very convenient. (Answer found here)

If you use screen for terminal splitting on your local machine (which I infer from the pc restart mentioned), you might want to check out the terminal emulator called terminator. It supports horizontal and vertical terminal splits (nested as well) and you can save your favourite layout for further use. Then you simply run terminator with an appropriate option: terminator -l <name of saved layout>.

Erathiel

Posted 2013-12-12T09:45:25.690

Reputation: 434

I am aware of nesting a screen inside another screen, but all the screens get terminated on restart. The screen inside a screen was resolved when layout was added, so your answer is both besides the question and outdated. I appreciate the terminator suggestion though. – randunel – 2013-12-12T13:09:00.700