Search code examples
vimuppercasecapitalize

How to capitalize specific uppercase words in vim


We have a 5000-line text file containing words like so:

BANKS
BEING AFRAID OF DOGS
This is a SENTENCE.
Just another sentence.
COUNTRY

Using vim, I want to capitalize the words only in the lines where all the words are in uppercase (meaning lines 3 and 4 should be left untouched). In other words, what I expect to get is:

Banks
Being Afraid Of Dogs
This is a SENTENCE.
Just another sentence.
Country

Solution

  • By referring to Power of g and Switching_case_of_characters.

    1. Applying the command to line containing upper case character and space only, which is g/^[A-Z ]*$/
    2. Then do Title case conversion s/\<\(\w\)\(\w*\)\>/\u\1\L\2/g

    The whole command will be :g/^[A-Z ]*$/s/\<\(\w\)\(\w*\)\>/\u\1\L\2/g