# I copied my password to the clipboard! Can you delete it?

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.

• 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.

• What if the clipboard has an undetermined number of entries? – LarsW Feb 26 '17 at 19:27
• On a more serious note: Use something like keepass to do that for you. – flawr Feb 26 '17 at 21:14
• "Disregard the possibility that the filler text coincides with the password." Solution:  , 0 bytes. Overwrites the password with itself. – Esolanging Fruit Feb 27 '17 at 3:10
• @Challenger5 but that's only If you choose to overwrite with some fixed or randomly chosen text – Luis Mendo Feb 27 '17 at 10:08
• 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... – Nat Feb 27 '17 at 11:01

# Ruby, 11 bytes (Windows)

echo|clip


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

# 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


# 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))


# 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' '') ""

# 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)


## VB.NET, 29 bytes

Sub F
Clipboard.Clear
End Sub


Saved 2 bytes thanks to @hvd

• Can you drop ()? – Qwertiy Feb 26 '17 at 20:40
• – Qwertiy Feb 26 '17 at 21:11
• 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. – Qwertiy Feb 26 '17 at 21:19
• Error: comment chain does not make sense. Please insert information about deleted comment >> – CalculatorFeline Feb 27 '17 at 4:01
• For further golfing, you can drop (). :) They're equally optional in a sub or function definition. – hvd Feb 28 '17 at 18:35

# 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.

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

Julia - 13 bytes

clipboard("")

• Hardly needing any explanation :-) – Luis Mendo Mar 2 '17 at 0:30

## 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.

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

# AutoIt, 11 bytes

ClipPut("")


Fills the clipboard with an empty string.

# Ruby 25 bytes (MacOS)

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


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

• This can be golfed down to 11 bytes: :|pbcopy – Synoli Feb 27 '17 at 21:19

# Tcl, 15 bytes.

Not the shortest but the most readable:

clipboard clear


note: must be executed with wish instead of tclsh

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

# 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();
}
}


# 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);
}
}


# 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();

• @Luis Mendo: shortened. – sergiol May 22 '17 at 9:28

## AutoIt, 11 bytes

ClipPut("")


Pretty simple.

## 10 bytes

ClipPut(0)


Could save 1 byte by setting clipboard to "0"