Created
April 27, 2016 08:57
-
-
Save Widdershin/987af5f2b0c530e1ffc6f597e373a325 to your computer and use it in GitHub Desktop.
Switch/open tmux sessions with ease using fzf
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 | |
require 'set' | |
PROJECTS_DIR = '/Users/nickj/Projects/' | |
def tmux_is_running? | |
ENV.include? 'TMUX' | |
end | |
def project_directories | |
# http://stackoverflow.com/a/1899164/1404996 | |
Dir | |
.entries(PROJECTS_DIR) | |
.select {|entry| File.directory?(File.join(PROJECTS_DIR, entry)) && !(entry =='.' || entry == '..') } | |
end | |
def project_already_running?(project) | |
`tmux has-session -t #{project}` | |
$?.success? | |
end | |
def start_session(project) | |
project_path = File.join(PROJECTS_DIR, project) | |
if tmux_is_running? | |
`tmux switch-client -t "$(TMUX= tmux -S "${TMUX%,*,*}" new-session -dP -s "#{project}" -c #{project_path})"` | |
else | |
`tmux new-session -s "#{project}" -c #{project_path}` | |
end | |
end | |
def switch_to(project) | |
`tmux switch -t #{project}` | |
end | |
tmux_sessions = `tmux list-sessions | cut -d: -f1`.split("\n") | |
options = Set.new(tmux_sessions + project_directories) | |
project = `echo "#{options.to_a.join("\n")}" | fzf`.chomp | |
if project_already_running?(project) | |
switch_to(project) | |
else | |
start_session(project) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment