3

I want to have two tmux panes going - the first could have one of several programs in it (often, but not always, vim) and the second will have an interactive session in R, python, or bc.

The two panes aren't always going to be showing the same system - usually I'll be running tmux on system A, doing something in one pane there, but then have the second pane ssh'd to system B and running an interactive session of R/Python/bc there.

My goal is to be able to highlight something in the first pane, and have it sent and executed in the other pane.

I know tmux can copy/paste between panes. And I know tmux can send-keys a specific command to another pane and end it with an enter so it is executed. But can I make it send-keys "whatever I have highlighted in the first pane" to the second pane then end with the enter character for execution?

(In other words, I'm trying to emulate the behavior of an IDE, where you can highlight a block of code and click "run". I am looking to do this specifically using tmux, not a special vim plugin, because sometimes I'll be using something other than vim in the first pane.)

1

I believe you can achieve what you're after by:

1) entering copy mode (prefix [)

2) selecting some text (most likely v(isual select)/y(ank))

3) sending it to another pane via tmux paste-buffer -t [left/right]

Here's a proof of concept I just sketched out:

Given a Ruby script called foo.rb in your current directory:

# foo.rb
x = "hello there"
puts x

... and two panes, vertically split (bash in left/irb session in right), if you run cat foo.rb, enter copy-mode, yank the file's contents and then run tmux paste-buffer -t right from the left pane, you should see the following output in the right pane:

λ irb
2.5.1 :001 > x = "hello there"
 => "hello there"
2.5.1 :002 > puts x
hello there
 => nil

From there, you could write a shell script and/or wire up a key binding to prevent you from having to type out/recall tmux paste-buffer -t [left/right].

paste-buffer should also handle "ending with enter" for you. From the tmux man page's paste-buffer entry:

When output, any linefeed (LF) characters in the paste buffer are replaced with a separator, by default carriage return (CR).

If the default separator doesn't work for some reason, you can also specify one using paste-buffer -s ....

  • Thank you - that definitely helped get me on the way to a solution! It does seem like no matter what I pass paste-buffer with the -s option, it still won't execute the command, but I found that you can concatenate commands with \; so I can add a send-keys C-m right after the paste-buffer command. – bluemouse Feb 27 at 23:40
0

For anyone who Googles across this later, here are the final, fully-tweaked lines from my .tmux.conf (inspired by pdoherty926's answer to my question here).

With this combination of key bindings I have it down to two keystrokes: F5 to start copying from where-ever my cursor is at the time that I press F5, and F6 to finish copying and execute the code in the other pane:

# These are lines that anybody using vim-like copy/paste will have
set-window-option -g mode-keys vi
bind P paste-buffer
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection
# These are the lines that make F5/F6 do their thing
bind-key -n F5 copy-mode \; send-keys v
bind-key -n F6 send-keys y \; paste-buffer -t %1 \; send-keys -t %1 C-m

The reason I did it this way is because you cannot directly bind the begin-selection and copy-selection commands - eg "bind-key -n F5 copy-mode \; begin-selection" wouldn't work, but if I bind "v" to begin-selection in copy mode (the standard vim-like binding), and then have F5 execute "send-keys v" after entering copy mode, that does work.

If you happen to be using an ssh session configured to handle relaying mouse clicks, you can also add the following:

setw -g mode-mouse on
set -g mouse-select-window on

With those lines (in addition to the ones above), now all you have to do is highlight the code with the mouse and press F6 to run it. (If you ever need to bypass tmux's mouse-click-grabbing while these lines are in your .tmux.conf, just hold down the shift key while you click.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.