46

It probably happened to you as well - sometimes when you copy a text from some web page into your rich-text e-mail draft in your favorite webmail client, you dislike the fact that the pasted piece has a different font/size/weight.. it somehow remembers the style (often images, when selected). How is it than that if you paste the same into your favorite text editor like Vim, there's no HTML, just the plain text?

alt text

It seems that clipboard maintains the selected data in various formats. How can one access data in any one of those formats (programmatically or with some utility)? How does the X11 clipboard work?

codingbadger
  • 42,678
  • 13
  • 95
  • 110
mykhal
  • 19,175
  • 11
  • 72
  • 80
  • i had an idea for a while, that browsers maintains their own clipboard as well, but it would mean that it would work only inside that browser app process, which is not the case. – mykhal Aug 26 '10 at 00:33
  • Related, if you want to see the mime type from cli: https://unix.stackexchange.com/questions/163081/application-that-allows-to-show-clipboard-contents-and-its-mime-type – aksh1618 May 04 '23 at 10:12

2 Answers2

39

The app you copy from advertises formats (mostly identified by MIME types) it can provide. The app you paste into has to pick its preferred format and request that one from the source app.

The reason you may not see all style info transferred is that the apps don't both support a common format that includes the style info.

You can also see issues because an app may for example try to paste HTML, but not really be able to handle all HTML. Or the apps may be buggy, or may not agree on what a particular MIME type really means.

Almost all apps can both copy and paste plain text, of course, but beyond that it's touch and go. If you don't get what seems to make sense, you could file a bug vs. one of the apps.

You may notice that if you exit the app you're copying from, you can no longer paste. (Unless you're running a "clipboard manager" or something.) This is because no data actually leaves the source app until the destination app asks for a format to paste. There are "clipboard managers" that ask for data immediately anytime you copy and store that data, so you can paste after the source app exits, but they have downsides (what if the data is huge, or is offered in 10 formats, etc.)

The following python code will show available formats for the currently-copied data, if you have pygtk installed. This app shows the ctrl+c copied data, not the middle-click easter egg. (See http://freedesktop.org/wiki/Specifications/ClipboardsWiki)

#!/usr/bin/python

import gtk;
clipboard = gtk.clipboard_get()
print("Current clipboard offers formats: " + str(clipboard.wait_for_targets()))
Havoc P
  • 8,365
  • 1
  • 31
  • 46
  • thanks for very helpful info (i was afraid the answer will include t[eo]ns if lines of C code..) now i just wonder why is the text/html data copied from firefox (utf-8 web page) in utf-16le encoding.. – mykhal Aug 26 '10 at 17:27
  • Followup, of a sort: http://askubuntu.com/questions/427704/how-can-i-edit-the-source-of-html-in-the-clipboard . Any tips on actually trying to fetch rich text from the clipboard? I'm too lazy to figure out how to create a buffer. – Adam Monsen Feb 28 '14 at 23:45
  • I couldn't get the current code to work. [This ones does though](https://gist.github.com/Hi-Angel/ced1fbaddd73d51e6a4e36f30d252648). I also took a liberty of breaking the output to multiple lines so it is more readable. Feel free to use it in your answer :) – Hi-Angel May 06 '20 at 14:25
  • 4
    I wrote a little utility that goes over all available formats, and prints their content in textual form https://github.com/Hi-Angel/scripts/blob/master/print_clipboard_content.py – Hi-Angel May 06 '20 at 16:02
  • 3
    One-liner to list targets (formats) with just `xclip`: `xclip -selection clipboard -target TARGETS -out` – Vladimir Panteleev Mar 07 '22 at 18:26
4

The code in Havoc P's answer to show the formats of the current clipboard sadly no longer works due to an API change in PyGTK. Here's an updated version as a one-liner:

python -c 'import gi; gi.require_version("Gtk", "3.0"); from gi.repository import Gtk, Gdk; print(*Gtk.Clipboard.get(Gdk.atom_intern("CLIPBOARD", True)).wait_for_targets()[1], sep = "\n")'

In Arch Linux, you can install PyGTK using sudo pacman -S pygtk.

Below are some examples.

Text from Chrome:

TIMESTAMP
TARGETS
SAVE_TARGETS
MULTIPLE
STRING
UTF8_STRING
TEXT
text/html
text/plain

Text from Gnome Terminal:

TIMESTAMP
TARGETS
MULTIPLE
SAVE_TARGETS
UTF8_STRING
COMPOUND_TEXT
TEXT
STRING
text/plain;charset=utf-8
text/plain
ZimbiX
  • 485
  • 3
  • 7