How can I filter the content of a register in vim?

vim

Solution

system() is the way to go. `:h system()`.

You can use the old fashion way (the one that gives you a full control as you would be able to pipe and redirect as many times as it pleases you):

:let res = system("echo ".shellescape(@+)." | the-filter-command")
:put=res

However, you may have issues with line-endings (the last character needs to be chomped). Hence this second solution where vim uses a temporary file and pass it to the filter program:

:let res = system(the-filter-command, @+)
:put=res

There is also another way to accomplish this if you play with another buffer:

:new
:put=@+
:%!the-filter-command
:%d +
:bd
:put=@+

Last note: Vim already has a few filters of its own like `:sort`, `uniq` is also possible natively (but a little bit more complex), ...

Problem

I want to filter the content of a register (in my case, the clipboard register `"+`) through an external command before pasting it into the buffer. There should be a solution along the lines of VIM: store output of external command into a register, but I just don't seem to be able to figure it out.

Original source

Related problems