Notepad++ Capitalize Every First Letter of Every Word
Background
According to Notepad++ specification (see Substitutions section), there are three operators that can be useful when turning substrings uppercase:
\u
Causes next character to output in uppercase\U
Causes next characters to be output in uppercase, until a\E
is found.\E
Puts an end to forced case mode initiated by\L
or\U
.
Thus, you can either match a substring and turn its first character uppercase with \u
to capitalize it, or match a character and use \U
/\E
.
Note that Unicode characters won't be turned uppercase, only ASCII letters are affected.
BOW (Beginning of Word) Bug in Notepad++
Note that currently (in Notepad++ v.6.8.8) the beginning of word does not work for some reason. A common solution that works with most engines (use it in Sublime Text and it will match) does not work:
\b(\w)
This regex matches all word characters irrespective of their position in the string.
I logged a bug Word boundary issue with a generic subpattern next to it #1404.
Solution #1 (for the current Notepad++ v.6.8.8)
The first solution can be using the \w+
and replace with \u$0
(no need using any capturing groups). Though this does not mean we only match the characters at the beginning of a word, the pattern will just match chunks of word characters ([a-zA-Z0-9_]
+ all Unicode letters/digits) and will turn the first character uppercase.
Solution #2 (for the current Notepad++ v.6.8.8)
The second solution can be implemented with special boundaries defined with lookbehinds:
(?:(?<=^)|(?<=\W))\w
And replace with \U$0\E
.
The regex (?:(?<=^)|(?<=\W))\w
matches an alphanumeric only at the beginning of a line ((?<=^)
) or after a non-word character ((?<=\W)
).
The replacement - \U$0\E
- contains a \U
flag that starts turning letters uppercase and \E
is a flag that tells Notepad++ to stop converting case.
Edge case
In case you have hyphenated words, like well-known
, and you only want the first part to be capitalized, you can use [\w-]+
with \u$0
replacement. It will also keep strings like -v
or --help
intact.
A simpler regex that worked for me:
Find: (\w+)
Replace: \u$0