1

I'm fairly new to vim (I'm using nvim) so forgive my lack of knowledge, many questions are similar to mine, but not quite the same.

I would like this:

  • vim by default should cut/copy to a buffer, not system clipboard.
  • vim's other register (I think its +, please correct me if its *!) should go to the system clipboard.

I have done some research but nothing (so far) is working for me.

Info:

  • I use tmux so pushing to tmux buffers.
  • I'm away from home, so currently using termux so termux-clipboard-get/set

Here is what I thought would work in my case:

let g:clipboard = {
  \   'name': 'TmuxTermux',
  \   'copy': {
  \      '*': ['tmux', 'load-buffer', '-'],
  \      '+': ['termux-clipboard-set'],
  \    },
  \   'paste': {
  \      '*': ['tmux', 'save-buffer', '-'],
  \      '+': ['termux-clipboard-get'],
  \   },
  \   'cache_enabled': 1,
  \ }

Now when I cut/copy, tmux buffers share vim's content, PERFECT! (almost...), I cannot use the + buffer to use the system clipboard though, if I press '+' its just moves my cursor down by one line, if I swap the above order (make termux-clipboard-get/set the * register, instead of +), when I press * it highlights all instances of the word my cursor is on.

From reading, I expected:

  • By default, vim use * register, this seems to be true as it successfully registers to tmux buffers with dd/yy and pastes with p (or in tmux with my bind of [PREFIX]+[p]
  • If I want to cut/copy/paste to my other register + (system clipboard), then I would use +dd/+yy and then paste with +p (or even *dd/*yy/*p) but this isnt working for me.

Problem:

As explained above, + will move my cursor down one line, when changing it to the * register, * highlights the current word and any other occurance of it

Apologies to those with more experience, I'm fresh from emacs (with a very sore pinky finger!:)

2
  • 1
    This may or may not help. Be aware that on X11, the clipboard does not really exist! Copy/paste is done by X selections, with two that always exists: PRIMARY and CLIPBOARD. But these are still not real--They're just tokens. When you "copy", the program does not actually copy--it claims ownership of the "CLIPBOARD" token, but does NOT copy to the non-existent clipboard. When another program wants to paste the clipboard, it looks to see which program owns the CLIPBOARD token (more...)
    – C. M.
    Apr 22 '21 at 14:03
  • 1
    (...cont) Once the other program sees that the first program owns the token, it then asks the first program to send that "clipboard data". The data is not actually copied to anything in the OS or in X11. This is why when, in X11, if you copy from a program, then exit the program.. you suddenly cannot paste! The program exited and no longer exists to send the "clipboard data".
    – C. M.
    Apr 22 '21 at 14:07
2

I use vim and tmux and to deal with the system clipboard I have the following:

" copy and paste with clipboard  
                                                                                                                                                        
" paste from system CTRL-C clipboard                                                                                                                                                                                                      
nnoremap <Leader>p "+p                                                                                                                                                                                                                    
" paste from system highlighted clipboard                                                                                                                                                                                                  
nnoremap <Leader>P "*p                                                                                                                                                                                                                    
" copy contents of unnamed register to system CTRL-C clipboard                                                                                                                                                                            
nnoremap <silent><Leader>y :call Preserve("normal! Gp\"+dGu")<cr>                                                                                                                                                                         
            \ :echo 'copied to CTRL-C clipboard'<cr>                                                                                                                                                                                      
" copy contents of unnamed register to system highlghted clipboard                                                                                                                                                                        
nnoremap <silent><Leader>Y :call Preserve("normal! Gp\"*dGu")<cr>                                                                                                                                                                         
            \ :echo 'copied to highlight clipboard'<cr>             

The <Leader>y and <Leader>Y let me pull the contents from vim's unnamed register to the system clipboard. Those two remaps rely on the following function (which must appear before the remaps in the .vimrc:

" run a command, but put the cursor back when it's done                                                                                                                                                                                       
function! Preserve(command)                                                                                                                                                                                                                   
    " Preparation: save last search, and cursor position.                                                                                                                                                                                     
    let _s=@/                                                                                                                                                                                                                                 
    let l = line(".")                                                                                                                                                                                                                         
    let c = col(".")                                                                                                                                                                                                                          
    " Do the business:                                                                                                                                                                                                                        
    execute a:command                                                                                                                                                                                                                         
    " Clean up: restore previous search history, and cursor position                                                                                                                                                                          
    let @/=_s                                                                                                                                                                                                                                 
    call cursor(l, c)                                                                                                                                                                                                                         
endfunction        

N.B. I've not spent a lot of time testing the following, but I find that when I am ssh'ed into a (linux) host, and in tmux session on host (but not on the client) that these remaps seem sometimes to be able to copy from vim's unnamed registers on the host to the system clipboard on the client (and vice versa). I love it when it's behaving like this, but sometimes the two don't talk I don't know why..

Anyway, I hope that helps someone!

1
  • 1
    nice answers @mattb and definately a help, upvoted all of them! the functions provided are great! my actual problem was due to neovim handling the clipboard slightly different to vim, I have to prefix " to the commands e.g "*p or "+p etc, but still some great info for others (and myself)
    – 5c0tt
    Apr 22 '21 at 23:56

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.