Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert the output of a shell-command directly into a tmux pane

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.

like image 813
cmh Avatar asked Feb 19 '16 15:02

cmh


2 Answers

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 contents
  • paste-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
like image 65
Thayne Avatar answered Sep 24 '22 02:09

Thayne


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 file
  • load-buffer -b tmp-copy-buffer ~/.tmux-buffer-tmp loads the contents of the above file into a tmux buffer
  • paste-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.
like image 25
cmh Avatar answered Sep 24 '22 02:09

cmh