From 9146724e6cfc95f50867cdfd9599255f07d38e1a Mon Sep 17 00:00:00 2001 From: Ivar Fatland Date: Sat, 11 Oct 2025 16:32:39 +0200 Subject: [PATCH] shell script based session management --- nvim/.config/nvim/snippets/sh.snippets | 29 ++++++++++++++++++++++++++ tmux/.tmux.conf | 1 + tmux/choose-session.sh | 4 ++++ tmux/tmux/edit-sessions.sh | 22 +++++++++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 tmux/choose-session.sh create mode 100644 tmux/tmux/edit-sessions.sh diff --git a/nvim/.config/nvim/snippets/sh.snippets b/nvim/.config/nvim/snippets/sh.snippets index 3a06ae5..bd4e22c 100644 --- a/nvim/.config/nvim/snippets/sh.snippets +++ b/nvim/.config/nvim/snippets/sh.snippets @@ -1,3 +1,32 @@ snippet raylibBuildLinux cc $1.c -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 -o $1 ./$1 + +snippet session + SESSION="${1:[session name]}" + + if tmux has-session -t "$SESSION" 2>/dev/null; then + if [ -n "$TMUX" ]; then + tmux switch-client -t "$SESSION" + else + tmux attach -t "$SESSION" + fi + exit 0 + fi + + tmux new-session -d -s "$SESSION" -c ${2:[path]} -n "${3:[main window name]}" + tmux send-keys -t "$SESSION":"$3" '${4:[main window cmd]}' C-m + + tmux new-window -t "$SESSION" -c $2 + + tmux select-window -t "$SESSION":"$3" + if [ -n "$TMUX" ]; then + tmux switch-client -t "$SESSION" + else + tmux attach -t "$SESSION" + fi + +snippet window + tmux new-window -t "$SESSION" -c ${1:[path]} -n "${2:[window name]}" + tmux send-keys -t "$SESSION":"$2" '${3:[cmd]}' C-m + diff --git a/tmux/.tmux.conf b/tmux/.tmux.conf index 02a743d..ed679f8 100644 --- a/tmux/.tmux.conf +++ b/tmux/.tmux.conf @@ -18,6 +18,7 @@ bind c-l select-pane -R bind c-g popup -E -w 100% -h 100% -d "#{pane_current_path}" -b rounded lazygit bind c-k popup -E -w 100% -h 100% -b rounded "tmux list-keys | fzf" bind c-t popup -E -w 100% -h 100% -b rounded -d "#{pane_current_path}" +bind c-s popup -E -w 100% -h 100% -b rounded "sh $HOME/choose-session.sh" set -g default-terminal "tmux-256color" set -as terminal-overrides ",xterm-256color:Tc" diff --git a/tmux/choose-session.sh b/tmux/choose-session.sh new file mode 100644 index 0000000..ed4719c --- /dev/null +++ b/tmux/choose-session.sh @@ -0,0 +1,4 @@ +SESSION_FILE=$(ls "$HOME/tmux" | fzf) +if [ -n "$SESSION_FILE" ]; then + sh "$HOME/tmux/$SESSION_FILE" +fi diff --git a/tmux/tmux/edit-sessions.sh b/tmux/tmux/edit-sessions.sh new file mode 100644 index 0000000..a1eda98 --- /dev/null +++ b/tmux/tmux/edit-sessions.sh @@ -0,0 +1,22 @@ +SESSION="edit sessions" + +if tmux has-session -t "$SESSION" 2>/dev/null; then + if [ -n "$TMUX" ]; then + tmux switch-client -t "$SESSION" + else + tmux attach -t "$SESSION" + fi + exit 0 +fi + +tmux new-session -d -s "$SESSION" -c ~/tmux/ -n "nvim" +tmux send-keys -t "$SESSION":"nvim" 'nvim .' C-m + +tmux new-window -t "$SESSION" -c ~/tmux/ + +tmux select-window -t "$SESSION":"nvim" +if [ -n "$TMUX" ]; then + tmux switch-client -t "$SESSION" +else + tmux attach -t "$SESSION" +fi