Search code examples
vim

Search and replace with the result of a command


In vim, I want to search and replace a text. But I want to replace it with the output of a command.

Given the text:

{ "_template": "foo" }
{ "_template": "foo" }

I want to search and replace that to become:

{ "_id": "239c55fd-538e-485f-8588-83d9735b6819" }
{ "_id": "2ae9f49f-244c-47b0-8f0f-c6c46e860af3" }

The latter is the result of the unix/linux command uuidgen.

It would look something like s:/"_template": "foo"/"_id": "<uuidgen>"/, but I'm unsure what to do at <uuidgen>. The command uuidgen is just an example, but it would be a command that takes no arguments and does not need any stdin passed in (like for example wc or so would).

It needs to call the command for each search/replace again.

Is this possible at all with vim? Or should I better use sed and/or awk instead?


Solution

  • Hum...

    Something like that?

    :s/"_template": "foo"/\='"_id": "'.trim(system('uuidgen')).'"'
    

    The key is :h :s\=