Add tmux fzf session chooser
Added tmux session chooser. Aliased to `tm`, calling `tmux_attach_start` (the original tm script). When called without arguments displays a fzf list of currently running tmux sessions, with a preview to their respective open panes. A session can be chosen in fzf which tmux will attach itself to. When creating a query in fzf which does not have a valid target and confirming, tmux will automatically create that session and attach itself to it. When called with an argument, tmux will attach itself or create a session of the same name. If called with the name of a session file, as before, tmux will automatically execute that session file and attach itself to it.
This commit is contained in:
parent
fbc1c44652
commit
92376839a4
5 changed files with 71 additions and 2 deletions
49
tmux/.config/sh/alias.d/tmux.sh
Normal file
49
tmux/.config/sh/alias.d/tmux.sh
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
exist() { type "$1" >/dev/null 2>&1; }
|
||||
|
||||
# show a list of running tmux sessions
|
||||
alias tl='tmux list-sessions -F "#{session_name}" 2>/dev/null'
|
||||
|
||||
# fzf
|
||||
if exist fzf; then
|
||||
# fzf select a tmux session to connect to, with pane preview
|
||||
alias tm='fzf_tmux'
|
||||
fi
|
||||
|
||||
fzf_tmux() {
|
||||
if [ -z "$1" ]; then
|
||||
result=$(
|
||||
tl | fzf \
|
||||
--layout=reverse \
|
||||
--height=50% \
|
||||
--border \
|
||||
--prompt="Session> " \
|
||||
--preview="tmux_pane_tree {}" \
|
||||
--preview-window=right:99% \
|
||||
--print-query
|
||||
)
|
||||
case "$?" in
|
||||
0)
|
||||
# found a session, attaching
|
||||
tmux_attach_start "$(echo "$result" | tail --lines=1)"
|
||||
;;
|
||||
1)
|
||||
# did not find a session, creating
|
||||
result=$(echo "$result" | head --lines=1)
|
||||
# if . was only thing entered, create one for current dir
|
||||
if [ "$result" = "." ]; then
|
||||
tmux_attach_start
|
||||
# create for query name
|
||||
else
|
||||
tmux_attach_start "$result"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
else
|
||||
tmux_attach_start "$1"
|
||||
fi
|
||||
}
|
||||
|
||||
unset choice
|
||||
unset -f exist
|
||||
|
|
@ -7,6 +7,6 @@ select-pane -t 1
|
|||
split-window -v watch -t -n 1 -c 'cd ~/.dotfiles; git -c color.ui=always log --graph --date=short --decorate --oneline --all --remotes'
|
||||
select-pane -t 4
|
||||
new-window -n code
|
||||
send-keys "cd ~/.dotfiles; vim ." C-m I
|
||||
send-keys "cd ~/.dotfiles; v ." C-m I
|
||||
new-window -n test
|
||||
select-window -t 1
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ set -g @plugin 'Morantron/tmux-fingers'
|
|||
set -g @plugin 'christoomey/vim-tmux-navigator'
|
||||
set -g @plugin 'tmux-plugins/tmux-resurrect'
|
||||
|
||||
if "test ! -d ${TMUX_PLUGIN_MANAGER_PATH}/tpm" \
|
||||
if "test ! -z ${TMUX_PLUGIN_MANAGER_PATH} && test ! -d ${TMUX_PLUGIN_MANAGER_PATH}/tpm" \
|
||||
"run 'git clone https://github.com/tmux-plugins/tpm {TMUX_PLUGIN_MANAGER_PATH}/tpm && {TMUX_PLUGIN_MANAGER_PATH}/tpm/bin/install_plugins'"
|
||||
|
||||
run -b "${TMUX_PLUGIN_MANAGER_PATH}/tpm/tpm"
|
||||
|
|
|
|||
153
tmux/.local/bin/tmux_attach_start
Executable file
153
tmux/.local/bin/tmux_attach_start
Executable file
|
|
@ -0,0 +1,153 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
# Attach to a tmux session, or create it if it doesnt exist
|
||||
|
||||
if [ "$1" = '-h' ] || [ "$1" = '--help' ]; then
|
||||
cat <<-EOF
|
||||
Usage: $0 [FILE] [NAME]
|
||||
Attach to an existing tmux session, or bootstrap a new session.
|
||||
|
||||
If no session exists which fits the criteria, this will create a new one and
|
||||
load it up obeying the commands contained in the FILE argument passed in. It
|
||||
must contain commands which are tmux-command compatible since they will be
|
||||
passed to tmux unmodified. If a session exists and a valid configuration FILE is
|
||||
passed, it will attach itself to the session and execute the session setup inside.
|
||||
|
||||
It will load the default tmux.conf in the following order:
|
||||
- XDG_CONFIG_HOME/tmux/tmux.conf (usually ~/.config/tmux/tmux.conf)
|
||||
- ~/.tmux.conf
|
||||
- /etc/tmux.conf
|
||||
Afterwards, it will apply the contents of the FILE argument passed into the command.
|
||||
|
||||
If no FILE argument is passed it will create an empty session, or attach itself to
|
||||
the session specified.
|
||||
|
||||
If no NAME argument is passed it will automatically create a session name from the
|
||||
FILE argument passed.
|
||||
|
||||
If creating a new session without passing in a FILE argument, and the current directory
|
||||
contains a .tmux.session file, it will automatically use that to set up the session.
|
||||
|
||||
By default, it also looks for valid session files in XDG_CONFIG_HOME/tmux/sessions/
|
||||
This path can be changed by setting the TM_SESSION_PATH environment variable.
|
||||
EOF
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Make tmux obey the XDG specification on startup
|
||||
if [ -e "$XDG_CONFIG_HOME/tmux/tmux.conf" ]; then
|
||||
start="tmux -f $XDG_CONFIG_HOME/tmux/tmux.conf"
|
||||
else
|
||||
start=tmux
|
||||
fi
|
||||
|
||||
_file_exists() {
|
||||
if [ -f "$1" ]; then
|
||||
true
|
||||
else
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
||||
_not_in_tmux() {
|
||||
[ -z "$TMUX" ]
|
||||
}
|
||||
|
||||
_session_exists() {
|
||||
if tmux has-session -t "$session_name" >/dev/null 2>&1; then
|
||||
true
|
||||
else
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
||||
_create_detached_session() {
|
||||
($start new-session -d -s "$session_name")
|
||||
}
|
||||
|
||||
_load_env_session_file() {
|
||||
if [ -f ./.tmux.session ]; then
|
||||
xargs -L1 tmux <./.tmux.session
|
||||
fi
|
||||
}
|
||||
|
||||
_load_session_file() {
|
||||
if [ -f "$session_file" ]; then
|
||||
xargs -L1 tmux <"$session_file"
|
||||
fi
|
||||
}
|
||||
|
||||
_set_session_file_path() {
|
||||
# Prefer local dir if file exists there, fall back to root dir
|
||||
if _file_exists "${1}"; then
|
||||
session_file="${1}"
|
||||
elif _file_exists "${session_dir}${1}"; then
|
||||
session_file="${session_dir}${1}"
|
||||
fi
|
||||
}
|
||||
|
||||
_set_session_vars() {
|
||||
session_dir=${TM_SESSION_DIR:-"$XDG_CONFIG_HOME/tmux/sessions/"}
|
||||
|
||||
# set up session name and config file - if passed in as arguments
|
||||
if [ "$#" -eq 2 ]; then
|
||||
if ! _file_exists "$1" && ! _file_exists "${session_dir}${1}"; then
|
||||
echo >&2 "can not source session file: '${1}' does not exist."
|
||||
exit 1
|
||||
fi
|
||||
_set_session_file_path "$1"
|
||||
session_name=${2}
|
||||
elif [ "$#" -eq 1 ]; then
|
||||
_set_session_file_path "$1"
|
||||
if ! _file_exists "$session_file"; then
|
||||
session_name=${1}
|
||||
fi
|
||||
fi
|
||||
|
||||
# set default session name if none was passed
|
||||
if [ -z "$session_name" ]; then
|
||||
# only if we have a filename should we append it to the session name
|
||||
if [ -n "$session_file" ]; then
|
||||
fname=/$(basename "$session_file" .session)
|
||||
fi
|
||||
# default to parent directory name / config file name for the session
|
||||
session_name=$(basename "$PWD" | sed 's/[^a-zA-Z0-9\-]//g' | tr . -)"$fname"
|
||||
fi
|
||||
}
|
||||
|
||||
_attach_or_create() {
|
||||
# first create a new session if one doesn't exist
|
||||
if ! _session_exists; then
|
||||
_create_detached_session
|
||||
# look for .tmux.session file if no file has been explicitly passed in
|
||||
if ! _file_exists "$session_file"; then
|
||||
_load_env_session_file
|
||||
fi
|
||||
fi
|
||||
|
||||
_load_session_file
|
||||
|
||||
# then attach or switch to the session
|
||||
if _not_in_tmux; then
|
||||
$start attach -t "$session_name"
|
||||
else
|
||||
$start switch-client -t "$session_name"
|
||||
fi
|
||||
|
||||
_unset_functions
|
||||
}
|
||||
|
||||
_unset_functions() {
|
||||
unset _set_session_vars
|
||||
unset _set_session_file_path
|
||||
unset _attach_or_create
|
||||
unset _load_session_file
|
||||
unset _create_detached_session
|
||||
unset _session_exists
|
||||
unset _not_in_tmux
|
||||
unset _file_exists
|
||||
}
|
||||
|
||||
_set_session_vars "$@"
|
||||
_attach_or_create
|
||||
exit 0
|
||||
20
tmux/.local/bin/tmux_pane_tree
Executable file
20
tmux/.local/bin/tmux_pane_tree
Executable file
|
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/env bash
|
||||
# create a tree-style view
|
||||
# of all open tmux panes
|
||||
# and the command they are currently running
|
||||
|
||||
tmux ls -F'#{session_id}' | while read -r s; do
|
||||
S=$(tmux ls -F'#{session_id}#{session_name}: #{T:tree_mode_format}' | grep ^"$s")
|
||||
session_info=${S##$s}
|
||||
session_name=$(echo "$session_info" | cut -d ':' -f 1)
|
||||
if [[ -n "$1" ]] && [[ "$1" == "$session_name" ]]; then
|
||||
echo -e "\033[1;34m$session_info\033[0m"
|
||||
tmux lsw -t"$s" -F'#{window_id}' | while read -r w; do
|
||||
W=$(tmux lsw -t"$s" -F'#{window_id}#{T:tree_mode_format} - #{pane_current_command}' | grep ^"$w")
|
||||
H=$(tmux lsw -t"$s" -F'#{window_id}#H' | grep ^"$w")
|
||||
echo " ${W##$w}" | sed "s/\"${H##$w}\" //"
|
||||
done
|
||||
else
|
||||
echo -e "\033[1m$session_info\033[0m"
|
||||
fi
|
||||
done
|
||||
Loading…
Add table
Add a link
Reference in a new issue