0

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.

  • 1
    I'm not familiar with some of the commands on macOS, but on Linux you can access the clipboard contents with `xclip`. So you can do something like `$ xclip -o | vim` which sends the clipboard contents to vim, and then from within vim run `: w !xclip -i` which sends the contents of the buffer to the clipboard. – m.k. May 14 '20 at 13:20

2 Answers2

1

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.

D. Ben Knoble
  • 4,273
  • 1
  • 20
  • 38
0

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
SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
  • Don’t you need `vim -`? And this doesn’t set the clipboard contents after the edits. – D. Ben Knoble May 15 '20 at 12:31
  • Sorry, my vim is aliased to neovim and it does not require - Actually I have added some comments on my code, so other people don't have any problems – SergioAraujo May 15 '20 at 21:17