2

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
6

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

:1,4s/^./\u&/g

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

2

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.

1

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.