96
\$\begingroup\$

Following the best security practices, I keep a plain text file with my passwords on my hard drive. In fact, I just copied and pasted one of them to access my PPCG account.

In a lucid moment, I decide that the password should better not remain in the clipboard after its use. Since this happens to me often, I could use a program to remove the clipboard contents.

Challenge

Write a program or function that deletes or overwrites any text contained in the clipboard, using a programming language of your choice.

Additional details:

  • If your system has several clipboards, you can write the program for any one of them. The only requirement is that it must be possible for the user to copy and paste text using that clipboard.

  • If your clipboard keeps a history of recent copied entries, assume the most recent entry.

  • If your answer is specific to an operating system or clipboard, indicate it in the title of your post, together with the used language.

  • The clipboard is guaranteed to contain text when your program is run. You can either delete the contents from the clipboard, or overwrite it with something else (not necessarily text). The only requirement is that after running the program, pasting from the clipboard will not produce the original text.

  • If you choose to overwrite with some fixed or randomly chosen text, you can assume that the previous clipboard contents are different from that text, so the password is effectively removed. In other words, disregard the possibility that the filler text coincides with the password.

  • The program should not have any side-effects like restarting the system, closing programs, shutting down the computer, or freezing it. After your program is run, the user should be able to keep using the computer as normal, only with the password removed from the clipboard. Also, standard loopholes are forbidden.

Shortest code in bytes wins.

\$\endgroup\$
  • \$\begingroup\$ What if the clipboard has an undetermined number of entries? \$\endgroup\$ – LarsW Feb 26 '17 at 19:27
  • 20
    \$\begingroup\$ On a more serious note: Use something like keepass to do that for you. \$\endgroup\$ – flawr Feb 26 '17 at 21:14
  • 17
    \$\begingroup\$ "Disregard the possibility that the filler text coincides with the password." Solution: ` `, 0 bytes. Overwrites the password with itself. \$\endgroup\$ – Esolanging Fruit Feb 27 '17 at 3:10
  • 2
    \$\begingroup\$ @Challenger5 but that's only If you choose to overwrite with some fixed or randomly chosen text \$\endgroup\$ – Luis Mendo Feb 27 '17 at 10:08
  • 11
    \$\begingroup\$ The very worst part about this question is that I can completely relate to the intro story despite knowing that it's satirical. ...so, keepass, huh? Should look into that... \$\endgroup\$ – Nat Feb 27 '17 at 11:01

46 Answers 46

4
\$\begingroup\$

Ruby, 11 bytes (Windows)

`echo|clip`

Runs the windows clip command to replace clipboard contents with "ECHO is on."

\$\endgroup\$
4
\$\begingroup\$

Cow, 9 bytes

OOOMMMOOO

Explanation:

OOO set current memory block to 0
MMM if the current memory block is 0, paste the clipboard and clear the clipboard.
OOO set it back to 0
\$\endgroup\$
3
\$\begingroup\$

Clojure, 117 bytes

#(.setContents(.getSystemClipboard(java.awt.Toolkit/getDefaultToolkit))(java.awt.datatransfer.StringSelection."")nil)

Basically the Java answer. Gets the system clipboard, and sets it to an empty string.

AWT lets you do so many random things. It's so verbose though!

Unfortunately, trying to put an import in there adds about 30 bytes, so I need to fully qualify everything.

(defn clear-clipboard []
  (.setContents
      (.getSystemClipboard
        (java.awt.Toolkit/getDefaultToolkit))
      (java.awt.datatransfer.StringSelection. "")
      nil))
\$\endgroup\$
3
\$\begingroup\$

APL (Dyalog APL) on Windows, 27 bytes

⎕A⎕WC'Clipboard'('Text' '')

⎕A using the name "ABC...Z",

⎕WCWindow Create a

'Clipboard' object with the

('Text' '') ""

\$\endgroup\$
3
\$\begingroup\$

Racket, 61 bytes

Clears the secondary X11 clipboard, or the usual clipboard on other systems.

#lang racket/gui
(send the-clipboard set-clipboard-string""0)
\$\endgroup\$
3
\$\begingroup\$

VB.NET, 29 bytes

Sub F
Clipboard.Clear
End Sub

Saved 2 bytes thanks to @hvd

\$\endgroup\$
  • 1
    \$\begingroup\$ Can you drop ()? \$\endgroup\$ – Qwertiy Feb 26 '17 at 20:40
  • 2
    \$\begingroup\$ Why? dotnetfiddle.net/nZNS1E \$\endgroup\$ – Qwertiy Feb 26 '17 at 21:11
  • 1
    \$\begingroup\$ Edit file outside of VS. You can also compile it from command line, but I think VS will compile it unchanged if it was saved from the other program. \$\endgroup\$ – Qwertiy Feb 26 '17 at 21:19
  • \$\begingroup\$ Error: comment chain does not make sense. Please insert information about deleted comment >> \$\endgroup\$ – CalculatorFeline Feb 27 '17 at 4:01
  • 1
    \$\begingroup\$ For further golfing, you can drop (). :) They're equally optional in a sub or function definition. \$\endgroup\$ – hvd Feb 28 '17 at 18:35
