This is a read-only archive!

Copy/paste in Linux: Eureka

It's been a few years since I officially grasped Linux's (well, X Windows') weird concept of copying and pasting, with its multiple discrete copy/paste methods: the highlight + middle click version, and "clipboard" Edit->Copy" + "Edit->Paste version.

But once in a blue moon, copying and pasting in X still surprises me. Try this:

  1. Open Firefox and a text editor. I'm trying with Vim.
  2. Highlight some text in Firefox.
  3. Middle-click paste it into the editor. The highlighted text is pasted, as expected.
  4. Close Firefox.
  5. Middle-click into the editor again.

Can you guess what happens at the end? If you said "Some random text from another application and/or nothing at all is pasted rather than the stuff from Firefox", you're right!

But today I read this article on jwz.org and finally understood how copy/paste works in X. Highlighting text doesn't copy anything, it just announces to the world "If any applications want to middle-click paste something, come ask me for it". So if you close the application you wanted to paste text from before you actually do the pasting, the application isn't around to give you the text you wanted any more, so you can't get it. The Edit->Copy / Edit->Paste version of copy/paste behaves the same way. You can't "Copy", close app, "Paste".

Note, this is different from how MS Windows works. When you copy some text in Windows it really copies to another location. You can close the app and still paste away. But Windows has a different (inconsistent) behavior when copy/pasting files in Explorer. There, it behaves like X in Linux: if right click a file and "Copy", it doesn't actually do anything with the data until you paste. If you right-click, "Copy", delete the file, "Paste", you don't get an error until you try to Paste.

In Vim in Linux, the "* register lets you access the "primary selection" (highlight / middle click selection), and the "+ register lets you access the clipboard.

In Vim in Windows, "* and "+ do the same thing, and use the clipboard.

September 16, 2008 @ 10:42 AM PDT
Cateogory: Linux

3 Comments

ph0enix
Quoth ph0enix on September 16, 2008 @ 1:14 PM PDT

In windows if you do have a large buffer of data waiting to be pasted and you try to quit the application, it usually asks you if you want to keep the data around for pasting later. I think this applies to certain apps only though.

Jason
Quoth Jason on September 16, 2008 @ 1:51 PM PDT

Actually, Windows does something similar to X with a caveat. The clipboard on either system can offer multiple manifestations of the same copied material, say with a Word document another program can ask for the plain text or the text with formatting, or with a graphics program offer the copied data in multiple formats and color depths. When a program that currently 'owns' the clipboard quits, Windows asks for the plain text and/or basic BMP representation of the copied content and then announces that Windows itself owns the clipboard again, offering just the plaintext version that it pulled and not the one with any formatting. Things like the MS Word "Clipboard Manager" enhance the process by hijacking that system, pulling the well known formats from the clipboard when it loses control, storing them in a list, and then announcing control of the clipboard again, offering up the content it pulled from the other program. Most X environments are good citizens and offer plain text, the only problem is that nobody's really used it since the mid 90s. And then there's the FreeDesktop.org clipboard standard which is different, and Gnome apps and KDE apps can use Ctrl-C/Ctrl-X/Ctrl-V to copy to a totally different clipboard, basically meaning you can use the one with highlighted text and the one that acts like Windows' as separate short-term and medium-term clipboards with different content.

Andreas Grech
Quoth Andreas Grech on June 24, 2010 @ 12:18 PM PDT

I don't know if this is the normal behaviour, but on my Ubuntu (10.04 LTS), whenever I do the Edit->Copy routine, it writes into both the "+ and "* registers.

On the other hand, the middle-click routine works as expected by writing only to the "* register.