Scripts and .screenrc to make GNU Screen splits easier

Update Thursday, 3rd of August 2017, 11:04:30 AM: much better to use bind over stuff to run a script within the .screenrc file.

I enjoy GNU screen via Cygwin on Windows a lot, but some of the commands get a bit fiddly. Creating splits is like that: you create a split, then move to it, then assign a window to it.

My first set of shortcuts were to create scripts to do those jobs.

To make a vertical split:

#!/bin/bash
# Manual way create a vertical split in GNU screen:
# C-a | # Create a vertical split
# C-a <Tab> # Move to the split
# C-a c # Create a new window within the split
# # OR
# C-a x # Assign an existing window by number to the split.
# This script send commands to a screen session to
# 1) create the split
# 2) focus on it
# 3) assign the first window to it.
# If you name your screen session, use "screen -S ScreenName".
screen -X split -v
screen -X focus right
screen -X select 1

To make a horizontal script:

#!/bin/bash
# Manual way create a horizontal split in GNU screen:
# C-a S # Create a horizontal split
# C-a <Tab> # Move to the split
# C-a c # Create a new window within the split
# # OR
# C-a x # Assign an existing window by number to the split.
# Save a bit of manual work with a script!
# This script send commands to a screen session to
# 1) create the split
# 2) focus on it
# 3) assign the first window to it.
# If you name your screen session, use "screen -S ScreenName".
screen -X split
screen -X focus down
screen -X select 1

Lastly, key bindings that go in my .screenrc file to run those scripts.

# ------------------------------
# SPLIT HORIZONTALLY OR VERTICALLY.
# ------------------------------
# Uses split scripts.
# Control+a, V for vertical; control+a, H for horizontal.
# bind V stuff 'screenSplitVertical'\012''
# bind H stuff 'screenSplitHorizontal'\012''
bind V exec $HOME/bin/screenSplitVertical.sh
bind H exec $HOME/bin/screenSplitHorizontal.sh

Explanation for version that uses exec:

  1. bind V says the rest of this line will be executed upon "control+a, shift+v"
  2. exec means run the rest of the line as a command.
  3. $HOME/bin/screenSplitVertical.sh is the full path to the command (script) I want to run.

Explanation for version that uses stuff:

  1. bind V says the rest of this line will be executed upon "control+a, shift+v"
  2. stuff will "write stuff to the command line" or "stuff text onto the command line".
  3. 'screenSplitVertical'\012'' will write out the command screenSplitVertical.sh and then '\012' which outputs a newline (ENTER key) to actually cause the script to run.

In general, exec is more suitable than stuff in this situation because stuff will leave the command in my command history.

More screen magic? Check out joaopizani's .screenrc config for some great ideas about resizing splits and moving between them.

Popular Posts