How to use magic regexp to replace in vim
How to use Vim magic regex to transform key-value pairs by lowercasing the key and wrapping it in a variable substitution syntax.
I have an input string and I would like to take the first column and transform it so that the value behind the = sign becomes the same as the key but lowercased and encapsulated in ${}.
The Vim substitution command below uses \v (very magic mode) to capture the key name with (\S+), then uses \L to lowercase the captured group and \E to end the case conversion. This is applied to a visual selection with '<,'>.
1
2
3
4
5
6
7
8
9
vim
...
CLIENT_ID = "helloo-id"
TENANT_ID = "helloo-tenant"
CLIENT_SECRET = "helloo-client"
...
:'<,'>s/\v(\S+)\s\=\s(\S+)/\1 = "${\L\1\E}"/
Result:
1
2
3
CLIENT_ID = "${client_id}"
TENANT_ID = "${tenant_id}"
CLIENT_SECRET = "${client_secret}"
This post is licensed under CC BY 4.0 by the author.
