Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalize first letter of each word in a selection using Vim

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?

like image 794
keelar Avatar asked Jul 03 '13 05:07

keelar


People also ask

How do you auto capitalize the first letter of every word?

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.

How do you capitalize letters in Vim?

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.

Which method is used to capitalize first letter of each word in a string?

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.

What capitalizes the first letter of the selected sentence?

Explanation: Upper case capitalize the first letter of a sentence and leave all other letters as lowercase.


1 Answers

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 side
  • g means substitute all matches, not only the first
like image 84
Rohit Jain Avatar answered Sep 25 '22 09:09

Rohit Jain