My goal is to replicate the middle-mouse copy-paste functionality in linux.
I can display the output of this clipboard in a pane via:
bind-key -T root MouseDown2Pane run-shell "xclip -selection primary -o"
I'd like to directly insert this output into the pane (i.e. similar to send-keys
) but I don't see a way to link the 2 commands together.
I'm running tmux version 2.1.
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 the set-buffer
tmux command in a shell with another tmux command. This is so we can expand the output of the xsel
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
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With