created April 10, 2009 · complexity basic · author Ir0nh34d · version 7.0
Using Vim under Cygwin, it is difficult to access the Windows clipboard:
"+ydoes not yank (copy) to the Windows clipboard.
"+pdoes not paste from the Windows clipboard.
"*pinstead seems to work however
As a result, you cannot easily use the clipboard to copy text between Cygwin Vim and a native Windows application. This tip shows some workarounds.
Using this function:
function! Putclip(type, ...) range let sel_save = &selection let &selection = "inclusive" let reg_save = @@ if a:type == 'n' silent exe a:firstline . "," . a:lastline . "y" elseif a:type == 'c' silent exe a:1 . "," . a:2 . "y" else silent exe "normal! `<" . a:type . "`>y" endif "call system('putclip', @@) " if you're using an old Cygwin "call system('clip.exe', @@) " if you're using Bash on Windows "As of Cygwin 1.7.13, the /dev/clipboard device was added to provide "access to the native Windows clipboard. It provides the added benefit "of supporting utf-8 characters which putclip currently does not. Based "on a tip from John Beckett, use the following: call writefile(split(@@,"\n"), '/dev/clipboard') let &selection = sel_save let @@ = reg_save endfunctionAnd simple mappings, such as:
vnoremap <silent> <leader>y :call Putclip(visualmode(), 1)<CR> nnoremap <silent> <leader>y :call Putclip('n', 1)<CR>
You can visually select some text then yank it to the Windows clipboard, or use the normal yank options (for example,
3\y to yank 3 lines based from the current line, assuming the default backslash leader key).
To allow for usage from the command line, add a command to execute
Putclip with a range:
com! -nargs=0 -range=% Putclip call Putclip('c', <line1>, <line2>)
Copy using `/dev/clipboard` Exclusively
You can eliminate the hassle of a vimscript if you choose, by selecting the text in visual mode, then entering the following...
:'<,'>w !cat > /dev/clipboard
Then of course you could assign a key combination or whatever you want to the above. I like how explicit this is within the Cygwin environment.
Note: The syntax above is `w !` NOT `w!` which do different things. Remember to include the space.
The easiest way to do this is to press SHIFT-INSERT in insert mode.
(The insert key is next to the Delete/Home/End keys.)
Another option is to use this function:
function! Getclip() let reg_save = @@ "let @@ = system('getclip') "Much like Putclip(), using the /dev/clipboard device to access to the "native Windows clipboard for Cygwin 1.7.13 and above. It provides the "added benefit of supporting utf-8 characters which getclip currently does "not. Based again on a tip from John Beckett, use the following: let @@ = join(readfile('/dev/clipboard'), "\n") setlocal paste exe 'normal p' setlocal nopaste let @@ = reg_save endfunction
And a simple mapping, such as:
nnoremap <silent> <leader>p :call Getclip()<CR>
You can use the normal paste options (for example,
\p to paste the clipboard contents, assuming the default backslash leader key).
"*yyand more on Cygwin plugin allowing use of