diff --git a/modules/home-manager/cli/tmux/default.nix b/modules/home-manager/cli/tmux/default.nix index a1c329a..d49b1d5 100644 --- a/modules/home-manager/cli/tmux/default.nix +++ b/modules/home-manager/cli/tmux/default.nix @@ -45,7 +45,21 @@ in bind -n M-n split-window -h -c "#{pane_current_path}" bind -n M-N split-window -v -c "#{pane_current_path}" - bind -n M-0 run 'create-tmux-session -s config -p nix "run:nvim ." vsplit' + # define sessions with Alt+F{1..4} for general purpose sessions + bind -n M-F1 if 'tmux has-session -t 1' {switch-client -t 1} {display-popup -E -E 'create-tmux-session -i 1'} + bind -n M-F2 if 'tmux has-session -t 2' {switch-client -t 2} {display-popup -E -E 'create-tmux-session -i 2'} + bind -n M-F3 if 'tmux has-session -t 3' {switch-client -t 3} {display-popup -E -E 'create-tmux-session -i 3'} + bind -n M-F4 if 'tmux has-session -t 4' {switch-client -t 4} {display-popup -E -E 'create-tmux-session -i 4'} + + # Alt+F10 for launching my Nix project + bind -n M-F10 run 'create-tmux-session -n config -p nix "run:nvim ." vsplit:20' + + # change window with Alt+{1..5} + bind -n -N "Goto window 1" M-1 select-window -T -t 1 + bind -n -N "Goto window 2" M-2 select-window -T -t 2 + bind -n -N "Goto window 3" M-3 select-window -T -t 3 + bind -n -N "Goto window 4" M-4 select-window -T -t 4 + bind -n -N "Goto window 5" M-5 select-window -T -t 5 # Theme set -g status-interval 2 diff --git a/modules/home-manager/cli/tmux/files/create-tmux-session.sh b/modules/home-manager/cli/tmux/files/create-tmux-session.sh index c153ac7..4e242eb 100755 --- a/modules/home-manager/cli/tmux/files/create-tmux-session.sh +++ b/modules/home-manager/cli/tmux/files/create-tmux-session.sh @@ -22,8 +22,12 @@ debug() { process_args() { until [[ -z $1 ]]; do case $1 in - -s | --session-name | --session) - SESSION="$2" + -i | --session-id | --id) + SESSION_ID="$2" + shift + ;; + -n | --session-name | --name) + SESSION_NAME="$2" shift ;; -p | --project-name | --window) @@ -42,8 +46,7 @@ process_args() { } has_session() { - local session_name="${1}" - tmux has-session -t "${session_name}" 2>/dev/null + tmux has-session -t "${SESSION_NAME:-$SESSION_ID}" 2>/dev/null } get_project_path() { @@ -66,13 +69,14 @@ is_running_in_tmux() { check_script_inputs() { debug "check script inputs" - if [[ -z "$SESSION" ]]; then - error "You must provide a session name" + if [[ -z "$SESSION_ID" ]] && [[ -z "$SESSION_NAME" ]]; then + error "You must provide session ID or a session name" exit 5 fi + SESSION_NAME=${SESSION_NAME:-$SESSION_ID} } -process_commands() { +process_command() { local input="$1" debug "Process command '$input'" local cmd arg @@ -92,19 +96,19 @@ process_commands() { tmux_vsplit() { local size="${1:-20}" debug "execute vsplit with size '${size}'" - tmux split-window -t "$SESSION" -c "$CURRENT_PATH" -v -p "$size" + tmux split-window -t "${SESSION_NAME}" -c "$CURRENT_PATH" -v -p "$size" } tmux_hsplit() { local size="${1:-20}" debug "execute hsplit with size '${size}'" - tmux split-window -t "$SESSION" -c "$CURRENT_PATH" -h -p "$size" + tmux split-window "${SESSION_NAME}" -c "$CURRENT_PATH" -h -p "$size" } tmux_neww() { local name="$1" debug "execute neww with name '${name}'" - local command=(tmux new-window -t "${SESSION}:$" -c "$CURRENT_PATH" -a) + local command=(tmux new-window -t "${SESSION_NAME}" -c "$CURRENT_PATH" -a) if [[ -n "$name" ]]; then command+=(-n "$name") fi @@ -117,7 +121,7 @@ tmux_run() { if [[ -z "$shell_command" ]]; then error "tmux-run need a command" else - tmux send-keys -t "$SESSION" "$shell_command" "C-m" + tmux send-keys -t "${SESSION_NAME}" "$shell_command" "C-m" fi } @@ -127,21 +131,27 @@ tmux_project() { get_project_path "$arg" } +create_session() { + tmux new-session -ds "${SESSION_NAME}" -c "${CURRENT_PATH}" -n "${PROJECT}" + if [[ "$SESSION_NAME" == "$SESSION_ID" ]]; then + tmux rename-session -t "$SESSION_ID" "$SESSION_ID ${CURRENT_PATH##*/}" + fi +} + main() { process_args "$@" is_running_in_tmux check_script_inputs - if ! has_session "$SESSION"; then - get_project_path "${PROJECT:-$SESSION}" - tmux new-session -ds "${SESSION}" -c "${CURRENT_PATH}" -n "${PROJECT}" - - # Process commands passed with command line + if ! has_session "${SESSION_NAME}"; then + get_project_path "${PROJECT}" + create_session + # Process tmux commands passed with command line for command in "${COMMANDS[@]}"; do - process_commands "$command" + process_command "$command" done fi - tmux switch-client -t "${SESSION}" + tmux switch-client -t "${SESSION_NAME:-$SESSION_ID}" } main "${@}"