Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)} )
echo $w > /tmp/w1
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}'
# }}}
@mrbradparks

This comment has been minimized.

Copy link

mrbradparks commented Jun 13, 2014

Sweet! this improves an already awesome tmux/zsh super power... nice!

@unphased

This comment has been minimized.

Copy link

unphased commented Mar 21, 2015

When I play with this, sometimes I get entries in the completion list that contain comical quantities of backslashes.

Any idea what's causing that? This sort of unexpected behavior is sort of making me wary of using this tool because I don't want it to suddenly lock up my machine.

Edit: Okay I think I have an idea of what's causing it. It's the feedback loop of tmux fetching the already seen backslashes and surfacing them as suggestions, thereby doubling (tripling if there is still room in the window) the number of backslashes. When they get left around in the shell scrollback, the backslashes will grow exponentially, but the total quantity will somewhat be limited by terminal window size at least (due to use of tmux capture-pane). This is actually very, very neat and amazingly powerful.

@blueyed

This comment has been minimized.

Copy link
Owner Author

blueyed commented Nov 8, 2016

@mrbradparks @unphased
Sorry, I was not notified about your comments (can you configure this on GitHub?)

I have updated the Gist with what I've been using since a while.
It is enhanced based on https://github.com/wellle/tmux-complete.vim/blob/master/sh/tmuxcomplete.

Check out also Vifon/autocomplete-ALL-the-things#20, which reminded me of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.