1

I am creating a yml file out of some variables. I have like 80 lines and they all have the pattern:

my-first-command-line-var: {{ my-first-command-line-var }},
my-second-cli-var: {{ {{ my-second-cli-var }} ,
my-third-var: {{ my-third-var }}

and so on

That's because all cli variables are with - but yaml can't handle _.

So I need to replace all - inside {{ }} with a _, so it becomes:

my-first-command-line-var: {{ my_first_command_line_var }},
my-second-cli-var: {{ {{ my_second_cli_var }} ,
my-third-var: {{ my_third_var }}

I have tried :%s/{{\zs\-*ze}}/_/g and some variations like :%s/{{\zs\(-\)*ze}}/_/g but nothing like this worked.

5

You can use a :s command with an expression on the replacement side (see :help sub-replace-expression), then you can use the substitute() function to perform the replacement of - for _, and the submatch() function to refer to the match.

For your specific case, the following command will work:

:%s/{{.*}}/\=substitute(submatch(0), "-", "_", "g")

This will match the contents in between {{ and }} on every line. And it will replace it with the result of the function call, which takes the matched contents (as submatch(0)) and replaces all -s with _s.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.