Convert to lowercase in Vim Visual mode until specific delimiter

Viewed 44

Basically i have a yml file and i want to convert the keys to lowercase.

example below.

KEY: value
ANOTHER_KEY: another_value

I tried the vwu and it works as expected but i want to apply in the whole file.

I also used the . (dot) to apply the above command using number & j multiple times.

2 Answers

Another possible approach:

:%norm! guiw

 %  ................. the whole file
 norm! .............. in normal mode please
 gu ................. make {motion} text lowercase 
 iw ................. inner word

It also could be:

:%norm! gut:

t: .............. until :

Okay, one approach is to use the sub-replace-expression functionality. The following command:

:%s/\(\s*\)\(\w*\)\(.*\)/\= submatch(1) . tolower(submatch(2)) . submatch(3)/

Will, for each line, match whitespace word whatever, and replace it with the same thing but word being lowercase. I'm assuming that you are familiar with the restrictions and usage of regular expressions so I won't go into all the details of why I did the expression that way, how to modify it, or restrictions with using them against a context-free language (I think?) like yaml here, but the \= token in the :s command can be pretty nifty.

Related