From 9313fe68ad5e94fd570807ee39b1bfaae86cce85 Mon Sep 17 00:00:00 2001 From: Sam Sudar Date: Thu, 13 Oct 2016 11:44:05 -0700 Subject: [PATCH] Add tmux completion This is adapted from the following project: https://github.com/srsudar/tmux-completion --- completions/Makefile.am | 1 + completions/tmux | 134 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 completions/tmux diff --git a/completions/Makefile.am b/completions/Makefile.am index 306e498ddaa..44174b44fd3 100644 --- a/completions/Makefile.am +++ b/completions/Makefile.am @@ -364,6 +364,7 @@ bashcomp_DATA = 2to3 \ tcpnice \ timeout \ tipc \ + tmux \ tracepath \ tshark \ tune2fs \ diff --git a/completions/tmux b/completions/tmux new file mode 100644 index 00000000000..4407fa1a33c --- /dev/null +++ b/completions/tmux @@ -0,0 +1,134 @@ +# tmux(1) completion + +_tmux() +{ + local cur prev opts onePrev + _init_completion || return + + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + onePrev="${COMP_WORDS[COMP_CWORD-1]}" + + if [ "$COMP_CWORD" -ge 2 ]; then + # Check if we want to list available sessions. We're going to assume + # this is defined as: [ls | list-session] -t . + local windowCommands currentSessions + windowCommands=("ls", "list-sessions") + + prev="${COMP_WORDS[COMP_CWORD-2]}" + if [ "$prev" = "attach" ] || [ "$prev" = "attach-session" ] ; then + if [ "$onePrev" = "-t" ] ; then + # Get a list of all session names. + # We're assuming this output is in the form: + # 'name: more info' + # We'll get the name by using cut--use ":" as a delimiter, and + # print the first column. If there are no sessions, we expect the + # following message on stderr: + # 'failed to connect to server: Connection refused'. + # In this case we'll return an empty list. + # NB: Since the failed to connect message is displayed on stderr, + # we need to pipe both stdout and stderr to cut. Therefore we are + # piping with |& rather than just |. + currentSessions=$(tmux ls |& cut -d : -f 1) + if [ "${currentSessions[0]}" = "failed to connect to server" ]; \ + then + # We don't want to display any options, so clear the array. + currentSessions=$( ) + fi + COMPREPLY=($(compgen -W "${currentSessions}" -- ${cur})) + return + fi + fi + fi + + opts=" \ + attach-session \ + bind-key \ + break-pane \ + capture-pane \ + choose-client \ + choose-session \ + choose-window \ + clear-history \ + clock-mode \ + command-prompt \ + confirm-before \ + copy-buffer \ + copy-mode \ + delete-buffer \ + detach-client \ + display-message \ + display-panes \ + down-pane \ + find-window \ + has-session \ + if-shell \ + join-pane \ + kill-pane \ + kill-server \ + kill-session \ + kill-window \ + last-window \ + link-window \ + list-buffers \ + list-clients \ + list-commands \ + list-keys \ + list-panes \ + list-sessions \ + list-windows \ + load-buffer \ + lock-client \ + lock-server \ + lock-session \ + move-window \ + new-session \ + new-window \ + next-layout \ + next-window \ + paste-buffer \ + pipe-pane \ + previous-layout \ + previous-window \ + refresh-client \ + rename-session \ + rename-window \ + resize-pane \ + respawn-window \ + rotate-window \ + run-shell \ + save-buffer \ + select-layout \ + select-pane \ + select-prompt \ + select-window \ + send-keys \ + send-prefix \ + server-info \ + set-buffer \ + set-environment \ + set-option \ + set-window-option \ + show-buffer \ + show-environment \ + show-messages \ + show-options \ + show-window-options \ + source-file \ + split-window \ + start-server \ + suspend-client \ + swap-pane \ + swap-window \ + switch-client \ + unbind-key \ + unlink-window \ + up-pane" + + COMPREPLY=($(compgen -W "${opts}" -- ${cur})) + return + +} +complete -F _tmux tmux + +# ex: filetype=sh