arturomp
arturomp

Reputation: 29580

How to convert a selection to lowercase or uppercase in Sublime Text

I have several strings selected in a file in Sublime Text and I want to convert them all to lowercase.

How can I convert them all to lowercase in Sublime Text?

Upvotes: 436

Views: 244714

Answers (6)

Josh
Josh

Reputation: 548

I ended up writing a plugin, took me 5 minutes and I wish I did it years ago.

Copy toggle_case_command.py to Packages/User/toggle_case_command.py, or click Tools > Developer > New Plugin..., and paste the contents of toggle_case_command.py into the new tab.

import sublime_plugin


class ToggleCaseCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        # Iterate over selected text
        for region in self.view.sel():
            if not region.empty():
                selected_text = self.view.substr(region)

                # check if the text is all uppercase or lowercase, then toggle.
                # To simplify logic, just check if everything is lower, if so then
                # uppercase, otherwise lowercase (as that's usually what I prefer).
                if selected_text.islower():
                    self.view.replace(edit, region, selected_text.upper())
                else:
                    self.view.replace(edit, region, selected_text.lower())

Key Bindings:

[
  {
    "keys": [
      "super+l"
    ],
    "command": "toggle_case"
  }
]

FYI, the default upper/lower case commands for Key Bindings are:

[
  {
    "keys": [
      "super+shift+u"
    ],
    "command": "upper_case"
  },
  {
    "keys": [
      "super+shift+l"
    ],
    "command": "lower_case"
  }
]

https://github.com/joshuataylor/toggle_case_command_plugin

Upvotes: 0

Anish Nair
Anish Nair

Reputation: 3368

For Windows:

  • Ctrl+K,Ctrl+U for UPPERCASE.
  • Ctrl+K,Ctrl+L for lowercase.

Method 1 (Two keys pressed at a time)

  1. Press Ctrl and hold.
  2. Now press K, release K while holding Ctrl. (Do not release the Ctrl key)
  3. Immediately, press U (for uppercase) OR L (for lowercase) with Ctrl still being pressed, then release all pressed keys.

Method 2 (3 keys pressed at a time)

  1. Press Ctrl and hold.
  2. Now press K.
  3. Without releasing Ctrl and K, immediately press U (for uppercase) OR L (for lowercase) and release all pressed keys.

Please note: If you press and hold Ctrl+K for more than two seconds it will start deleting text so try to be quick with it.

I use the above shortcuts, and they work on my Windows system.

Upvotes: 86

Navneet Kumar
Navneet Kumar

Reputation: 671

For Windows OS

For Uppercase CTRL + K + U

For Lowercase CTRL + K + L

Upvotes: 22

user3486945
user3486945

Reputation: 301

As a bonus for setting up a Title Case shortcut key Ctrl+kt (while holding Ctrl, press k and t), go to Preferences --> Keybindings-User

If you have a blank file open and close with the square brackets:

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

Otherwise if you already have stuff in there, just make sure if it comes after another command to prepend a comma "," and add:

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

Upvotes: 30

wadey
wadey

Reputation: 81

For others needing a key binding:

{ "keys": ["ctrl+="], "command": "upper_case" },
{ "keys": ["ctrl+-"], "command": "lower_case" }

Upvotes: 8

arturomp
arturomp

Reputation: 29580

From the Sublime Text docs for Windows/Linux:

Keypress            Command
Ctrl + K, Ctrl + U  Transform to Uppercase
Ctrl + K, Ctrl + L  Transform to Lowercase

and for Mac:

Keypress    Command
cmd + KU    Transform to Uppercase
cmd + KL    Transform to Lowercase

Also note that Ctrl + Shift + p in Windows ( + Shift + p in a Mac) brings up the Command Palette where you can search for these and other commands. It looks like this:

enter image description here

Upvotes: 860

Related Questions