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.
-
Why don't you simply install a vim built with clipboard support? – romainl Oct 17 '14 at 14:42
-
1That is what I have decided on. It still isn't really satisfactory for a headless machine (without X installed), but it works fine for my usual setup. – Andrew Thaddeus Martin Oct 17 '14 at 15:22
4 Answers
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)
- 161
- 1
- 3
-
3This should be the accepted answer, as it actually answers the original question. The currently accepted answer dodges the question. – Yetanotherjosh May 22 '17 at 21:30
-
-
I forgot I had this vim plugin installed. It does exactly what I wanted. – fourpastmidnight Aug 28 '23 at 16:08
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.
- 305
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:
ctrl-b [to enter selection modectrl-v SPACEto enter block selection mode- select ONLY the text you want to copy.
- 1,251
-
You would probably also need to do
set nolistin case your Vim config displays special characters (whitespaces, newlines). – PlasmaBinturong Feb 21 '24 at 16:00
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.
- 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
-Xoption (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