From 067df4e34b563a8525442b8f668e43a1c815eb4e Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Tue, 7 Jan 2025 17:38:46 +0200 Subject: [PATCH] feat(bin): t, custom tmux session manager - drops tms bindings from tmux config, wasn't using them - drops t alias for tail -f, clashed with t script --- config/alias | 2 -- config/tmux/tmux.conf | 13 ++++++++----- local/bin/t | 45 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) create mode 100755 local/bin/t diff --git a/config/alias b/config/alias index 8a383e1..7f69ef8 100755 --- a/config/alias +++ b/config/alias @@ -79,8 +79,6 @@ alias zapds='find . -name ".DS_Store" -print -delete' alias zappyc="find . -type f -name '*.pyc' -ls -delete" # Run all zaps alias zapall="zapds && zappyc" -# tail with follow flag on -alias t='tail -f' # directory usage, return only the total alias dn='du -chd1' # Mirror site with wget diff --git a/config/tmux/tmux.conf b/config/tmux/tmux.conf index ecfa86b..5ffd406 100644 --- a/config/tmux/tmux.conf +++ b/config/tmux/tmux.conf @@ -7,6 +7,10 @@ # ╭──────────────────────────────────────────────────────────╮ # │ Bindings │ # ╰──────────────────────────────────────────────────────────╯ +# bind flags +# -r = repeatable, only needs prefix once +# -n = doesn't need prefix +# -t = binds to a certain key-table (root, copy-mode, prefix, etc.) # Set to Control + Space, keeping the default of C-b intact. # unbind C-b @@ -39,11 +43,6 @@ bind r source-file ~/.dotfiles/config/tmux/tmux.conf \; display "tmux cfg reload bind -r '(' switch-client -p\; refresh-client -S bind -r ')' switch-client -n\; refresh-client -S -# tms, https://github.com/jrmoulton/tmux-sessionizer -bind C-h display-popup -E "tms" -bind C-j display-popup -E "tms switch" -bind C-k display-popup -E "tms windows" - # Open a new window with + N bind N new-window @@ -61,6 +60,10 @@ bind '"' split-window -v -c "#{pane_current_path}" bind '!' split-window -h -c "#{pane_current_path}" bind '^' switch-client -t'{marked}' +# .local/bin/t triggers +bind -r T new-window ~/.local/bin/t +bind -r D run-shell "t ~/Code/ivuorinen/dotfiles" + # synchronize all panes in a window bind y setw synchronize-panes diff --git a/local/bin/t b/local/bin/t new file mode 100755 index 0000000..8be06b7 --- /dev/null +++ b/local/bin/t @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# +# Credit to ThePrimeagen, jessarcher +# https://github.com/jessarcher/dotfiles/blob/master/scripts/t +# +# Tweaks by Ismo Vuorinen 2025 + +# Set environment variables for configuration with defaults +T_ROOT="${T_ROOT:-$HOME/Code}" +DOTFILES="${DOTFILES:-$HOME/.dotfiles}" + +# Function to print an error message and exit +error_exit() +{ + echo "Error: $1" >&2 + exit 1 +} + +# Select the directory +if [[ $# -eq 1 ]]; then + selected="$1" +else + items=$(find "$T_ROOT" -maxdepth 2 -mindepth 1 -type d) + items+="$(printf "\n%s" "$DOTFILES")" + items+="$(printf "\n/tmp")" + selected=$(echo -e "$items" | fzf) || exit 0 # Exit if no selection is made +fi + +# Exit if no directory was selected +[[ -z $selected ]] && error_exit "No directory selected." + +# Sanitize the session name +dirname=$(basename "$selected" | tr '.' '_') + +# Try to switch to the tmux session +if tmux switch-client -t "=$dirname" 2> /dev/null; then + exit 0 +fi + +# Create a new tmux session or attach to an existing one +if tmux new-session -c "$selected" -d -s "$dirname" 2> /dev/null; then + tmux switch-client -t "$dirname" +else + tmux new -c "$selected" -A -s "$dirname" +fi