Is it possible to access the Clipboard as a file from the command line?

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.


Solution 1:

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