0

On a restricted system where installing new tools is not possible, how could one copy a string into the X Window clipboard (the clipboard that works with Ctrl+C / Ctrl+V) with the command line or at least in an automated way, without using xsel nor any other such third-party tool?

I am using Red Hat.

I would find it hard to believe that achieving such a basic task could not be done using native Linux only.

5
  • 2
    What does "native Linux only" mean? xsel is a native Linux program, why don't you want to use that? And what Linux distribution are you using?
    – terdon
    Commented Sep 14, 2021 at 10:09
  • I am using Red Hat. On some restricted systems you cannot install packages using a package manager such as apt, and also it is fairly complicated to install from sources. That is why I would need a solution that only uses Linux features that are "natively" present on the system. Something like "echo my_string > /dev/clipboard" would be perfect.
    – Ramanewbie
    Commented Sep 14, 2021 at 10:22
  • 1
    OK. But just so you know, none of that is a "Linux" feature which is why the question is confusing. Remember that Linux is just a kernel, nothing more, so everything is done using "3rd party tools". Are you aware of middle click paste? You can select anything with your mouse, and that automatically copies it to a (different, not the X Window, clipboard), and you can then paste it by middle clicking. Perhaps that is an acceptable workaround for you? I don't think what you are asking for is possible without using something like xsel. That's why xsel exists, after all.
    – terdon
    Commented Sep 14, 2021 at 10:41
  • @terdon xsel and xclip are third-party in the sense that they aren't part of the basic X.org tool set. Commented Sep 14, 2021 at 11:55
  • @terdon Middle click paste is not an acceptable workaround because it is not really automated given that it requires a user input action (the press of a mouse button.).
    – Ramanewbie
    Commented Sep 15, 2021 at 20:51

1 Answer 1

2

The Python standard library includes the tkinter module (Tkinter in Python 2), and you can access the X clipboard through that.

python3 -c 'import sys, tkinter; sys.stdout.write(tkinter.Tk().clipboard_get())'
python2 -c 'import sys, Tkinter; sys.stdout.write(Tkinter.Tk().clipboard_get())'

You can also set the clipboard content, but that's a bit more invasive, because the clipboard content is owned by a running application. Programs like xsel leave a background process running while they own the clipboard. Here's a simple code fragment that you need to keep running until you're no longer interested in the content; close the window when you're done.

echo -n 'new clipboard content' | python3 -c 'import tkinter; w = tkinter.Tk(); w.clipboard_clear(); w.clipboard_append(input()); w.mainloop()'
echo -n 'new clipboard content' | python2 -c 'import Tkinter, sys; w = Tkinter.Tk(); w.clipboard_clear(); w.clipboard_append(sys.stdin.read()); w.mainloop()'

(There are better ways to access the clipboard in Python, such as pyperclip which is cross-platform and doesn't require displaying a window, but they aren't part of the standard library.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .