10

I see a lot of explanations of how to make vim copy to the system clipboard (involving xclip for linux or pbcopy or OSX), but I would like to yank to the tmux clipboard. To be clear, what I want is to position my cursor over a word and hit something like "*yiw and then hop over to another window with tmux (maybe one where I want to paste a large URL for curl or wget) and press Ctrl-B ] to paste (or with whatever tmux prefix key you use). Thanks for help that any one can offer. It would be awesome if this is possible.

4 Answers4

16

If youre working on a terminal and not a local vim process consider using vim-tbone which enables storing selections to the tmux buffer. It is a basic configuration to map the :Tyank :Tput commands to keyboard shortcuts of your choice (I use ty and tp respectively)

Pete weissbrod
  • 161
  • 1
  • 3
4

I prefer to keep the tmux clipboard and system clipboard separate. A little more explanation here, but these are the mappings I use:

vnoremap <leader>tc y<cr>:call system("tmux load-buffer -", @0)<cr>gv
nnoremap <leader>tp :let @0 = system("tmux save-buffer -")<cr>"0p<cr>g;

The main advantage compared to other strategies I've seen is that this faithfully copies the vim selection (including block selection) instead of working line-wise. Similarly, it pastes the tmux buffer at the cursor instead of working line-wise.

One disadvantage would be that for copying it requires a visual selection (instead of e.g. just a movement).

EDIT: If desired, it would probably be easy to modify the copy mapping to something like nnoremap <leader>tc :call system("tmux load-buffer -", @0) -- this would require the user to first yank the text of interest, then would copy whatever they had yanked to Tmux. The advantage here would be that normal vim movement-yank would work without first requiring a visual selection.

n8henrie
  • 305
0

One way is:

In Vim, :only | set nonu In Tmux, ctrl-b z

This makes the buffer occupy entire terminal screen. Then you can use tmux to select the text.

Another way is using tmux block selection:

  1. ctrl-b [ to enter selection mode
  2. ctrl-v SPACE to enter block selection mode
  3. select ONLY the text you want to copy.
Kay
  • 1,251
0

It is not clear why you should use tmux clipboard.

I find it easier to copy to system clipboard from vim or any other application and them paste it on tmux. You could try following on your ~/.tmux.conf:

bind-key -n C-v run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer"

This allows to paste from system clipboard to tmux using Ctrl-V.

For further information on this approach: Tmux: Clipboard Integration.

mMontu
  • 459
  • You are correct. Using a system clipboard as an intermediary seems to be the way to go. Do you know if vim can be configured to yank to a clipboard other that xclip? My idea is that if it could, then this could work on a headless machine without X installed. The binding you gave (and the ones in the link) could certainly be adapted to use another clipboard program. – Andrew Thaddeus Martin Oct 17 '14 at 15:26
  • I believe vim uses the system clipboard, unaware if it is xclip or not. You may find additional information in :help 'clipboard' and on this answer. – mMontu Oct 17 '14 at 16:04
  • This does not answer the question. I explicitly want to isolate the system clipboard, vim clipboard and tmux clipboard because I actually want to hold multiple different buffers in different contexts. This solution will require corrupting my system clipboard every time I want to copy something on tmux. – SOFe Apr 05 '23 at 02:26
  • There are (at least) 2 good reasons for needing the tmux buffer instead of the system one: when being logged over SSH without the -X option (i.e without X11 forwarding), or when the Vim version on the server is compiled without clipboard access (and you do not have the rights to change it). – PlasmaBinturong Feb 21 '24 at 15:58