"+y does not copy to the clipboard
Solution 1:
Type in the terminal:
$ vim --version | grep -e version -e +mouse.* -e +clipboard
Ideally you have the huge
version installed with the clipboard feature as well as a number of mouse features compiled-in. Just in case, please, report the result by editing your question.
vi/vim use different clipboard(s) from your normal GUI: i.e. it has its very own clipboard, so if you try to copy/paste from a vim-opened file to anywhere outside vim with your mouse using CTRL+x/CTRL+v, you'll end up with .... nothing.
I) try to use your mouse (after that I will get to your exact question)
Open a file in vim and highlight text with your left mouse button pressed down. Click on the highlighted text with the right button. If you don't see an enabled "copy" contextual menu choice, it probably means that you have set
set mouse=a
somewhere in your vi/vim configuration. This option allows you to use your mouse in the 4 most common vim modes: normal (escaped), insert, visual, cmd-line modes.
Type :help mouse
in your vi/vim session (normal mode) if you want to know more. If you have that option enabled you can still copy in vi and paste outside vim with your mouse. For that, just press shift as you highlight your text in vi/vim. Your vi/vim session shell will behave as if set mouse=a
had not been set just for the duration of that task. This is referred to as the "xterm handling" of the mouse. You will encounter that expression if you decide to read technical stuff on this.
II) use registers from the visual mode within vi/vim
There are two registers you can invoke from the cmd-line while in vi/vim, as you try to copy something to later paste it outside vim. I will call them the "* and the "+ registers (not their real names).
- position your cursor in visual mode and highlight text
- if you yank text with
"*y
, then you can normally paste outside vi/vim, say in an .odt file, using the middle button of your mouse. The other register is NOT affected. - if you yank with
"+y
, you actually yank to and subsequently paste from what's called the "clipboard". In that case you will clobber whatever was in the clipboard up to that instant. To paste outside vi/vim, just use the usual CTRL+v`.
Note: After pasting, neither registers' state changes. They still contain the same stuff as before. You can go back to vi/vim and paste their content with either "*p
or "+p
(after the cursor) or perhaps also just p
.
You can actually change the above behavior by making everything paste to the clipboard. Do :help clipboard
from within the normal vi/vim mode to find out more about the clipboard. Usually having differentiated registers accessible with either "*y
or "+y
is fiendishly practical, because when you want to preserve what is already in the clipboard, you just use "*y
to yank or "*d
to cut out -> no disruption to the clipboard there!
If somehow using "+y
or "+d
doesn't work for you, as seems to be case it is maybe because you have NOT installed all the compiled feature you need for your vim. In particular if you don't have the "clipboard" feature, go ahead and install the package vim-gtk
from the Ubuntu Software Center, or:
$ sudo apt-get install vim-gtk
That will set you up nicely, along with extra nice add-ons to interactively look at files written in C, Python, latex, Perl, Ruby and others, not to mention local html doc for vim if you need it.
... which you do if you are going to do anything with vim really. ;-)
If you do that, you might want to close all your vim sessions and perhaps even source your .vimrc
file, although I am not clear on that. Logging out and back in won't hurt though.
And this was just scratching the surface of vi...
EDIT:
For section I): you can arrange for automatic X11 primary "*y
register copying upon selecting with the mouse in visual mode, by including set go+=a
into yr ~/.vimrc
file.
For section II): If you have the clipboard feature compiled in your install of vim, you can map clipboard copying to CTRL+c by inserting :vmap <C-C> "+y
in your ~/.vimrc. Thereafter you will only need to highlight your text, do CTRL+c to copy to clipboard, and CTRL+v to paste anywhere outside vi/vim. No more "+y
needed here in this case. This specific mapping is only valid for the visual mode.