You're close; well sort-of. Copy-mode commands need to be sent via sendkeys -X
.
So to pipe the selected text to a command, it would be:
- Prefix :
send-keys -X copy-pipe "wc"
BUT, Tmux doesn't display the output of a copy-pipe
anywhere. With run-command
, the output is displayed in a copy-buffer. For copy-pipe
, it seems to just be gobbled. The example you linked to in another answer simply saved a buffer, so no output/feedback was necessary.
For a wc
, that's not too useful. And I'm guessing that the output of pastebin
is a quite-useful URL ...
The simplest alternative that I've found starts to get messy, especially since you want to this with arbitrary commands:
- Prefix :
send-keys -X copy-selection-no-clear ; run-shell "tmux show-buffer | wc"
Note that there's no error checking there to determine if something is selected. It will simply run show-buffer
on the most recent buffer.
A Potentially Better Option, IMHO
Since Vim is good at this use-case, use it. Set up a key-binding that opens the scrollback buffer in vim
using process substitution.
For testing in the shell (tested):
tmux bind-key -T copy-mode v new-window -d -n scrollback "vi <(tmux capture-pane -p -S - -E -)" \\\; run-shell "sleep 1" \\\; select-window -t scrollback
Or in your config (untested):
bind-key -T copy-mode v new-window -d -n scrollback "vi <(tmux capture-pane -p -S - -E -)" \; run-shell "sleep 1" \; select-window -t scrollback
Hacky, yes, and you may need to increase the sleep
for larger buffers -- I haven't tested that. There's probably a better way, but that's what I came up with.
Also you'll lose ANSI color codes, but you might be able to handle that (if you care) with the AnsiEsc VIM extension, along with adding -e
to the capture-buffer
(to keep ANSI codes in the buffer output).