Search code examples
regexshellvimlowercase

How to only lowercase quoted string in VIM


Say I have a file with the following content:

Apple 'BANANA' ORANGE 'PEACH'

What is the regex to convert all quoted uppercase to lowercase?

The expected output file should look like:

Apple 'banana' ORANGE 'peach'

Solution

  • Try

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

    '\w\+' match any word that is inside quotes. and replace it with the lowercase version of the match. \= tells substitute to evaluate the expression tolower(submatch(0)) where tolower() switches the string found in submatch(0) (the whole match) to lower case.


    You can also use the \L atom to turn the string after it to lower case and \0 is the same as submatch(0)

    :%s/'\w\+'/\L\0/g
    

    Take a look at :h s/\L