Notepad++ Capitalize Every First Letter of Every Word

后端 未结 5 1396
陌清茗
陌清茗 2020-12-24 00:51

Input: \"notepad++ capitalize every first letter of every word\"

Output: \"Notepad++ Capitalize Every First Letter Of Every Word\"

相关标签:
5条回答
  • 2020-12-24 01:03

    Uppercase The First Letter Of Every Word: Use the shortcut: Alt + U

    lowercase the first letter of every word: Use the shortcut: Clt + U

    Shortcut working in version 7.6.3

    0 讨论(0)
  • 2020-12-24 01:09

    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.

    0 讨论(0)
  • 2020-12-24 01:17

    I have achieved something similar by recording a macro that uses the following replacement.

    Find what:    ([a-z])+
    Replace with: \u$0\E
    
    Tick 'In selection'
    

    This is the resulting macro that I extracted from C:\Users\%USERNAME%\AppData\Roaming\Notepad++\shortcuts.xml.

    <Macro name="Title Case" Ctrl="no" Alt="no" Shift="no" Key="0">
        <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
        <Action type="3" message="1601" wParam="0" lParam="0" sParam="([A-Z])" />
        <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
        <Action type="3" message="1602" wParam="0" lParam="0" sParam="\L$0" />
        <Action type="3" message="1702" wParam="0" lParam="898" sParam="" />
        <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
        <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
        <Action type="3" message="1601" wParam="0" lParam="0" sParam="([a-z])+" />
        <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
        <Action type="3" message="1602" wParam="0" lParam="0" sParam="\u$0\E" />
        <Action type="3" message="1702" wParam="0" lParam="898" sParam="" />
        <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
    </Macro>
    

    Extra: you can add this to your right-click context menu (contextMenu.xml) using:

    <Item MenuEntryName="Macro" MenuItemName="Title Case" />
    
    0 讨论(0)
  • 2020-12-24 01:23

    There is a shortcut available in Notepad++ v7.3.2 to capitalize every first letter of every word.

    ALT + U

    Not sure about prior versions.

    0 讨论(0)
  • A simpler regex that worked for me:

    Find: (\w+)

    Replace: \u$0

    0 讨论(0)
提交回复
热议问题