Skip to content

Instantly share code, notes, and snippets.

@cookrn
Last active March 29, 2017 15:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cookrn/2711966 to your computer and use it in GitHub Desktop.
Save cookrn/2711966 to your computer and use it in GitHub Desktop.
Named Screen -- Inspired by @ahoward

Named Screen

A utility inspired by @ahoward that allows screens to be the name of the directory they were spawned from. This is mainly helpful for Terminal/iTerm tabs.

Executing this should rejoin sessions with the same name if they already exist or otherwise create them.

Usage

  1. Place or symlink into your path (I symlink as ns)
  2. Ensure the script is executable
  3. Run named_screen (or ns) from a project directory
  4. Examine your terminal tab, the contents of ~/.screens/, and the output of screen -ls
  5. Use screen as you normally would
#!/usr/bin/env ruby
require 'fileutils'
## Find `screen`
#
screen_bin_path = `which screen`.chomp
if screen_bin_path.nil? || screen_bin_path.empty?
abort('No `screen` detected!')
end
## Make the named screens storage path
#
screens_path = File.expand_path File.join('~', '.screens')
FileUtils.mkdir_p(screens_path)
## Determine the screen name and full path
#
pwd = `pwd`.chomp
screen_name = pwd.split('/')[-1]
named_screen_path = "#{screens_path}/#{screen_name}"
## Hardlink the new named screen
#
unless File.exist?(named_screen_path)
FileUtils.ln(screen_bin_path, named_screen_path)
end
## Go!
#
if `screen -list` !~ /\s\d+\.#{screen_name}\s/i
system(named_screen_path, '-dmS', screen_name)
end
exec(named_screen_path, '-aADRRS', screen_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment