Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
890 views
in Technique[技术] by (71.8m points)

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?

question from:https://stackoverflow.com/questions/65924329/search-and-replace-with-the-result-of-a-command

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Hum...

Something like that?

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

The key is :s-=


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...