Created
November 27, 2011 02:00
-
-
Save ryandotsmith/1396797 to your computer and use it in GitHub Desktop.
tab completion for attaching to tmux sessions
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
# Tab completion for tmux sessions. | |
# Quickly open new tmux sessions in your projects dir. | |
# Setup: | |
# Source this code in your bash shell. | |
# Update the code_dir var with the root directory of your source code. | |
# Usage: | |
# Use the tab to open an existing session. | |
# $ tat [TAB] | |
# Arguments that are passed to tat will be used to create a new session. | |
# Tat will open a new tmux session and set the default path to the found dir. | |
# $ tat payments | |
# $ pwd | |
# /code_dir/payments | |
tat() | |
{ | |
local session_name="$1" | |
tmux attach-session -t "$session_name" | |
if [ $? -ne 0 ]; then | |
local code_dir="/home/ryandotsmith/src" | |
local list_of_dirs=( $(find "$code_dir" -name "$session_name" -type d ) ) | |
local first_found="${dirs[0]}" | |
cd "$first_found" | |
echo "tat() is creating new tmux session with name=$session_name" | |
tmux new-session -d -s "$session_name" | |
echo "tat() is setting default path with dir=$first_found" | |
tmux set default-path "$first_found" | |
tmux attach-session -t "$session_name" | |
fi | |
} | |
_tat() | |
{ | |
COMPREPLY=() | |
local session="${COMP_WORDS[COMP_CWORD]}" | |
COMPREPLY=( $(compgen -W "$(tmux list-sessions 2>/dev/null | awk -F: '{ print $1 }')" -- "$session") ) | |
} | |
complete -F _tat tat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment