-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmux_autocomplete.bash
62 lines (54 loc) · 2.1 KB
/
tmux_autocomplete.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#/usr/bin/env bash
#
# Author: Owen Littlejohns, 2018-09-21
#
# A tab completion script for tmux commands. Currently included:
# - Standard command completion, e.g. "tmux a" -> "tmux attach-session"
# - Session selection for use with "tmux attach-session -t "
#
# To implement, source the file:
#
# source tmux_autocomplete.bash
#
# To avoid sourcing this in every new terminal add the previous command to the
# .bash_profile or .bashrc (or equivalent files in other shells).
#
# Reference: https://iridakos.com/tutorials/2018/03/01/bash-programmable-completion-tutorial.html
#
_find_suggestions()
{
local possible_list=$1
local tab_argument=$2
local suggestions=($(compgen -W "${possible_list}" -- "${tab_argument}"))
if [ "${#suggestions[@]}" == "1" ]; then
# Only one match, so remove command literal and proceed with autocomplete
local resolved_output=$(echo ${suggestions[0]/%\ */})
COMPREPLY=("${resolved_output}")
else
# More than one suggestion, respond with suggestions intact:
COMPREPLY=("${suggestions[@]}")
fi
}
_tmux_attach_completions()
{
# Tab complete for standard commands taken from /usr/local/etc/bash_completion.d/tmux
if [[ "${#COMP_WORDS[@]}" -eq "2" ]]; then
# Complete "tmux <command>", e.g. "tmux a" becomes "tmux attach-session"
all_commands=$(tmux list-commands | awk 'BEGIN {ORS = " "} ; {print $1}')
_find_suggestions "${all_commands}" "${COMP_WORDS[1]}"
fi
# For now everything below here is specifically for "tmux attach-session"
if [[ "${#COMP_WORDS[@]}" -lt "4" ]]; then
# There aren't at least four arguments, and it should be "tmux a -t "
return
fi
if [[ ("${COMP_WORDS[COMP_CWORD-1]}" -ne "-t") || ( ! "${COMP_WORDS[1]}" =~ ^a$|^attach$|^attach-session$) ]]; then
# Either the previous argument isn't "-t" or the second argument isn't
# "a", "attach" or "attach-session"
return
fi
# Get all the session names as a space separated list:
sessions=$(tmux ls | awk 'BEGIN {ORS = " "} ; {listOutput = $1; split(listOutput, sessionArray, ":"); print sessionArray[1]}')
_find_suggestions "${sessions}" "${COMP_WORDS[COMP_CWORD]}"
}
complete -F _tmux_attach_completions tmux