This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/tmux.sh

93 lines
1.9 KiB
Bash
Raw Normal View History

2023-12-07 04:02:47 +03:00
# Create/attach to named session.
2023-12-07 05:11:26 +03:00
# By default uses name `main`.
2023-12-07 04:02:47 +03:00
# Usage: ta [NAME]
2023-12-07 01:44:42 +03:00
function ta() {
2023-12-05 21:50:45 +03:00
local name="$1"
# set default name.
if [[ "$name" = "" ]]; then
name="main"
fi
2023-12-05 21:50:45 +03:00
# call tmux.
tmux new -s "$name" 2> /dev/null || tmux attach-session -t "$name"
2023-08-08 16:24:15 +03:00
}
2023-12-07 04:02:47 +03:00
# Detach from running session.
2023-12-07 01:44:42 +03:00
function td() {
2023-12-07 00:42:06 +03:00
tmux detach-client
}
2023-08-08 16:24:15 +03:00
# Detach all other tmux clients.
function tda() {
tmux detach-client -a
}
2023-12-07 04:02:47 +03:00
# List running sessions.
2023-12-07 01:44:42 +03:00
function tl() {
2023-12-07 00:42:06 +03:00
tmux list-sessions
}
2023-08-08 16:24:15 +03:00
# Rename current session. Uses current dir name by default.
# Usage: trn [NAME]
2023-12-07 01:44:42 +03:00
function trn() {
2023-12-05 21:50:45 +03:00
local name="${1}"
2023-12-05 21:50:45 +03:00
[[ "${name}" = "" ]] && name="${PWD##*/}"
2023-12-05 21:50:45 +03:00
tmux rename-session "${name}"
}
2023-08-08 16:24:15 +03:00
2023-12-07 04:02:47 +03:00
# Assign name to window. Uses current dir name by default.
# Usage: tn [NAME]
2023-12-07 01:44:42 +03:00
function tn() {
2023-12-05 21:50:45 +03:00
local name="${1}"
2023-12-05 21:50:45 +03:00
[[ "${name}" = "" ]] && name="${PWD##*/}"
2023-12-05 21:50:45 +03:00
tmux rename-window "${name}"
}
2023-08-08 16:24:15 +03:00
2023-12-07 04:02:47 +03:00
# Kill specified session.
2023-12-07 05:11:26 +03:00
# By default it kills `main` session.
2023-12-07 04:02:47 +03:00
# Usage: tk [NAME]
2023-12-07 01:44:42 +03:00
function tk() {
2023-12-05 21:50:45 +03:00
# set default name.
if [[ "${1}" = "" ]]; then
1="main"
fi
2023-12-05 21:50:45 +03:00
# call tmux.
for name in "$@"; do
tmux kill-session -t "${name}"
done
2023-08-08 16:24:15 +03:00
}
2023-12-07 04:02:47 +03:00
# Kill all sessions.
2023-12-07 01:44:42 +03:00
function tka() {
2023-12-05 21:50:45 +03:00
local sessions=$(tmux list-sessions | sed -e 's/:.*//')
2023-08-08 16:24:15 +03:00
2023-12-05 21:50:45 +03:00
for session in $sessions; do
tmux kill-session -t "$session"
done
2023-08-08 16:24:15 +03:00
}
2023-10-23 00:24:36 +03:00
2023-12-07 04:02:47 +03:00
# Autocomplete with running sessions once.
2023-12-07 01:44:42 +03:00
function _complete_tmux_session() {
2023-12-05 21:50:45 +03:00
_autocomplete_first "$(tmux list-sessions 2> /dev/null | sed -e 's/:.*//')"
2023-10-23 00:24:36 +03:00
}
2023-12-07 04:02:47 +03:00
# Autocomplete with running sessions.
2023-12-07 01:44:42 +03:00
function _complete_tmux_sessions() {
2023-12-05 21:50:45 +03:00
_autocomplete "$(tmux list-sessions 2> /dev/null | sed -e 's/:.*//')"
2023-11-19 22:12:18 +03:00
}
2023-12-07 04:02:47 +03:00
# Autocomplete with current dir name and dirs inside this one.
2023-12-07 01:44:42 +03:00
function _complete_tmux_name() {
2023-12-05 21:50:45 +03:00
_autocomplete_first "${PWD##*/}"$'\n'$(ls --classify | grep /$ | sed -e 's/\/$//')
2023-11-08 20:05:19 +03:00
}
2023-11-19 22:12:18 +03:00
complete -F _complete_tmux_session ta
complete -F _complete_tmux_sessions tk
2023-11-27 16:28:27 +03:00
complete -o nosort -F _complete_tmux_name trn tn