Vim Tips Wiki
Line 56: Line 56:
   
 
==Paste from Windows clipboard==
 
==Paste from Windows clipboard==
  +
<CR>
[BR]
 
   
 
The easiest way to do this is to press SHIFT-INSERT in insert mode.
 
The easiest way to do this is to press SHIFT-INSERT in insert mode.

Revision as of 03:05, 10 May 2012

Tip 1623 Printable Monobook Previous Next

created April 10, 2009 · complexity basic · author Ir0nh34d · version 7.0


Using Vim under Cygwin, it is difficult to access the Windows clipboard:

  • Typing "+y does not yank (copy) to the Windows clipboard.
  • Typing "+p does not paste from the Windows clipboard.

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.

Copy to Windows clipboard

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

Paste from Windows clipboard

<CR>

The easiest way to do this is to press SHIFT-INSERT in insert mode.

(By INSERT, I mean the insert key next to the Delete/Home/End keys.)

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

See also

  • fakeclip plugin allowing use of "*p, "*yy and more on Cygwin

Comments

How can we make a "cut" version of copy? I don't know enough about writing functions. Blindly changing "y" to "d" just resulting in it deleting one character only. Robertmarkbram 04:38, 5 August 2009 (UTC)


Changing Putclip to be:

function! Putclip(type, ...) range
    let sel_save = &selection
    let &selection = "inclusive"
    let reg_save = @@

    if a:type == 'n'
        silent exe a:firstline . "," . a:lastline . "d"
    elseif a:type == 'c'
        silent exe a:1 . "," . a:2 . "d"
    else
        silent exe "normal! `<" . a:type . "`>d"
    endif
    call system('putclip', @@)

    let &selection = sel_save
    let @@ = reg_save
endfunction

Should do the trick. --Ir0nh34d 03:36, 7 August 2009 (UTC)


Nice one Ir0nh34d! Changed that to CutClip.. and give it some shortcuts:

" Cut via \x in normal or visual mode.
vnoremap <silent> <leader>x :call Cutclip(visualmode(), 1)<CR>
nnoremap <silent> <leader>x :call Cutclip('n', 1)<CR>
" Cut via Alt+x
vnoremap <silent> ^[x :call Cutclip(visualmode(), 1)<CR>
nnoremap <silent> ^[x :call Cutclip('n', 1)<CR>

I like to use Alt+C or \y for copy and Alt+p or \p for paste. That way, I keep some of the Windows and Vim familiarity about the actions. --Robertmarkbram 08:38, 9 August 2009 (UTC)