Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "copy to clipboard" in vim of Bash on Windows? [closed]

I used to use "+y to copy text to system's clipboard, but it doesn't work in vim of Bash on Windows.

like image 311
nbystndr Avatar asked Jun 11 '17 05:06

nbystndr


People also ask

Can you copy from vim to clipboard?

In vim command mode press v , this will switch you to VISUAL mode. Move the cursor around to select the text or lines you need to copy. Press y , this will copy the selected text to clipboard.

How do I copy and paste from clipboard in vim?

Press v to select characters, or uppercase V to select whole lines, or Ctrl-v to select rectangular blocks (use Ctrl-q if Ctrl-v is mapped to paste). Move the cursor to the end of what you want to cut. Press d to cut (or y to copy). Move to where you would like to paste.

How do I enable clipboard in vim?

You need to make sure clipboard is activated (which is probably not the case). if you get "-clipboard" then you would have to install vim again with the "clipboard" functionality. You can do it it by installing "vim-gtk3" or "gvim".


1 Answers

Since neither "*y nor "+y (...) work, an alternative is the following:

Add this to your .vimrc and create a file called ~/.vimbuffer

" copy (write) highlighted text to .vimbuffer vmap <C-c> y:new ~/.vimbuffer<CR>VGp:x<CR> \| :!cat ~/.vimbuffer \| clip.exe <CR><CR> " paste from buffer map <C-v> :r ~/.vimbuffer<CR> 

Higlight any text using visual or visual-block and press ctrl-c. Paste copied text outside your terminal using the usual method or paste copied text to any vim pane using ctrl-v in normal or visual mode.

Ctrl-c yanks the selected text, overwrites ~/.vimbuffer with the selected text, runs a UNIX command to pipe out the data from ~/.vimbuffer to clip.exe.

Any further improvement (e.g.: making command silent) is much appreciated!

Edit: command can now copy strings with any length, not just whole lines. Old command:

vmap <C-c> :w! ~/.vimbuffer \| !cat ~/.vimbuffer \| clip.exe <CR><CR>

Edit2: @Tropilio below has a much cleaner approach down in the comments using system events. I've been using that for a year now.

like image 100
davidanderle Avatar answered Sep 19 '22 22:09

davidanderle