Wikia

Vim Tips Wiki

Using the Windows clipboard in Cygwin Vim

Talk0
1,613pages on
this wiki
Revision as of 07:56, July 11, 2012 by JohnBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 clipboardEdit

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

Copy using `/dev/clipboard` Exclusively Edit

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.

Paste from Windows clipboardEdit


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

See alsoEdit

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

CommentsEdit

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)

Around Wikia's network

Random Wiki