Skip to content

Instantly share code, notes, and snippets.

@kwellman
Created October 11, 2010 03:44
Show Gist options
  • Save kwellman/619941 to your computer and use it in GitHub Desktop.
Save kwellman/619941 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
"""Simple script to split the terminal window into regions using GNU Screen and
automatically run commands in the new regions.
"""
import os, sys
import optparse
parser = optparse.OptionParser(usage='splitscr.py [options] <command> ...')
parser.add_option('-s', '--newscreen', action='store_true', dest='new_screen', help='Create a new screen session for the splits')
parser.add_option('-v', '--vsplit', action='store_true', dest='vertical_split', help='Split vertically instead of horizontally')
options, args = parser.parse_args()
if not args:
parser.error('nothing to do: no commands specified')
if options.new_screen:
# start a nested screen
os.system('screen -S nested %s "%s"' % (sys.argv[0], '" "'.join(args)))
sys.exit()
for i in range(len(args)-1):
# split the screen into windows
if options.vertical_split:
os.system('screen -X split -v')
else:
os.system('screen -X split')
for i, arg in enumerate(args):
if not i == 0:
# set the input focus
os.system('screen -X focus')
# give the window a title and id
os.system('screen -t split%s %s' % (i, i+20))
# execute the command in the window
os.system('screen -X -p %s stuff "%s\n"' % (i+20, arg))
if not i == 0:
# select the next window
os.system('screen -X select %s' % (i+20))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment