Search code examples
regexvimsystem

How to pass Vim's regex captured groups to system command?


I have some data that looks like this:

6000.10c 1.2345E6

And I can write a regular expression for Vim to capture the 'important' parts:

/6000.10c\s\+\(\S\+\)

This will capture the number portion into the first group. What I would like to do is then pass this captured string to a system command that will do something with the results and return a string that is put into Vim.

How do I send regex captured group(s) to a system command?


Solution

  • Here is an example, well the awk is in fact not a must, it is just for showing the system():

    s/\v6000.10c\s+\zs(\S+)/\=system("awk '{print $0*2}' <<<". submatch(1))/
    

    Will change:

    6000.10c 1.2345E6
    

    into:

    6000.10c 2469000