5

I'm using Debian "Buster" (version 10.3) with GNOME Desktop version 3.30.2.

Is there a way to clear the clipboard on demand, using Terminal command(s) only and without installing any clipboard manager?

Thank you for your time!

PajuranCodes
  • 303
  • 3
  • 12
  • 43

2 Answers2

8

You can clear the X selection clipboard (the selection you can middle-click to paste) with xclip (install on Debian/Ubuntu-based systems with sudo apt install xclip).

Once that's installed, you can just run it:

printf '' | xclip

To remove the actual clipboard (which requires a key sequence to copy or paste), call it out explicitly:

printf '' | xclip -selection clipboard

You may need to first run export DISPLAY=:0 if you're running this from a different session, and that assumes you're running X11 on display zero. This should be automatically taken care of for you.

These work by storing empty strings into the respective clipboards.

There are clipboard managers like clipman that save your clipboard history. This does not interact with them.

Adam Katz
  • 14,455
  • 5
  • 68
  • 83
  • Hi, Adam. First of all, thanks for the answer! Well, I tried both commands, after installing `xclip`. Each time, I receive the following two lines: `No protocol specified`, followed by `Error: Can't open display: :0`. Any thoughts, please? I suppose, it has something to do with your `export DISPLAY=:0` advice. Unfortunately, since I'm not a Linux pro, I don't understand it at all. Some step-by-step procedures would probably help me. Thanks. – PajuranCodes Apr 05 '20 at 13:36
  • @dakis – If you can't open display `:0` then that's not your display. Open a terminal emulator in GNOME (even if it's not the one you want to ultimately use) and type `echo $DISPLAY`. Use that value instead of `:0`. If the environment variable isn't working, try `xclip -d :0` (replacing `:0` as needed). I'm not sure what the `No protocol specified` part means (the man page has no clues there), but try `xclock` or `gnome-terminal` to ensure you've at least got your `$DISPLAY` sorted out. – Adam Katz Apr 06 '20 at 13:02
  • Hi again, Adam. Your answer is correct! Well, until yesterday evening I never defined an environment variable in Debian. Though the faith made so, that i had to learn about it (when trying to install Dart Sass instead of the now deprecated Ruby Sass). So, today I decided to read and try your answer and suggestions again. This is how I proceeded overall: – PajuranCodes Apr 07 '20 at 12:38
  • 1) I've logged in into Terminal as the root user and I installed `xclip` with: `apt install xclip`. 2) I executed `echo $DISPLAY`. The result was: `:0`. 3) I closed all Terminal windows. 4) In the file `/home//.bashrc` I added the line `export DISPLAY=:0` and saved the change. 5) I opened Terminal again - therefore beeing logged in as the current user, not as the root user - and wrote the two commands from your answer: `printf '' | xclip` and `printf '' | xclip -selection clipboard`. Et voila: the second command cleared my clipboard... – PajuranCodes Apr 07 '20 at 12:46
  • Thank you for everything! Now I can work on somehow creating and saving a script to clear the clipboard when I click on it. Cool stuff, I think :-) Good luck! – PajuranCodes Apr 07 '20 at 12:47
  • I do not recommend adding that to your `~/.bashrc` file, _especially_ without first checking for whether the variable is already defined. Consider `if [ -z "$DISPLAY$SSH_TTY" ]; then export DISPLAY=:0; fi` as a safety. This helps ensure you don't overwrite another display or remotely affect your local session's UI. – Adam Katz Apr 07 '20 at 16:04
  • I wrote the if-statement, that you recommended, in the `~/.bashrc` file and it works. Thanks. – PajuranCodes Apr 10 '20 at 15:31
  • You can also use `echo -n`. – Nishant Apr 08 '22 at 20:40
1

You need to be running Xorg session but you can use xsel to manipulate the clipboard. Not sure if this works in Wayland.

parry
  • 504
  • 3
  • 9
  • Hi, parry. I made a lot of tests with "xsel". Unfortunately none of them could clear the clipboard. I tend to think, that this have, indeed, something to do with Wayland. – PajuranCodes Apr 10 '20 at 15:36