├── README.md └── tmux-sessionizer /README.md: -------------------------------------------------------------------------------- 1 | ## tmux sessionizer 2 | its a script that does everything awesome at all times 3 | 4 | ## Requirements 5 | fzf and tmux 6 | 7 | ## Usage 8 | ```bash 9 | tmux-sessionizer [] 10 | ``` 11 | 12 | if you execute tmux-sessionizer without any parameters it will FZF set of default directories or ones specified in config file. 13 | -------------------------------------------------------------------------------- /tmux-sessionizer: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/tmux-sessionizer" 3 | CONFIG_FILE="$CONFIG_DIR/tmux-sessionizer.conf" 4 | 5 | # config file example 6 | # ------------------------ 7 | # # file: ~/.config/tmux-sessionizer/tmux-sessionizer.conf 8 | # # If set this override the default TS_SEARCH_PATHS (~/ ~/personal ~/personal/dev/env/.config) 9 | # TS_SEARCH_PATHS=(~/) 10 | # # If set this add additional search paths to the default TS_SEARCH_PATHS 11 | # # The number prefix is the depth for the Path [OPTIONAL] 12 | # TS_EXTRA_SEARCH_PATHS=(~/ghq:3 ~/Git:3 ~/.config:2) 13 | # # if set this override the TS_MAX_DEPTH (1) 14 | # TS_MAX_DEPTH=2 15 | # ------------------------ 16 | 17 | # test if the config file exists 18 | if [[ -f "$CONFIG_FILE" ]]; then 19 | # shellcheck source=/dev/null 20 | source "$CONFIG_FILE" 21 | fi 22 | 23 | sanity_check() { 24 | if ! command -v tmux &>/dev/null; then 25 | echo "tmux is not installed. Please install it first." 26 | exit 1 27 | fi 28 | 29 | if ! command -v fzf &>/dev/null; then 30 | echo "fzf is not installed. Please install it first." 31 | exit 1 32 | fi 33 | } 34 | 35 | switch_to() { 36 | if [[ -z $TMUX ]]; then 37 | tmux attach-session -t "$1" 38 | else 39 | tmux switch-client -t "$1" 40 | fi 41 | } 42 | 43 | has_session() { 44 | tmux list-sessions | grep -q "^$1:" 45 | } 46 | 47 | hydrate() { 48 | if [ -f "$2/.tmux-sessionizer" ]; then 49 | tmux send-keys -t "$1" "source $2/.tmux-sessionizer" c-M 50 | elif [ -f "$HOME/.tmux-sessionizer" ]; then 51 | tmux send-keys -t "$1" "source $HOME/.tmux-sessionizer" c-M 52 | fi 53 | } 54 | 55 | sanity_check 56 | 57 | # if TS_SEARCH_PATHS is not set use default 58 | [[ -n "$TS_SEARCH_PATHS" ]] || TS_SEARCH_PATHS=(~/ ~/personal ~/personal/dev/env/.config) 59 | 60 | # Add any extra search paths to the TS_SEARCH_PATHS array 61 | # e.g : EXTRA_SEARCH_PATHS=("$HOME/extra1:4" "$HOME/extra2") 62 | # note : Path can be suffixed with :number to limit or extend the depth of the search for the Path 63 | 64 | if [[ ${#TS_EXTRA_SEARCH_PATHS[@]} -gt 0 ]]; then 65 | TS_SEARCH_PATHS+=("${TS_EXTRA_SEARCH_PATHS[@]}") 66 | fi 67 | 68 | # utility function to find directories 69 | find_dirs() { 70 | # list TMUX sessions 71 | if [[ -n "${TMUX}" ]]; then 72 | current_session=$(tmux display-message -p '#S') 73 | tmux list-sessions -F "[TMUX] #{session_name}" 2>/dev/null | grep -vFx "[TMUX] $current_session" 74 | else 75 | tmux list-sessions -F "[TMUX] #{session_name}" 2>/dev/null 76 | fi 77 | 78 | # note: TS_SEARCH_PATHS is an array of paths to search for directories 79 | # if the path ends with :number, it will search for directories with a max depth of number ;) 80 | # if there is no number, it will search for directories with a max depth defined by TS_MAX_DEPTH or 1 if not set 81 | for entry in "${TS_SEARCH_PATHS[@]}"; do 82 | # Check if entry as :number as suffix then adapt the maxdepth parameter 83 | if [[ "$entry" =~ ^([^:]+):([0-9]+)$ ]]; then 84 | path="${BASH_REMATCH[1]}" 85 | depth="${BASH_REMATCH[2]}" 86 | else 87 | path="$entry" 88 | fi 89 | 90 | [[ -d "$path" ]] && find "$path" -mindepth 1 -maxdepth "${depth:-${TS_MAX_DEPTH:-1}}" -path '*/.git' -prune -o -type d -print 91 | done 92 | } 93 | 94 | if [[ $# -eq 1 ]]; then 95 | selected="$1" 96 | else 97 | selected=$(find_dirs | fzf) 98 | fi 99 | 100 | if [[ -z $selected ]]; then 101 | exit 0 102 | fi 103 | 104 | if [[ "$selected" =~ ^\[TMUX\]\ (.+)$ ]]; then 105 | selected="${BASH_REMATCH[1]}" 106 | fi 107 | 108 | selected_name=$(basename "$selected" | tr . _) 109 | tmux_running=$(pgrep tmux) 110 | 111 | if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then 112 | tmux new-session -ds "$selected_name" -c "$selected" 113 | hydrate "$selected_name" "$selected" 114 | fi 115 | 116 | if ! has_session "$selected_name"; then 117 | tmux new-session -ds "$selected_name" -c "$selected" 118 | hydrate "$selected_name" "$selected" 119 | fi 120 | 121 | switch_to "$selected_name" 122 | --------------------------------------------------------------------------------