From afa35b0690bf29cfc2dc29e8abd7aa69b6c50e7f Mon Sep 17 00:00:00 2001 From: David Ibia Date: Sun, 11 Aug 2024 13:41:25 +0100 Subject: [PATCH] refactor(.zshrc): refactor tml alias to a function to list tmux sessions feat(.zshrc): add tma function to attach to tmux session with optional session name or interactive selection feat(.zshrc): add get_tmn_recommendation function to get directory recommendations for tmn feat(.zshrc): add tmn function to create new tmux session with optional session name or interactive selection feat(.zshrc): add _fzf_complete_tma and _fzf_complete_tmn functions for fzf completion in tma and tmn commands --- .zshrc | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/.zshrc b/.zshrc index 123af01..9ce86ed 100644 --- a/.zshrc +++ b/.zshrc @@ -173,14 +173,59 @@ fi # TMUX Setup Start -alias tml="tmux ls" - -function tma() { - tmux attach -t $1 | title "Workspace" +function tml() { + sessions=$(tmux ls | sed -E 's/:.*$//') + for session in $sessions; do + echo $session + done } + +function tma() { + + # If no argument is passed, show the list of tmux sessions with fzf + + if [ -z "$1" ]; then + tmux attach -t $(tml | fzf) + return + fi + + # If an argument is passed, attach to the session with that name + tmux attach -t $1 + +} + +function get_tmn_recommendation() { + # Get the list of directories in the current directory + list_of_directories_in_current_directory=$(ls -d */ | sed 's/\/$//') + + # Return the list of directories as newline-separated strings + for dir in $list_of_directories_in_current_directory; do + echo $dir + done +} + + function tmn() { - tmux new -s $1 -c $PWD | title "Workspace" + + + # If no argument is passed, get the recommendations and show them with fzf + + if [ -z "$1" ]; then + random_tag_name=$(date +%s) + tmux new -s $(get_tmn_recommendation | fzf) | title $("Workspace - $random_tag_name") + return + fi + + tmux new -s $1 +} + +_fzf_complete_tma() { + _fzf_complete --multi --reverse --prompt="tma> " -- "$@" < <(tmux ls | sed -E 's/:.*$//') +} + +_fzf_complete_tmn() { + _fzf_complete --multi --reverse --prompt="tmn> " -- "$@" < <( get_tmn_recommendation ) } # Setup Flutter