Search code examples
macosvimcommand-linealfred

How to use VIM(buffer) for quick editing of clipboard content


Being spoiled by the awesome VIM mode on ZSH I wanted to recreate the same experience for my clipboard. VIM mode in ZSH allows the current command to be edited in a VIM buffer an to be written back to the command-line, example:

Example of VIM mode on ZSH

I want to recreate the same experience for my clipboard (on macOS). I got it working using the following mini script:

#!/bin/bash

tmpfile=/tmp/$(openssl rand -base64 8)

touch $tmpfile
pbpaste > $tmpfile
vim $tmpfile
pbcopy < $tmpfile
rm $tmpfile

I have a feeling this could be a lot easier. What I want to accomplish is: 1. Open VIM (command-line) with the current content of the system clipboard 2. Edit the content in VIM 3. On write out, copy the content back to the system clipboard

The end-goal is to get this in a Alfred workflow that allows me to quickly edit clipboard content on the fly.


Solution

  • This requires the vipe tool (which stands for vi pipe, I think): I have a bash function called pbed that does exactly what you ask:

    pbed () {
      pbpaste | vipe | pbcopy
    }
    

    You could easily turn that into a script. You can get vipe from brew in the moreutils package.