7

When something is copied to the clipboard, is it available anywhere as an editable file? I'd assume it's written somewhere even if it's just a temporary file - although I am likely mistaken.

I'd like to be able to easily edit the content of the clipboard, using an editor like Vim, then have those edits saved back out to the Clipboard.

3
  • Writing the paste contents to a file in plain text would be quite a security risk where data would leave one app and go to another. As you can see in the answer, there are ways to access that, but dumping it to a file every time it changes would be a good definition of malware and spyware TBH - especially if that file was readable by non admin / non root type accounts. What you have asked for is quite logical and I've seen it implemented in several ways.
    – bmike
    Commented Jan 18, 2019 at 12:55
  • @bmike Good point about the system-wide accessibility of the temp file. Think it's fixable, but I need access to a Mac first to test the script I have in mind :-)
    – nohillside
    Commented Jan 18, 2019 at 12:57
  • Yours wasn’t so bad since the person has to trigger the snippet @nohillside - you’re correct that squirreling it away in /var/folders or other user restricted temp space instead of global temp would be pretty easy. The “when ever anything is copied” automation is where the real problem would snowball if the risk wasn’t understood or managed.
    – bmike
    Commented Jan 18, 2019 at 22:32

1 Answer 1

6

You can read and write to the Pasteboard with the Terminal commands pbcopy and pbpaste. So to edit it you could use something like

pbpaste > /tmp/clip.txt && vi /tmp/clip.txt && pbcopy < /tmp/clip.txt

If you need it often, a shell function might be better suited. You can define one by putting the following into your .bashrc

pbedit() {
    local _t=$(mktemp)
    chmod 600 "$_t"

    pbpaste > "$_t"
    ${EDITOR:-vi} "$_t"
    pbcopy < "$_t"

    rm -f "$_t"
}

This tries to minimize the risk of other people accessing the temporary file while editing (but can't prevent anybody with admin or root priviledges from access).

3
  • 1
    While the OP never stated what the content of the clipboard may be nonetheless, this answer is only relevant to text and not binary data that may be on the clipboard. Commented Jan 18, 2019 at 15:37
  • 2
    True, though binary data does not usually lend itself to be easily edited with vim. I dare say there are some who roll that way, though.
    – benwiggy
    Commented Jan 18, 2019 at 17:44
  • 1
    This is awesome. Just remember that pasteboard implementation on macOS is as a server that can contain multiple items, binary and text and ever text items can have multiple interpretations when Unicode or rich text is stored. For most instances assuming you have a single item and that it’s text holds true. developer.apple.com/documentation/appkit/…
    – bmike
    Commented Jan 18, 2019 at 22:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .