GeistInTheBASH
GeistInTheBASH

Reputation: 107

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.

Upvotes: 0

Views: 468

Answers (2)

SergioAraujo
SergioAraujo

Reputation: 11830

I have this on my zshrc:

# Edit content of clipboard on vim (scratch buffer)
function _edit_clipboard(){
    # pbpaste | vim -c 'setlocal bt=nofile bh=wipe nobl noswapfile nu'
    pbpaste | vim
}
zle -N edit-clipboard _edit_clipboard
bindkey '^x^v' edit-clipboard

Upvotes: 0

D. Ben Knoble
D. Ben Knoble

Reputation: 4703

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.

Upvotes: 2

Related Questions