Vim Tips Wiki
(Yank Cygwin vim content to the Windows clipboard)
 
No edit summary
Line 3: Line 3:
 
Using this function:
 
Using this function:
   
<pre>function! Putclip()
+
<pre>function! Putclip(type, ...) range
let save_z=getreg('z')
+
let sel_save = &selection
let save_z_type=getregtype('z')
+
let &selection = "inclusive"
normal gv"zy
+
let reg_save = @@
  +
call system('putclip', @z)
 
  +
if a:type == 'n'
call setreg('z', save_z, save_z_type)
 
  +
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', @@)
  +
  +
let &selection = sel_save
  +
let @@ = reg_save
 
endfunction</pre>
 
endfunction</pre>
   
And a simple mapping, such as:
+
And simple mappings, such as:
  +
 
<pre>vnoremap <silent> <leader>y :call Putclip(visualmode(), 1)<CR>
  +
nnoremap <silent> <leader>y :call Putclip('n', 1)<CR></pre>
  +
 
You can visually select some text then yank it to the Windows clipboard, or use the normal yank options (i.e. 3\y to yank 3 lines based from the current line).
   
  +
To allow for usage from the command line, add a command to execute Putclip with a range:
<pre>vnoremap <silent> <leader>y :call Putclip()<CR></pre>
 
   
  +
<pre>com! -nargs=0 -range=% Putclip call Putclip('c', <line1>, <line2>)</pre>
You can visually select some text then yank it to the Windows clipboard.
 

Revision as of 03:27, 10 April 2009

One of the challenges of using Cygwin vim is that it does not yank to the Windows clipboard when you type "+y. As a result, you cannot access any content you've yanked from Cygwin vim with any native Windows application.

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', @@)

    let &selection = sel_save
    let @@ = reg_save
endfunction

And 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 (i.e. 3\y to yank 3 lines based from the current line).

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>)