7

Say I pick some png file and run the following command:

xclip -selection clip -t image/png image.png

I now have that image in my clipboard. If I paste it somewhere that accepts an image, it is pasted as expected and all is good.

Now instead consider that I press ctrl+V while being in the firefox address bar or in the text field I'm typing this in. The binary contents of the file are pasted verbatim into the text field, in some cases causing the browser to hang for a while.

Of course I know it doesn't make sense to paste an image there, but I sometimes do it accidentally, and then it causes problems.

When I instead paste an image I copied using firefox's "copy image" button, it doesn't get pasted when I try to paste it, so it must be possible to store it in the clipboard to allow for this behaviour.

How can I place an image in the clipboard without making the image get pasted verbatim as binary data into text fields? If it's possible to somehow place both an image and a text string (such as the path to the image or something) in the clipboard and have it pick the appropriate one when pasting, that would be awesome.

1
  • X allows you to present the selection (there's no real "clipboard") in multiple formats, so the pasting application can choose the "best" one. AFAIK xclip can't do that. You'd have to modify the source code of xclip. – dirkt Jul 3 '17 at 18:38
5

I copied an image into clipboard with xclip like you did and here's what list of targets I got:

> xclip -selection clip -t TARGETS -o
TARGETS
image/png

and now if I copy an image from a web page I get this:

> xclip -selection clip -t TARGETS -o
TIMESTAMP
TARGETS
MULTIPLE
SAVE_TARGETS
text/html
text/_moz_htmlinfo
text/_moz_htmlcontext
image/png
image/jpeg
image/x-icon
image/x-ico
image/x-win-bitmap
image/vnd.microsoft.icon
application/ico
image/ico
image/icon
text/ico
image/tiff
image/bmp
image/x-bmp
image/x-MS-bmp

and for example setting target as text/html gives such output

> xclip -sel c -t text/html -o       
<img src="..." alt="...">>

So obviously it's xclip problem as stated in prev answer

1

The above answer appears to be either incomplete or incorrect. After copying an image from Mozilla Firefox, I get the following output:

$ xclip -selection clip -t TARGETS -o
TIMESTAMP
TARGETS
MULTIPLE
SAVE_TARGETS
text/html
text/_moz_htmlinfo
text/_moz_htmlcontext
image/png
image/bmp
image/x-bmp
image/x-MS-bmp
image/x-icon
image/x-ico
image/x-win-bitmap
image/vnd.microsoft.icon
application/ico
image/ico
image/icon
text/ico
image/jpeg

As the previous awnser states, output as "text/html" will output a valid html document with a link to the image. However, if you choose "image/png" (or any of the similar options), it outputs the raw png data.

Tl;dr: xclip -selection clip -t TARGETS -o tells you what formats you can paste from. Use xclip -selection clip -t '$TYPE' -o to select one. This can be directly saved to a file, and works almost exactly as expected.

1
  • I’m not sure I understand. Are you saying that the user should type '$TYPE'? – Scott Apr 9 '20 at 4:13

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.