210

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 ?

2
  • 5
    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. Commented Dec 19, 2012 at 5:39
  • tmux's "copy mode" commands and default keyboard shortcuts in the man page for reference.
    – toraritte
    Commented Jan 20, 2023 at 16:12

9 Answers 9

309

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. Ctrl+Space

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.

  1. Move to opposite end of text to copy.

  2. Alt+w Copies selected text into tmux clipboard.
    On Mac, use Esc+w. Try Enter if none of the above work.

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

  4. 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.

15
  • 4
    Thanks. Just a sidenode: The Alt + w did not work on Mac. Alt was mapped to Esc by default. Hence it is Esc+w.
    – Karan
    Commented Mar 22, 2016 at 7:58
  • 19
    if you use screen shortcuts: Ctrl + A [ (enter copy-mode), Space (start highlighting), Enter (end highlighting), Ctrl + A ] (paste).
    – jfs
    Commented Aug 2, 2016 at 9:58
  • 22
    Worth noting that "enter" works for me whereas alt+w does not. Commented Nov 29, 2017 at 1:02
  • 9
    As noted in awhan.wordpress.com/2010/06/20/copy-paste-in-tmux, if you have vi key bindings, step 5 should be replaced with Enter key.
    – Ying Xiong
    Commented Dec 4, 2017 at 13:04
  • 5
    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.
    – uday
    Commented Dec 5, 2017 at 20:38
51

Unfortunately, I can't add a comment yet to Alex Leach's answer so I'm going to include an addendum here for Mac OS X users:

  1. Enter Copies the selected text
4
  • 3
    thanks! I am on Ubuntu Xenial, using byobu/tmux with all default settings, this worked, Alt + w did not. Commented Mar 21, 2016 at 18:16
  • 1
    This doesn't work on OS X Yosemite with tmux 2.0, nor does alt+w, nor does esc+w :(
    – weberc2
    Commented Apr 6, 2016 at 15:59
  • Works for me too on Windows using a Zoc terminal.
    – jdhao
    Commented Oct 22, 2018 at 9:24
  • why doesn't command+c work? Commented May 4, 2021 at 15:32
25

If you are using vim and tmux on macOS (Tested on macOS 10.12.2, tmux 2.3):

(Assume that prefix key combination of tmux is prefix. The prefix is ctrl + b in defaults.)

  • Copy:
    1. Press prefix + [ to enter copy mode.
    2. Use arrow keys to go to the start/end of text selection.
    3. Press ctrl + space (If you have set ctrl + space as prefix, Press ctrl + space + space instead)
    4. Use arrow keys to move to the other side of selection.
    5. Press ctrl + w.
  • Paste:
    1. Press prefix + ] in insert mode.
3
  • 1
    I am on mac os and in step 3, pressing only space works, in step 5, pressing return works.
    – zyy
    Commented May 6, 2020 at 17:05
  • but how do I paste it out of tmux like to an email? Commented May 4, 2021 at 15:33
  • 1
    I'm on macOS Big Sur with tmux from brew. In step 5, return does not work, but I can confirm that ctrl-W does. Commented Oct 13, 2021 at 18:09
20

I am connecting to my CentOS server on a Windows machine using ZOC terminal ssh client. Here is what works for me:

  • Enter copy mode: <prefix>+[
  • Start copy: <Space>
  • Copy text: <Enter>
  • Paste text: <prefix>+]
1
  • but how do I paste it out of tmux like to an email? Commented May 4, 2021 at 15:33
8

If you have vim open, its better to use the vim copy paste to copy text.

Tmux copy paste will work, however there are several disadvantages.

First, when copying multiple lines of text, you will have to select multiple lines. Tmux copy paste typically doesnt understand that vim has line numbers or there are markings in the vim gutter (such as relative numbers/absolute numbers). As a result, it will copy those as well.

Secondly, I have found that if the text has certain language specific formatting, and you directly try to copy paste using tmux to vim, it will mess up the formatting. This can be avoided by using set paste!. However, this requires a lot more work.

Use "+y to copy and "+p to paste

Vim natively provides a method to copy paste between the vim buffer and the system clipboard, using either "+y or "*y. Then pasting using "+p or "*p. This will avoide copying the relative numbers in the gutter or other random marks which are not relevant to vim

