1

Often while editing (say, a post on this network), I find myself wanting to copy something, edit it, and paste it back.

At other times, I have some text in my clipboard, but isn't perfect. I would like a quick way to edit it and place it back in the clipboard for pasting.

What can I do?

1

So long as your Vim is built with +clipboard, you can do this from the shell without external tools. Try the command:

vim +'pu+' +'$d' +'au BufWriteCmd vimclippy %y+ | set nomodified' vimclippy

N.B. I'm using the "+ register for the clipboard. Use the "* register in the :put and :yank commands to edit the primary/selection instead.

Instead of saving a file when you write the buffer, the changes will be saved back into the clipboard (and 'modified' will be unset so you can quit without Vim complaining the changes are unsaved. Using buftype=nofile is an alternative implementation).

You can alias this so that you can run it simply by typing vimclippy. e.g. in bash:

alias vimclippy="vim +'pu+' +'\$d' +'au BufWriteCmd vimclippy %y+ | set nomodified' vimclippy"
  • Neat to see a « pure vim » solution! My use of the shell tends to fit the pbcopy | vipe | pbpaste idiom more—pipelines are just too dang cool—but +1 from me – D. Ben Knoble Oct 8 at 15:49
  • Correction: pbpaste | vipe | pbcopy – D. Ben Knoble Oct 8 at 16:36
0

Command-line tools

For the purposes of this sections, I'm going to assume you have a command line tool(s) which

  • output the clipboard on stdout (clippaste)
  • reads stdin and places it in the clipboard (clipcopy)

With these, you can use

vipe (available with moreutils)

vipe is the pipe editor (it uses your actual EDITOR, so it doesn't have to be vim).

Run at a shell:

clippaste | vipe | clipcopy

Manual vipe:

Run at a shell:

clippaste > /tmp/clip
vim /tmp/clip # in bash, you can abbreviate as 'vim !$'
clipcopy < /tmp/clip

clippaste and clipcopy

By OS:

  • macOS: pbpaste and pbcopy
  • *nix: xclip -selection c -o and xclip -selection c
  • *nix: xsel (?)

Registers

In vim, use the clipboard register:

:enew | put * | 0delete

Then

:%yank * | bdelete
  • I welcome edits regarding clipboard tools for other OS – D. Ben Knoble Oct 8 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.