I also like to use Vim to edit my system clipboard, and I wrote a couple of blogposts about how I've configured my devices to streamline this.
The basic idea is to use Vim's features to automate moving the text from the
clipboard into Vim, and back again after editing, so all you have to do is
actually edit the text.
As I'm generally not in Vim when I want to edit my clipboard, I've set up a function in my shell to achieve this (works in at least bash and zsh):
vimclippy() {
vim +'silent pu!+' +'$d _' +'1' +'set nomodified' +'au BufWriteCmd vimclippy %y+ | set nomodified' vimclippy
}
...but if you want to be able to do this from inside Vim, it's easy to set up a
command, too:
function! s:vimclippy() abort
edit vimclippy
silent put! +
$delete _
1
set nomodified
augroup vimclippy
autocmd!
autocmd BufWriteCmd vimclippy %yank + | set nomodified
augroup END
endfunction
command VimClippy call s:vimclippy()
This also allows us to easily create a version that works in the standard Windows command prompt, too*, by dropping a file named vimclippy.bat somewhere in your Path with the contents:
vim +VimClippy
All the versions work in the same way: starting from an empty buffer, they:
:put the contents of the system clipboard,
:delete the extra blank line at the end of the buffer,
- Move back to the start of the buffer,
- Set up an autocommand to replace the normal behaviour of
:w so that instead of actually writing the buffer, it instead yanks its contents back into the system clipboard.
*: I'm sure it's easy for someone that knows how to convert the original shell command into something that works in the Windows command prompt so you don't need to update your Vim configuration. I am not that person.
arglist. I'll follow your advice :-) – Vivian De Smedt Oct 27 '23 at 19:07:bufdo if empty(bufname('%'))|bw|endif– Christian Brabandt Oct 28 '23 at 08:40