107
\$\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\$
10
  • 22
    \$\begingroup\$ On a more serious note: Use something like keepass to do that for you. \$\endgroup\$
    – flawr
    Commented Feb 26, 2017 at 21:14
  • 18
    \$\begingroup\$ "Disregard the possibility that the filler text coincides with the password." Solution: ` `, 0 bytes. Overwrites the password with itself. \$\endgroup\$ Commented Feb 27, 2017 at 3:10
  • 3
    \$\begingroup\$ @Challenger5 but that's only If you choose to overwrite with some fixed or randomly chosen text \$\endgroup\$
    – Luis Mendo
    Commented Feb 27, 2017 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
    Commented Feb 27, 2017 at 11:01
  • 1
    \$\begingroup\$ @Challenger5 That wouldn't be a full Java program or function ;) \$\endgroup\$
    – user41805
    Commented Feb 28, 2017 at 10:22

55 Answers 55

1
2
4
\$\begingroup\$

PHP + PHP GTK 2.0, 24 bytes

This assumes you already have the PHP GTK extension loaded.

<?GtkClipboard::clear();

Documentation reference: http://gtk.php.net/manual/en/html/gtk/gtk.gtkclipboard.method.clear.html

\$\endgroup\$
9
  • \$\begingroup\$ You don't need the opening <? tag since you can run the code without the <? tag in the command line with the -r switch. See more info here: meta.codegolf.stackexchange.com/questions/7098/… \$\endgroup\$ Commented Feb 27, 2017 at 6:02
  • \$\begingroup\$ @KodosJohnson I intentionally left the opening tag there. \$\endgroup\$ Commented Feb 27, 2017 at 9:04
  • \$\begingroup\$ @KodosJohnson He would still have to add the bytes in the flags. \$\endgroup\$
    – ckjbgames
    Commented Mar 1, 2017 at 17:50
  • \$\begingroup\$ @ckjbgames No I wouldn't. The flag is free. I didn't change the code because I intentionally left the opening tags. \$\endgroup\$ Commented Mar 1, 2017 at 17:54
  • \$\begingroup\$ @IsmaelMiguel I have seen people add the interpreter/compiler flags to the byte count. \$\endgroup\$
    – ckjbgames
    Commented Mar 1, 2017 at 20:13
4
\$\begingroup\$

Ruby, 11 bytes (Windows)

`echo|clip`

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

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

Julia - 13 bytes

clipboard("")
\$\endgroup\$
1
  • \$\begingroup\$ Hardly needing any explanation :-) \$\endgroup\$
    – Luis Mendo
    Commented Mar 2, 2017 at 0:30
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\$

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\$
6
  • 1
    \$\begingroup\$ Can you drop ()? \$\endgroup\$
    – Qwertiy
    Commented Feb 26, 2017 at 20:40
  • 2
    \$\begingroup\$ Why? dotnetfiddle.net/nZNS1E \$\endgroup\$
    – Qwertiy
    Commented Feb 26, 2017 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
    Commented Feb 26, 2017 at 21:19
  • \$\begingroup\$ Error: comment chain does not make sense. Please insert information about deleted comment >> \$\endgroup\$ Commented Feb 27, 2017 at 4:01
  • 1
    \$\begingroup\$ For further golfing, you can drop (). :) They're equally optional in a sub or function definition. \$\endgroup\$
    – hvd
    Commented Feb 28, 2017 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\$
1
  • 1
    \$\begingroup\$ Yeah I would say it does. It is just supposed to clear it \$\endgroup\$
    – user63187
    Commented Mar 1, 2017 at 19: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\$
1
  • \$\begingroup\$ Assuming /cygdrive/c/WINDOWS/system32 is on your $PATH, cd|clip also works. \$\endgroup\$ Commented Mar 4, 2017 at 0:08
3
\$\begingroup\$

Red, 19 bytes

write-clipboard ""

writes a null string to the system clipboard

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

Rust (Windows), 128 bytes

#[link(name="user32")]extern"C"{fn OpenClipboard(h:u8);fn EmptyClipboard();}fn main(){unsafe{OpenClipboard(0);EmptyClipboard()}}

Calls some Windows API functions improperly, doesn't even close the clipboard afterwards:

#[link(name = "user32")]
extern "C" {
    fn OpenClipboard(h: u8);
    fn EmptyClipboard();
}
fn main() {
    unsafe {
        OpenClipboard(0);
        EmptyClipboard();
    }
}
\$\endgroup\$
3
\$\begingroup\$

APL (Dyalog APL) on Windows, 27 26 bytes

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

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

⎕WCWindow Create a

'Clipboard' object with the

('Text'⎕A) "ABC...Z"


Alternative of same length, but empties the clipboard and doesn't leave behind a global clipboard object:

(⎕NEW⊂'Clipboard').Text←''

⎕NEW⊂'Clipboard' create a NEW clipboard object

().Text←'' and set its Text property to the empty string

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

AutoIt, 11 bytes

ClipPut("")

Fills the clipboard with an empty string.

\$\endgroup\$
0
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
  • 1
    \$\begingroup\$ This can be golfed down to 11 bytes: `:|pbcopy` \$\endgroup\$
    – Synoli
    Commented Feb 27, 2017 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\$
1
  • 1
    \$\begingroup\$ I think you should remove the note and change title to Tcl/Tk \$\endgroup\$
    – sergiol
    Commented Jun 23, 2017 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\$
0
2
\$\begingroup\$

Julia 29 bytes

InteractiveUtils.clipboard(9)

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

Perl 5 + Win32::Clipboard, 41 bytes

$CLIP=Win32::Clipboard();
$CLIP->Empty();

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Why do you have to put it into a variable? \$\endgroup\$
    – xigoi
    Commented Jan 6, 2021 at 15:41
  • \$\begingroup\$ @xigoi I'm not actually sure why I did that. I'm not very well versed with Perl, and I don't have a windows machine to try it on right now. \$\endgroup\$
    – Razetime
    Commented Jan 6, 2021 at 16:17
2
\$\begingroup\$

Windows 10, 2 keystrokes

Win + V Delete

Based on the excel answer, the Google Chrome answer, and the Notepad++ answer.

Opens Windows Clipboard (this must be enabled in settings) and deletes the current entry.

\$\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
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\$
0
1
\$\begingroup\$

AutoIt, 11 bytes

ClipPut("")

Pretty simple.

10 bytes

ClipPut(0)

Could save 1 byte by setting clipboard to "0"

\$\endgroup\$
1
\$\begingroup\$

Factor, 38 bytes

""clipboard get set-clipboard-contents
\$\endgroup\$
1
\$\begingroup\$

HTML 50 45 bytes

<x onclick=navigator.clipboard.writeText`x`>x

Click on the rendered letter x to copy x to the clipboard.

\$\endgroup\$
1
\$\begingroup\$

AutoHotkey, 12 bytes

A_Clipboard=

AutoHotkey allows direct reading and writing to the clibpoard via the A_Clipboard global, and allows writing an empty string to a variable with just =.

\$\endgroup\$
1
\$\begingroup\$

Swift 5.10 (macOS), 50 bytes

The AppKit framework (and, by extension, NSPasteboard) are macOS-only.

import AppKit
NSPasteboard.general.clearContents()

Yeah. Turns out NSPasteboard has a method dedicated for this purpose.

This uses less bytes than setString(_:forType:), even with closure tricks, since you have to set the data type beforehand. That's AppKit for you.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.