Last active
May 19, 2017 16:09
-
-
Save carlzulauf/0d426aefe8cf0f9637b853cb5e2015c1 to your computer and use it in GitHub Desktop.
Tmux Config and Startup Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# definitely DON'T use Ctrl+b. lame. | |
unbind C-b | |
# I never use the Insert key on the console, but it is a bit of a stretch | |
# set -g prefix IC | |
# ` is a cool prefix | |
set -g prefix ` | |
set -g default-terminal "screen-256color" | |
# this should allow for `` to be a literal ` | |
bind-key ` send-prefix | |
# 1 is easier to reach than 0 | |
set -g base-index 1 | |
# make 0 window 10 | |
bind-key 0 select-window -t ':10' | |
# - moves left, = moves right | |
bind-key - previous-window | |
bind-key = next-window | |
bind-key p delete-buffer | |
bind-key K kill-window | |
bind-key Q kill-session | |
# Why have all this RAM if you're not going to use it? | |
set -g history-limit 12288 | |
# black background, hostname red, date and 24hr time in white, extra space for aesthetics | |
set -g status-right '#[fg=red,bg=black] #H:#(echo $USER) ' | |
set -g status-left '#[fg=green,bg=black] #S:#I ' | |
# check out mouse mode! (scrolling) | |
# set -g mode-mouse on | |
# set -g mouse-resize-pane on | |
# set -g mouse-select-pane on | |
# set -g mouse-select-window on | |
bind m \ | |
set -g mode-mouse on \;\ | |
set -g mouse-resize-pane on \;\ | |
set -g mouse-select-pane on \;\ | |
set -g mouse-select-window on \;\ | |
display 'Mouse: ON' | |
bind M \ | |
set -g mode-mouse off \;\ | |
set -g mouse-resize-pane off \;\ | |
set -g mouse-select-pane off \;\ | |
set -g mouse-select-window off \;\ | |
display 'Mouse: OFF' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Usage: | |
# tproj <- get a list of available projects (configured below) | |
# tproj project-name <- start or resume a specific project | |
class Project | |
attr_reader :abbr, :dir, :options | |
def initialize(abbr:, dir:, **options) | |
@abbr = abbr | |
@dir = dir | |
@options = options | |
end | |
def names | |
options.fetch(:names) { [abbr] } | |
end | |
def windows | |
options.fetch(:windows) { 1 } | |
end | |
def commands | |
options.fetch(:commands) { [] } | |
end | |
def labels | |
options.fetch(:labels) { [] } | |
end | |
def start | |
options.fetch(:start) { false } | |
end | |
def inputs | |
options.fetch(:inputs) { [] } | |
end | |
end | |
def project(**options) | |
Project.new(options) | |
end | |
def rails(**options) | |
defaults = { | |
commands: ["bin/rails s", "bin/rails c"], | |
labels: ["r/s", "r/c"], | |
windows: 4, | |
start: false, | |
} | |
project defaults.merge(options) | |
end | |
projects = [ | |
rails(abbr: "ub", dir: "~/projects/underbee", names: %w(underbee ub), windows: 5, start: true), | |
rails(abbr: "pmgr", dir: "~/projects/n2/pub_manager", windows: 6), | |
rails(abbr: "n2q", dir: "~/projects/n2/n2q"), | |
rails(abbr: "ids", dir: "~/projects/if/ids"), | |
rails(abbr: "omg", dir: "~/projects/omgdots", names: %w(omg omgdots)), | |
project(abbr: "dots", dir: "~/projects/dotfiles", names: %w(dotfiles dots)), | |
] | |
unless ARGV[0] | |
names = projects.map(&:abbr).join(", ") | |
puts "Available projects: #{names}" | |
exit | |
end | |
current = projects.detect { |proj| proj.names.member?(ARGV[0]) } | |
unless current | |
STDERR.puts "Project not found: #{ARGV[0].inspect}" | |
exit 1 | |
end | |
existing_sessions = `tmux list-sessions`.split("\n") | |
match = existing_sessions.detect { |x| x.split(":")[0] == current.abbr } | |
print "\033]0;tmux:#{current.dir}\007" | |
exec "tmux attach -t #{current.abbr}" if match | |
Dir.chdir File.expand_path(current.dir) | |
system "tmux new -s #{current.abbr} -d" | |
delayed = [] | |
1.step(to: current.windows) do |n| | |
cmd, label, keys = current.commands[n - 1], current.labels[n - 1], current.inputs[n - 1] | |
system "tmux new-window -t #{current.abbr}" unless n == 1 | |
if cmd || keys | |
tmux_command = ["tmux", "send-keys", "-t", "#{current.abbr}:#{n}", cmd || keys] | |
tmux_command << "Enter" if cmd && current.start | |
delayed << tmux_command | |
end | |
if label | |
system "tmux", "rename-window", "-t", "#{current.abbr}:#{n}", label | |
end | |
end | |
sleep 0.5 # give bash a chance so prompt is drawn before keys | |
delayed.each { |cmd| system *cmd } | |
system "tmux select-window -t #{current.abbr}:1" | |
exec "tmux attach -t #{current.abbr}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment