In ViM, how can I transform these lines of text:

stringa1 minuscolo
stringa2 minuscolo
stringa33 minuscolo
stringa44 minuscolo

into this:

Stringa1 minuscolo
Stringa2 minuscolo
Stringa33 minuscolo

The following command will convert(It is a string substitute regex) the first character to uppercase

:%s/^./\u&/g will replace first char in all the lines


This command will replace line starting from 1 to 4 (1,4) change the line range in the command.


It's also worth noting the ~ command, which switches the case of the character under the cursor, and the gU operator which converts to uppercase across the given motion. Example:

gUl converts the current character to uppercase (or leaves it uppercase if it is already)

gUe converts to uppercase through to the end of the word

g~e switches the case of each letter through the end of the word.

Try :help case for more.


Below is an alternative approach (Linux provides you so many different ways of doing the same thing; ultimately depends on personal choice/preference.)

sed -rin 's/./\u&/1p' file.txt

Another alternative is below which capitalizes first letter of each word

sed -rin 's/([a-zA-Z])([a-zA-Z]*)/\u\1\2/g p' file.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.