Insert the output of a shell-command directly into a tmux pane
This can be achieved by redirecting the output of the shell command into a (temporary file), then inserting the contents of that file directly into the pane using the tmux load-buffer
and paste-buffer
commands:
bind-key -T root MouseDown2Pane run-shell "xclip -selection primary -o >~/.tmux-buffer-tmp" \; load-buffer -b tmp-copy-buffer ~/.tmux-buffer-tmp \; paste-buffer -b tmp-copy-buffer -d \; run-shell -b "rm ~/.tmux-buffer-tmp"
Explaining each step:
run-shell "xclip -selection primary -o >~/.tmux-buffer-tmp"
uses the xclip utility to insert the contents of the clipboard into a temporary fileload-buffer -b tmp-copy-buffer ~/.tmux-buffer-tmp
loads the contents of the above file into a tmux bufferpaste-buffer -b tmp-copy-buffer -d
pastes those contents direty into the active pane (and deletes the temporary buffer so that the state of the buffers is unchanged by the mouse click)run-shell -b "rm ~/.tmux-buffer-tmp"
removes the temporary file.
Another way to do this that doesn't require a temporary file is:
bind-key -T root MouseDown2Pane run-shell 'tmux set-buffer -b x-clip "$(xsel -op)"' \; paste-buffer -b x-clip -d
Break down:
bind-key -T root MouseDown2Pane
: Bind to a middle mouse click on a pane in the root key table (which applies when you aren't in copy mode and haven't pressed the prefix)run-shell 'tmux set-buffer -b x-clip "$(xsel -op)"'
: This is a little hacky, but it runs theset-buffer
tmux command in a shell with another tmux command. This is so we can expand the output of thexsel
command to get the clipboard contentspaste-buffer -b x-clip -d
: Pastes the contents of the buffer, and deletes it.
Another way to do it:
bind-key -T root MouseDown2Pane run-shell 'xclip -o | tmux load-buffer -bxclip -' \; paste-buffer -bxclip -d