Vim Tips Wiki
Advertisement
Tip 1511 Printable Monobook Previous Next

created July 23, 2007 · complexity basic · author Robert Iannucci · version 7.0


I know that this has been covered in other places, but I was dissatisfied with the ability to only copy and paste line-wise. I found it particularly annoying when I just wanted to yank a single word out to do a web search or similar. After a bit of poking around with Vim's builtin functions (on version 7 at least), I discovered the following workaround:

" On OSX
vmap <C-c> y:call system("pbcopy", getreg("\""))<CR>
nmap <C-v> :call setreg("\"",system("pbpaste"))<CR>p
" On ubuntu (running Vim in gnome-terminal)
" The reason for the double-command on <C-c> is due to some weirdness with the X clipboard system.
vmap <C-c> y:call system("xclip -i -selection clipboard", getreg("\""))<CR>:call system("xclip -i", getreg("\""))<CR>
nmap <C-v> :call setreg("\"",system("xclip -o -selection clipboard"))<CR>p

But the double command problem seems to be caused by the xclip utility. That bug is present in xclip 0.08-7. But xclip 0.11 downloaded from sourceforge works fine and:

vmap <C-c> y: call system("xclip -i -selection clipboard", getreg("\""))<CR>

is sufficient.

" groovyness in Insert mode (lets you paste and keep on typing)
" This blows away i_CTRL-V though (see :help i_CTRL-V)
imap <C-v> <Esc><C-v>a

"+ and "* are supposed to 'do the right thing', but there isn't any such integration with OS X, and I have simply had complete failure with ubuntu (even with +xterm_clipboard +clipboard +X11).

Though, it has been reported to work fine on Ubuntu 8.04 with default packages (vim 7.1-138).

One other thing to note: mapping <C-v> blows away the whole visual block mode, but I never use it. A possible alternative mapping would be to map these with something like ':vnoremap y' so that it's automatic in visual mode.

Comments

 TO DO 

  • Explain the point of this tip. Why is it necessary to avoid using the copy/paste mechanisms provided by Vim?
  • Is this tip only related to Mac OS X or the Ubuntu Linux? If so, state that at the top.
  • Is there some problem with Vim (what version?) on Mac OS X? Exactly what? Has the appropriate mailing list been informed?
  • Do other users experience this problem? If not, should the tip be flagged for deletion?
  • VimTip1199 claims to be related to character-wise pasting. Is it helpful?

To address the first question, the point of mapping <C-c> is to be able to copy large blocks of text from vim into another application. I use this to copy from vim into an rdesktop session, for example.

But my point in the above "todo" is that the tip needs to read coherently for people who might not be familiar with the particular problem that the tip addresses. For example, Vim is run by a lot of Windows users, where the idea of having trouble copying between apps is totally unknown. The text in the tip only makes sense to people who are used to having the problem.
Also, you might use <C-c> to copy large blocks of text, but the author of the tip explicitly talks about yanking a single word. The text should make sense: Is the tip only for short snippets? Do all Mac OS X users need this tip to copy between apps? What about Ubuntu users?
Vim has "+y and "+p. The tip needs to explain when that is not adequate.
If anyone has some suggestions, please edit the tip, or add suggested text below. In time, suggestions could be enhanced and merged into the tip. --JohnBeckett 03:34, 11 November 2008 (UTC)

Advertisement