Copy text from one tmux pane to another (using vim)

  • I have two files opened in vim, each in one tmux pane. I would like to copy let's say 10 lines from one file to another.

    How can I accomplish this not using the mouse's Copy -> Paste ?

    You could involve *tmux*, but there are generally simpler ways. Are both the Vim instances on the same (local?) machine? Usually, I would just use a temporary file for that (i.e. visually select the lines, `:w /tmp/lines`, then `:r /tmp/lines` in the other instance). Or, if your Vim knows how to talk to your system clipboard, you could use the `+` or `*` registers to avoid the temporary file.

  • Alex Leach

    Alex Leach Correct answer

    8 years ago

    You'll have to use tmux shortcuts. Assuming your tmux command shortcut is the default: Ctrl+b, then:

    1. Ctrl+b, [ Enter copy(?) mode.

    2. Move to start/end of text to highlight.

    3. Ctrlspace

      Start highlighting text (on Arch Linux). When I've compiled tmux from source on OSX and other Linux's, just Space on its own usually works. Selected text changes the colours, so you'll know if the command worked.

    4. Move to opposite end of text to copy.

    5. Alt+w Copies selected text into tmux clipboard. (On Mac use Esc+w.)

    6. Move cursor to opposite tmux pane, or completely different tmux window. Put the cursor where you want to paste the text you just copied.

    7. Ctrl+b, ] Paste copied text from tmux clipboard.

    tmux is quite good at mapping commands to custom keyboard shortcuts.

    See Ctrl+b,? for the full list of set keyboard shortcuts.

    Thanks. Just a sidenode: The Alt + w did not work on Mac. Alt was mapped to Esc by default. Hence it is Esc+w.

    @KaranThis is not true for me on OS X 10.10.5 with tmux 2.0, although alt+w doesn't copy the text either.

    if you use `screen` shortcuts: `Ctrl + A [` (enter copy-mode), `Space` (start highlighting), `Enter` (end highlighting), `Ctrl + A ]` (paste).

    Accessing tmux on ubuntu 16.04LTS, using putty as terminal on Windows 10. All the commands that @alexleach mentioned worked great

    "y" also works and also copies to system clipboard as well which is what I prefer.

    Worth noting that "enter" works for me whereas alt+w does not.

    As noted in https://awhan.wordpress.com/2010/06/20/copy-paste-in-tmux/, if you have vi key bindings, step 5 should be replaced with `Enter` key.

    what If I need to copy and paste to a text editor? For some reason it doesn't seem to pick up that selected text even after pressing 'enter' after selecting the text.

    Note if you have in your `.tmux.conf` the lines `set -g mode-keys vi` and `set -g status-keys vi`, you can move with VI movement commands, enter selection mode with `v`/`V`, and copy the text with `y`.

    you just saved my day :)

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM