In Vim, I know we can use ~
to capitalize a single char (as mentioned in this question), but is there a way to capitalize the first letter of each word in a selection using Vim?
For example, if I would like to change this
hello world from stack overflow
to
Hello World From Stack Overflow
how should I do it in Vim?
To use a keyboard shortcut to change between lowercase, UPPERCASE, and Capitalize Each Word, select the text and press SHIFT + F3 until the case you want is applied.
Visual select the text, then U for uppercase or u for lowercase. To swap all casing in a visual selection, press ~ (tilde). Without using a visual selection, gU<motion> will make the characters in motion uppercase, or use gu<motion> for lowercase.
To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it.
Explanation: Upper case capitalize the first letter of a sentence and leave all other letters as lowercase.
You can use the following substitution:
s/\<./\u&/g
\<
matches the start of a word.
matches the first character of a word\u
tells Vim to uppercase the following character in the substitution string (&)
&
means substitute whatever was matched on the left-hand sideg
means substitute all matches, not only the firstIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With