1
votes

I have a number of email templates for use inside mailchimp that are currently using standard merge tags, ala *|SUBJECT|*. I want to convert these templates for Handlebars usage.

I can easily use %sno/*|/{{ and %sno/|*\}} to swap the tag delimiters, but this leaves me with all-caps names, like {{SUBJECT}}. Is there a good command for forcing lowercase inside of double curly braces in VIM so I don't have to hunt down and change every single variable?

Quicker problem description:

I have a lot of situations like this in an email template:

<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>*|SUBJECT|*</title>

I instead want this:

<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{subject}}</title>

What are the VIM commands I need to get there?

5

5 Answers

5
votes

How about doing all that in one go?

:%s/\*|\(.\{-}\)|\*/{{\L\1\E}}/g

See :help sub-replace-special.

3
votes

Try this

:%s/{{\w\+}}/\=tolower(submatch(0))/g

It takes the match of the regex, replaces it with lower case of it.

:h tolower

to know more

3
votes

To just make the {{SUBJECT}} string lower case you can use the g command like this:

:g/{{[A-Z]\+}}/normal gugu
3
votes

For a single instance, you can select the text inside the braces and force to lower case (in normal mode) with:

vi}gu

From there, you can build a macro that searches for the next occurrence, and runs the command for you. This would look something like:

qq/{{.*}}<enter>wvi}gUq
100@q

The first part creates a macro that searches for the pattern {{.*}}, moves to the next word (which will be inside the brackets), visually selects all text inside the inner brackets, and then converts it to lowercase. The second line is just an example of how to call the macro 100 times.

1
votes

Another option to do the substitution and lowercasing with one line of vim code. Use the 'g' command to operate on only those lines containing the '*|' delimiter.

:g/\*|/s/\*|/{{/|s/|\*/}}/|normal guu