Limon Monte
Limon Monte

Reputation: 54389

Sublime Text - command to make first character uppercase

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.

Upvotes: 21

Views: 16819

Answers (2)

Ste
Ste

Reputation: 2293

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

Upvotes: 6

MattDMo
MattDMo

Reputation: 102852

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.

Upvotes: 52

Related Questions