2
  • 1
    I don't get it. I tried copying in vim with "+y, but when switching to a pane that has a normal shell, trying to paste with "+p wont work, because it will start typing "...etc What am I doing wrong?
    – SFbay007
    Commented Apr 12, 2020 at 3:12
  • @SFbay007 in case you were still wondering or someone else stumbles upon this, the "+y [y]anks (copies) the text into the clipboard (+) register ("). If you are [p]asting in vim, you use "+p, but if you are pasting in a normal shell you would use your normal method of paste (eg ctrl + shift + v). Note you may need to use the * register instead, depending on your system
    – ljden
    Commented Feb 10, 2022 at 2:28
4

You could use the system clip board with "*y and "*p instead of the normal y and p.

4
  • 1
    To use system clipboard you have to use "+2yy – copy two lines to X11 clipboard "+dd – cut line to X11 clipboard "+p – paste X11 clipboard
    – Patryk
    Commented Jan 9, 2013 at 12:59
  • Do I need to type anything before using these commands? Commented Mar 9, 2018 at 12:46
  • @ThomasAhle, no.. its literally the double quotes ("), followed by plus (+) followed by 2ys (y). Make sure you are in normal mode. If you are in normal mode, you should be able to use h,j,k,l to move . I have also seen sometimes on my ubuntu VM, the "+" register vanishes, for unknown reason. To verify that you have the + register, you can type :reg and check whether there is string "+ in the first column.
    – alpha_989
    Commented Apr 12, 2018 at 15:05
  • Yes, this is it, this is the answer. Much simpler than the other ones suggested (using tmux copy/paste) and this doesn't mess line numbers. Would this cause any issues if you remapped standard y and p to "+y and "+p ?
    – ryan
    Commented Aug 23, 2019 at 16:08
2

I have chosen to use Ctrl+Alt+C and Ctrl+Alt+V to copy and paste within tmux. Tmux's copy-mode-vi enables navigation within a tmux pane with vim keys hjkl^$..., rectangle mode is useful to copy table columns or when there are further splits within the pane, activate it with r.

Here is how I set it up in ~/.tmux.conf:

bind P paste-buffer
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi y send-keys -X copy-selection
bind-key -T copy-mode-vi r send-keys -X rectangle-toggle
# Also copy to system clipboard
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -sel clip -i'
# map copy mode to ctrl+alt+c
bind-key -n 'C-M-c' copy-mode
# map paste buffer to ctrl+alt+v
bind-key -n 'C-M-v' paste-buffer

I created this setup by modifying this blog post.

This copy-paste solution will also work if the 2 tmux panes (vim instances or anything else) are on different machines (one remote, one local).

1
  • Just what I needed! Thank you!
    – Shinto C V
    Commented May 22, 2020 at 7:12
0

A couple of options for vim use since the tmux shortcut sequence is bit long and I find it hard to remember

Like the answers above said you can use "+y and "*y to copy and then "+p and "*p respectively to paste. If you want vim to use the clipboard by default so you can just use y/p directly then.

set clipboard=unamed "sets the default copy register to be *
set clipboard=unamedplus "sets the default copy register to be +

source for clipboard command: https://vi.stackexchange.com/questions/84/how-can-i-copy-text-to-the-system-clipboard-from-vim

0

I've had the issue myself and uploaded my .vimrc and .tmux.conf files here: tvp-repl

You can configure tmux to

  1. Copy the vim-pane selection to the clipboard,
  2. Read the clipboard and paste it to another pane, and
  3. Jump back to the original pane.

All you need to set up vim to copy to clipboard [visual mode Ctrl+c]:

set clipboard=unnamedplus
vnoremap <C-c> "+y

Then have tmux command to run copying from vim and pasting the buffer to another pane [Ctrl+b Enter]:

bind-key Enter run "tmux send-keys -t 0 C-c" \; 
run "tmux select-pane -t 1" \; 
run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer" \; 
run "tmux send-keys -t 1 Enter" \; 
run "tmux select-pane -t 0"

Notice: a) This script requires xclip to interface with the clipboard. b) Assumption is vim is in pane 0; target pane is pane 1.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .