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.


    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.

    What if the clipboard has an undetermined number of entries?

    @LarsW Can you please ellaborate or give an example?

    Well, on my computer I can access the last ~10 I copied/selected, but I can imagine that number being different on different systems. I don't know if this matters at all, so that's why I asked.

    @LarsW Oh, I see. You can assume the most recent entry. Edited into the text

    On a more serious note: Use something like keepass to do that for you.

    "Disregard the possibility that the filler text coincides with the password." Solution: ` `, 0 bytes. Overwrites the password with itself.

    @Challenger5 but that's only _If you choose to overwrite with some fixed or randomly chosen text_

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

    @Challenger5 That wouldn't be a full Java program or function ;)

    Or you could...you know...copy something else?! Erase the last thing copied??!! Ha. Ha. Ha.

  • Adám

    Adám Correct answer

    4 years ago

    Excel, 1 keystroke


    Works because Excel, uniquely, always has a selection. Probably works in other spreadsheet applications too. As opposed to the other joking entries, Excel answers are actually somewhat frequent on PPCG.

    Isn't this 2 keystrokes? "A keystroke is a single press of a key on a keyboard. Each key press is a keystroke." http://www.computerhope.com/jargon/k/keystroke.htm

    @Martijn Possibly. I just copied from here.

    I'm not sure it's really valid to score by keystrokes. Also, since you say _As opposed to the other joking entries_, I interpret that as you admitting this is not an actually valid answer. So I won't accept it even if it's the shortest one. If you can point me to some relevant meta-post establishing the validity of keystrokes as scoring method, I will happily accept this submission

    @LuisMendo Your in-between-lines analysis is correct, but now that I actually took the time ro research on Meta: https://codegolf.meta.stackexchange.com/a/4817/43319 would seem to indicate that it is acceptable, as opposed to "Windows" which isn't an editor.

    @Adám My understanding it that that meta-post refers to a hypothetical "editor golf" _challenge category_, not to using the number of keystrokes as a scoring method for "code golf" challenges. I've pinged Doorknob and Dennis to know their opinion

    I'm still not totally convinced that keystrokes are a valid scoring method and are comparable to bytes. But I haven't had any conclusive feedback to settle this, so I'm assuming it is valid and therefore accepting this answer

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM

Tags used