Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 786
Limon Monte Avatar asked Mar 16 '15 15:03

Limon Monte


People also ask

How do you capitalize in Sublime Text?

Sublime text allows you to quickly convert selected text to upper case or lower case. To convert to upper case: In quick succession, hit Cmd–K then Cmd–U (Mac) or Ctrl–K then Ctrl–U (Windows). To convert to lower case: In quick succession, hit Cmd–K then Cmd–L (Mac) or Ctrl–K then Ctrl–L (Windows).

How do I run commands in Sublime Text?

To open a command palette in Sublime Text editor, you can use the shortcut key combination Ctrl+Shift+P on Windows and Cmd+Shift+P on OSX.


2 Answers

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.

like image 175
MattDMo Avatar answered Sep 20 '22 09:09

MattDMo


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

like image 26
Ste Avatar answered Sep 17 '22 09:09

Ste