├── readme.md └── tat.sh /readme.md: -------------------------------------------------------------------------------- 1 | TAT(1) 2 | 3 | ## Name 4 | tat - tmux attach utility 5 | 6 | ## Synopsis 7 | 8 | **tat [session_name | new_session_name]** 9 | 10 | ## Description 11 | 12 | Tab completion for tmux sessions. 13 | 14 | Quickly open new tmux sessions in your project's dir. 15 | 16 | ## Setup 17 | 18 | ```bash 19 | git clone git://github.com/ryandotsmith/tat.git ~/path/to/tat 20 | echo "source ~/path/to/tat/tat.sh" >> ~/.bashrc 21 | echo "export CODE_ROOT_DIRS=\"/home/you/code\"" >> ~/.bashrc 22 | echo "export CODE_ROOT_DIRS=\"/home/you/other_code_dir:$CODE_ROOT_DIRS\"" >> ~/.bashrc 23 | ``` 24 | 25 | ## Usage 26 | 27 | Use the tab key to open an existing session. 28 | 29 | ```bash 30 | $ tat [TAB] 31 | open_session_1 open_session_2 32 | ``` 33 | 34 | Arguments that are passed to tat will be used to create a new session. 35 | Tat will open a new tmux session and set the default path to the found dir. 36 | 37 | ```bash 38 | $ tat my_project 39 | $ pwd 40 | /home/you/code/my_project 41 | ``` 42 | 43 | ## Motivation 44 | 45 | There are popular tmux helpers on github. They require ruby and yaml file. No thanks! 46 | It is my opinion that bash is the right tool for the job here. UNIX doesn't care about yaml. 47 | 48 | ## Issues 49 | 50 | tat(1) assumes you have your code in "root" directories. We use find(1) to scan 51 | (with max-depth=1) the directories to find one that matches your argument to 52 | tat(1). 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /tat.sh: -------------------------------------------------------------------------------- 1 | tat() { 2 | local session_name="$1" 3 | local sessions=( $(tmux list-sessions 2>/dev/null | cut -d ":" -f 1 | grep "^$session_name$") ) 4 | 5 | if [ ${#sessions[@]} -gt 0 ]; then 6 | # If there is already a session with the same name, attach to it. 7 | tmux attach-session -t "$session_name" 8 | else 9 | # If there is no existing session, create a new (detached) one. 10 | tmux new-session -d -s "$session_name" 11 | 12 | # Try to find a matching code directory. 13 | local code_root_dirs=$(echo $CODE_ROOT_DIRS | sed 's/:/ /g') 14 | local matching_dirs=( $(find $code_root_dirs -maxdepth 1 -name "$session_name" -type d ) ) 15 | 16 | # If there is a matching directory, set it as the default path and jump into the directory. 17 | if [ ${#matching_dirs[@]} -gt 0 ]; then 18 | local code_dir=${matching_dirs[0]} 19 | tmux set default-path "$code_dir" 1>/dev/null 20 | tmux send-keys -t "$session_name:1" "cd $code_dir && clear" C-m 21 | 22 | # If there is a .tmux file in this directory, execute it. 23 | if [ -f "$code_dir/.tmux" ]; then 24 | eval "$code_dir/.tmux" $session_name 25 | fi 26 | fi 27 | 28 | # Finally, attach to the newly created session. 29 | tmux attach-session -t "$session_name" 30 | fi 31 | } 32 | 33 | _tat() { 34 | COMPREPLY=() 35 | local session="${COMP_WORDS[COMP_CWORD]}" 36 | local code_root_dirs=$(echo $CODE_ROOT_DIRS | sed 's/:/ /g') 37 | 38 | # For autocomplete, use both existing sessions as well as directory names. 39 | local sessions=( $(compgen -W "$(tmux list-sessions 2>/dev/null | awk -F: '{ print $1 }')" -- "$session") ) 40 | local directories=( $( 41 | for dir in $code_root_dirs; do 42 | cd "$dir" 2 >/dev/null && compgen -d -- "$session" 43 | done 44 | ) ) 45 | 46 | COMPREPLY=( ${sessions[@]} ${directories[@]} ) 47 | } 48 | 49 | complete -o filenames -o nospace -F _tat tat 50 | --------------------------------------------------------------------------------