Sublime Text - command to make first character uppercase
Asked Answered
O

2

21

There are upper_case and lower_case commands:

{ "keys": ["ctrl+k", "ctrl+u"], "command": "upper_case" },
{ "keys": ["ctrl+k", "ctrl+l"], "command": "lower_case" },

I'm searching for command to capitalize the first letter of string, which can be assigned to custom shortcut.

Otes answered 16/3, 2015 at 15:16 Comment(0)
L
52

Under Edit -> Convert Case is the Title Case option. The following key binding should work:

{ "keys": ["ctrl+k", "ctrl+t"], "command": "title_case" }

Add this to your custom keymap, and it will override the default command for CtrlK,CtrlT - fold_tag_attributes. Alternatively, you can use

{ "keys": ["ctrl+k", "ctrl+i"], "command": "title_case" }

which is not assigned to anything in the default Sublime keymap.

If you're interested in other types of conversions, check out the Case Conversion plugin on Package Control. It installs commands for snake_case, camelCase, PascalCase, dot.case, and dash-case, along with a few other utilities, such as a function to separate words with slashes.

Logicize answered 16/3, 2015 at 16:21 Comment(2)
If anyone who stumbles upon this is using a OS X. You'll want to paste { "keys": ["super+k", "super+t"], "command": "title_case" } – Apriorism
...but "First character uppercase" isn't the same thing as "Title Case" and this answer only provides the later. – Rakel
I
6

The answer is for Title case but the OP asked for sentence case for what I can gather.

Here's the regex for all cases πŸ€¦β€β™‚οΈ

In sublime press Ctrl+H to bring up the replace dialog and click the regex button.

In the find box use: (^|\.\s+|…\s|\t)([a-z])

In the replace box use: \L\1\U\2


Additionally, you can use a plugin called RegReplace found here: https://packagecontrol.io/packages/regreplace so that you can add this to a menu, command or context menu.

I've added all the basic case examples here just to show how to nest the RegReplace items in a sub-menu for your context click menu.

Once installed go to: Perferences>Package Settings>RegReplace>Rules - User and paste the following.

{
  "format": "3.0",
  "replacements": {

    "case_lower":
    {
      "find": "(.+)",
      "replace": "\\L\\1",
      "greedy": true,
    },

    "case_sentence":
    {
     "find": "(^|\\.\\s+|…\\s|\\t)([a-z])",
     "replace": "\\L\\1\\C\\2",
     "greedy": true
   },

   "case_title":
   {
    "find": "\\b(\\w)(\\w+)",
    "replace": "\\C\\1\\L\\2",
    "greedy": true,
  },

  "case_upper":
  {
    "find": "(.+)",
    "replace": "\\C\\1",
    "greedy": true,
  }

}
}

Then go to the menu again and go to: Perferences>Package Settings>RegReplace>Settings and paste the following in the user file that'll appear on the right panel.

{

 "selection_only": true, // Optional but I prefer to only replace the selection.

 "extended_back_references": true // true allows the \l\1 to return the text to lowercase or others.

}

And set up this as a menu go to the file: ...\User\Context.sublime-menu and paste this:

[
  {"caption" : "-"},

  // https://packagecontrol.io/packages/regreplace
  {
    "caption": "Reg Replace",
    "children":
    [
      { "caption": "Convert Case: Lower", "command": "reg_replace", "args": {"replacements": ["case_lower"]} },
      { "caption": "Convert Case: Sentence", "command": "reg_replace", "args": {"replacements": ["case_sentence"]} },
      { "caption": "Convert Case: Title", "command": "reg_replace", "args": {"replacements": ["case_title"]} },
      { "caption": "Convert Case: Upper", "command": "reg_replace", "args": {"replacements": ["case_upper"]} }
    ]

  }

]

More RegReplace examples can be found here: Perferences>Package Settings>RegReplace>Rules - Example.

Here's my context-menu

the context menu

Inept answered 13/4, 2021 at 15:21 Comment(3)
thanks for the "extended_back_references": true information, it was driving me mad. One thing though, I had to use \L\1, using \\L\\1 did not work correctly for me. Maybe a problem in my end. – Jea
using this method changes the \1 group to lowercase in subsequent uses of the group, so it does not work as expected – Jea
ST4's "convert_ident_case" command can take a "first_case" argument with a value of "lower". Maybe this could be used. – Brochure

© 2022 - 2024 β€” McMap. All rights reserved.