How do I start screen with multiple splits directly from the command line?

7,844

Solution 1

For the specific case of window arrangements, there's a screen command to save them to a file: layout dump. From man screen:

layout dump [filename]

Write to a file the order of splits made in the current layout. This is
useful to recreate the order of  your  regions  used  in  your  current
layout.  Only  the  current  layout is recorded. While the order of the
regions are recorded, the sizes of  those  regions  and  which  windows
correspond  to  which regions are not. If no filename is specified, the
default is layout-dump, saved in the directory that the screen  process
was  started in. If the file already exists, layout dump will append to
that file. As an example:

           C-a : layout dump /home/user/.screenrc

will save or append the layout to the user's .screenrc file.

So, once you make the arrangement manually, press Ctrla:, then type layout dump /path/to/some/file. The layout will be saved to /path/to/some/file and you can then restore it in a new session with:

screen -c /path/to/some/file

Solution 2

I came up with the following to create the output shown in my question and following @muru's excellent answer. Using layout dump gave me the following:

split
focus
split -v
focus

Note: Tilde (~) expansion does not work with layout dump so instead of ~/layout.dmp for example you would need to use /home/<username>/layout.dmp.

From which I then created the following .screenrc

# create the top screen
chdir /home/server/log
screen -t "Apache Log" tail -n 1 -f access.log
# split the screen and focus onto the new created space
split
focus
#create the bash
chdir /home/server/log
screen
# split vertically and focus onto the new area
split -v
focus
# create the htop screen
screen -t "Htop" htop
# focus twice to end up with the bash area active
focus
focus

Now I only need to type screen and get my wanted layout started. I leave that here as an example for those who are wondering, but don't forget to up-vote @muru's answer, since he is the one who made me able to solve this.

Share:
7,844

Related videos on Youtube

Videonauth
Author by

Videonauth

Updated on September 18, 2022

Comments

  • Videonauth
    Videonauth over 1 year

    I'm using screen after I have logged in with ssh to my server. As of now I set up the splits in my screen window by hand and run the commands by hand as shown in the following screen-shot:

    enter image description here

    • The top part should run tail -n 1 -f /home/server/log/access.log.
    • The lower right part should run htop
    • The lower left one should simply be a command prompt

    Is there any way to have that be done via commands/script, so I not have to redo it every time by hand?

    • muru
      muru over 6 years
    • Videonauth
      Videonauth over 6 years
      @muru This quite answers it for the part how to start the separate screens, just not the part how to have them directly ordered like above shown.
    • muru
      muru over 6 years
      Everything that can be done using shortcuts in screen can be done using commands in .screenrc. For this arrangement we have the split command.
  • Videonauth
    Videonauth over 6 years
    +1 nice one; The split -v seems undocumented :) this is why I was struggling.
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 6 years
    @Videonauth it actually is documented under default keybindings section. C-a | (split -v) Split the current region vertically into two new ones.
  • muru
    muru over 6 years
    Yes, it looks it's only mentioned in the manpage, next to C-a |, but not in the GNU docs
  • Videonauth
    Videonauth over 6 years
    Found an odd behavior you might want to mention: If you type for example layout dump ~/layout the process will fail, it need to full path (i.e. /home/$USER/layout)
  • muru
    muru over 6 years
    @Videonauth since tilde expansion is usually done by the shell, it's not surprising if a given command doesn't support it internally. Some do, most don't.
  • dojuba
    dojuba over 5 years
    I can't dump the layout from a detached session by following: screen -r SESSION_NAME -X layout dump FILE_NAME. Other commands work fine with -X.