3
\$\begingroup\$

Linux shell (text console), 10 (or 6) bytes

gpm -k;gpm

gpm(8) is the daemon providing mouse control for the Linux text terminal (not X11). gpm -k tells the daemon to shut down, no further pasting will be possible.

It could be shortened to just

gpm -k

which shutdowns the daemon but does not restart it - but it cold be argued that this goes against the "user should be able to keep using the computer as normal" requirement.

\$\endgroup\$
  • \$\begingroup\$ Yeah I would say it does. It is just supposed to clear it \$\endgroup\$ – Christopher Mar 1 '17 at 19:30
3
\$\begingroup\$

Julia - 13 bytes

clipboard("")
\$\endgroup\$
  • \$\begingroup\$ Hardly needing any explanation :-) \$\endgroup\$ – Luis Mendo Mar 2 '17 at 0:30
3
\$\begingroup\$

Cygwin (bash), 10 bytes

w>/dev/cl*

In Linux everything is a file. Except the clipboard. That is part of a program. How impure! Thankfully when Windows became enlightened with true ttys and bash, it was not hindered by the ancient blotch X. On Windows you can use the clipboard as file, the way it was meant to be used. (Ignore that this is worse for golfing).

Needs the procps-np package for w. In other mintty terminals (git bash), ls could be used.

\$\endgroup\$
  • \$\begingroup\$ Assuming /cygdrive/c/WINDOWS/system32 is on your $PATH, cd|clip also works. \$\endgroup\$ – David Conrad Mar 4 '17 at 0:08
2
\$\begingroup\$

AutoIt, 11 bytes

ClipPut("")

Fills the clipboard with an empty string.

\$\endgroup\$
2
\$\begingroup\$

Ruby 25 bytes (MacOS)

IO.popen("pbcopy","w"){}

Launches the MacOS/OS X pbcopy as a subprocess and clears the clipboard.

\$\endgroup\$
  • 1
    \$\begingroup\$ This can be golfed down to 11 bytes: `:|pbcopy` \$\endgroup\$ – Synoli Feb 27 '17 at 21:19
2
\$\begingroup\$

Tcl, 15 bytes.

Not the shortest but the most readable:

clipboard clear

note: must be executed with wish instead of tclsh

\$\endgroup\$
  • \$\begingroup\$ I think you should remove the note and change title to Tcl/Tk \$\endgroup\$ – sergiol Jun 23 '17 at 7:47
2
\$\begingroup\$

Java, 56 Bytes

javafx.scene.input.Clipboard.getSystemClipboard()::clear

It can be used with an functional interface (for example java.lang.Runnable) just like

()->javafx.scene.input.Clipboard.getSystemClipboard().clear()

which was accepted as a function on page 1.

Full example:

public class Test
{
    public static void main(String... args)
    {
        Runnable test1 = () -> javafx.scene.input.Clipboard.getSystemClipboard().clear();
        Runnable test2 = javafx.scene.input.Clipboard.getSystemClipboard()::clear;

        // both are valid Runnable Objects on which you can call the run()-Method to clear the clipboard
        test1.run();
        test2.run();
    }
}
\$\endgroup\$
1
\$\begingroup\$

Java, 139 bytes

enum c{;static{java.awt.Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new java.awt.datatransfer.StringSelection(""),null);}}

No one said that the program can't crash at the end.

Ungolfed version:

enum c {;
    static {
        java.awt.Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new java.awt.datatransfer.StringSelection(""), null);
    }
}
\$\endgroup\$
1
\$\begingroup\$

MFC, 54 51

OpenClipboard(0);EmptyClipboard();CloseClipboard();

Perhaps the first MFC submission ever!


If inside a method of a CWnd derived class I can use CWnd::OpenClipboard() which implementation is

_AFXWIN_INLINE BOOL CWnd::OpenClipboard()
    { ASSERT(::IsWindow(m_hWnd)); return ::OpenClipboard(m_hWnd); }

and I don't have to supply a parameter to OpenClipboard. Then it becomes:

MFC, 50

OpenClipboard();EmptyClipboard();CloseClipboard();
\$\endgroup\$
  • \$\begingroup\$ @Luis Mendo: shortened. \$\endgroup\$ – sergiol May 22 '17 at 9:28
1
\$\begingroup\$

AutoIt, 11 bytes

ClipPut("")

Pretty simple.

10 bytes

ClipPut(0)

Could save 1 byte by setting clipboard to "0"

\$\endgroup\$

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.