Skip to content

Instantly share code, notes, and snippets.

@blueyed
Last active May 26, 2023 07:39
Show Gist options
  • Save blueyed/6856354 to your computer and use it in GitHub Desktop.
Save blueyed/6856354 to your computer and use it in GitHub Desktop.
ZSH configuration to complete words from tmux pane(s)
# Complete words from tmux pane(s) {{{1
# Source: http://blog.plenz.com/2012-01/zsh-complete-words-from-tmux-pane.html
# Gist: https://gist.github.com/blueyed/6856354
_tmux_pane_words() {
local expl
local -a w
if [[ -z "$TMUX_PANE" ]]; then
_message "not running inside tmux!"
return 1
fi
# Based on vim-tmuxcomplete's splitwords function.
# https://github.com/wellle/tmux-complete.vim/blob/master/sh/tmuxcomplete
_tmux_capture_pane() {
tmux capture-pane -J -p -S -100 $@ |
# Remove "^C".
sed 's/\^C\S*/ /g' |
# copy lines and split words
sed -e 'p;s/[^a-zA-Z0-9_]/ /g' |
# split on spaces
tr -s '[:space:]' '\n' |
# remove surrounding non-word characters
=grep -o "\w.*\w"
}
# Capture current pane first.
w=( ${(u)=$(_tmux_capture_pane)} )
local i
for i in $(tmux list-panes -F '#D'); do
# Skip current pane (handled before).
[[ "$TMUX_PANE" = "$i" ]] && continue
w+=( ${(u)=$(_tmux_capture_pane -t $i)} )
done
_wanted values expl 'words from current tmux pane' compadd -a w
}
zle -C tmux-pane-words-prefix complete-word _generic
zle -C tmux-pane-words-anywhere complete-word _generic
bindkey '^X^Tt' tmux-pane-words-prefix
bindkey '^X^TT' tmux-pane-words-anywhere
zstyle ':completion:tmux-pane-words-(prefix|anywhere):*' completer _tmux_pane_words
zstyle ':completion:tmux-pane-words-(prefix|anywhere):*' ignore-line current
# Display the (interactive) menu on first execution of the hotkey.
zstyle ':completion:tmux-pane-words-(prefix|anywhere):*' menu yes select interactive
# zstyle ':completion:tmux-pane-words-anywhere:*' matcher-list 'b:=* m:{A-Za-z}={a-zA-Z}'
zstyle ':completion:tmux-pane-words-(prefix|anywhere):*' matcher-list 'b:=* m:{A-Za-z}={a-zA-Z}'
# }}}
@cpakkala
Copy link

cpakkala commented Aug 1, 2020

Nice work; thanks. Would be nice if it ignored terminal escape sequences though, but I'm not sure how to encompass them all. A huge crazy regex? For now I just have to either disable colors or live with not being able to complete git output, grep output, etc.

edit - my bad, I just assumed it was the color because non-colored words were working, but the problem was actually words beginning with a period.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment