5

I am trying to copy a piece of text to the system clipboard. First, I do a visual selection: select " then + then y.

I then get a message 7 lines yanked. When I press p, the data is pasted in vim; but, when I'm trying to paste data to any other program - no result. How do I solve this problem?

4
  • possible duplicate of Copy to Primary Selection in VIM – muru Aug 9 '15 at 14:49
  • 1
    I don't use VIM, but in general for copy-pasting things from a terminal window: Select text with the mouse, right-click and "copy", or hit CTRL+INS once it's selected. Insert text into a terminal with SHIFT+INS – Byte Commander Aug 9 '15 at 14:56
  • Also CTRL+SHIFT+C works, in the same way CTRL+SHIFT+V will paste the clipboard content inside the terminal – kos Aug 9 '15 at 15:56
  • @ByteCommander that does not work if you have mouse enabled in vim. – cserpell Dec 30 '20 at 16:13
7

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 "*yor "+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+=ainto 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.

2
  • 4
    Just simply installing vim-gtk did the trick for me. – Cnly Nov 16 '17 at 13:37
  • It is worth noting that you may need to uninstall the package vim, or change the alternative for vim manually. See askubuntu.com/a/1106538/568304 – Murphy Jan 3 '19 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.