3

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.

Community
  • 1
  • 1
0x89
  • 2,940
  • 2
  • 31
  • 30

2 Answers2

5

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), ...

Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
1
:let @a = system("ls -l " . shellescape(@+))

Seems to work here.

dubiousjim
  • 4,722
  • 1
  • 36
  • 34
  • that does not filter the contents of "+. Also it is missing a ). – 0x89 Feb 16 '10 at 15:20
  • You're right I dropped the final ')'. Fixed. But it was working here a moment ago. Let me investigate... Yeah, still works. Are you sure "+ has a valid value when you're testing? It wasn't overwritten because you copied and pasted from your browser, for example? (I made that mistake myself.) – dubiousjim Feb 16 '10 at 15:45
  • ah, Luc's comment made me realize what you were probably missing. I was using the same example format from the linked question. Yeah, just pipe or do whatever you want in the string you supply to system(). – dubiousjim Feb 16 '10 at 15:54
  • your answer still does not filter anything. – 0x89 Feb 16 '10 at 16:52