How to switch text case in visual studio code
Visual Studio-CodeVisual Studio-Code Problem Overview
How does one switch the case of highlighted text in Visual Studio Code? VS allows this via CTRL+SHIFT+U and CTRL+U.
Is there a command binding that I can set up to do this, or is it by default some other key combination?
Visual Studio-Code Solutions
Solution 1 - Visual Studio-Code
Echoing justanotherdev's comment:
Mind-blowing and useful:
- Command Palette:
CTRL
+SHIFT
+p
(Mac:CMD
+SHIFT
+p
) - type
>transform
pick upper/lower case and press enter
Solution 2 - Visual Studio-Code
I've written a Visual Studio Code extension for changing case (not only upper case, many other options): https://github.com/wmaurer/vscode-change-case
To map the upper case command to a keybinding (e.g. Ctrl+T U), click File -> Preferences -> Keyboard shortcuts, and insert the following into the json config:
{
"key": "ctrl+t u",
"command": "extension.changeCase.upper",
"when": "editorTextFocus"
}
EDIT:
With the November 2016 (release notes) update of VSCode, there is built-in support for converting to upper case and lower case via the commands editor.action.transformToUppercase
and editor.action.transformToLowercase
. These don't have default keybindings.
The change-case extension is still useful for other text transformations, e.g. camelCase, PascalCase, snake-case, etc.
Solution 3 - Visual Studio-Code
Quoted from this post:
> The question is about how to make CTRL+SHIFT+U work in Visual Studio
> Code. Here is how to do it. (Version 1.8.1 or above). You can also choose a different key combination.
>
> File-> Preferences -> Keyboard Shortcuts.
>
> An editor will appear with keybindings.json
file. Place the following
> JSON in there and save.
>
> [
> {
> "key": "ctrl+shift+u",
> "command": "editor.action.transformToUppercase",
> "when": "editorTextFocus"
> },
> {
> "key": "ctrl+shift+l",
> "command": "editor.action.transformToLowercase",
> "when": "editorTextFocus"
> }
> ]
>
> Now CTRL+SHIFT+U will capitalise selected text, even if multi line. In
> the same way, CTRL+SHIFT+L will make selected text lowercase.
>
> These commands are built into VS Code, and no extensions are required
> to make them work.
Solution 4 - Visual Studio-Code
To have in Visual Studio Code what you can do in Sublime Text ( CTRL+K CTRL+U and CTRL+K CTRL+L ) you could do this:
-
Open "Keyboard Shortcuts" with click on "File -> Preferences -> Keyboard Shortcuts"
-
Click on "keybindings.json" link which appears under "Search keybindings" field
-
Between the
[]
brackets add:{ "key": "ctrl+k ctrl+u", "command": "editor.action.transformToUppercase", "when": "editorTextFocus" }, { "key": "ctrl+k ctrl+l", "command": "editor.action.transformToLowercase", "when": "editorTextFocus" }
-
Save and close "keybindings.json"
Another way:
Microsoft released "Sublime Text Keymap and Settings Importer", an extension which imports keybindings and settings from Sublime Text to VS Code.
Solution 5 - Visual Studio-Code
For those who fear to mess anything up in your vscode json settings this is pretty easy to follow.
-
Open
"File -> Preferences -> Keyboard Shortcuts"
or"Code -> Preferences -> Keyboard Shortcuts"
for Mac Users -
In the search bar type
transform
. -
By default you will not have anything under
Keybinding
. Now double-click onTransform to Lowercase
orTransform to Uppercase
. -
Press your desired combination of keys to set your keybinding. In this case if copying off of Sublime i will press
ctrl+shift+u
for uppercase orctrl+shift+l
for lowercase. -
Press
Enter
on your keyboard to save and exit. Do same for the other option. -
Enjoy
KEYBINDING
Solution 6 - Visual Studio-Code
Now an uppercase and lowercase switch can be done simultaneously in the selected strings via a regular expression replacement (regex, CtrlH + AltR), according to v1.47.3 June 2020 release:
This is done through 4 "Single character" character classes (Perl documentation), namely, for the matched group following it:
- \l <=>
[[:lower:]]
: first character becomes lowercase - \u <=>
[[:upper:]]
: first character becomes uppercase - \L <=>
[^[:lower:]]
: all characters become lowercase - \U <=>
[^[:upper:]]
: all characters become uppercase
$0
matches all selected groups, while $1
matches the 1st group, $2
the 2nd one, etc.
Hit the Match Case button at the left of the search bar (or AltC) and, borrowing some examples from an old Sublime Text answer, now this is possible:
- Capitalize words
- Find:
(\s)([a-z])
(\s
matches spaces and new lines, i.e. " venuS" => " VenuS") - Replace:
$1\u$2
- Uncapitalize words
- Find:
(\s)([A-Z])
- Replace:
$1\l$2
- Remove a single camel case (e.g. cAmelCAse => camelcAse => camelcase)
- Find:
([a-z])([A-Z])
- Replace:
$1\l$2
- Lowercase all from an uppercase letter within words (e.g. LowerCASe => Lowercase)
- Find:
(\w)([A-Z]+)
- Replace:
$1\L$2
- Alternate Replace:
\L$0
- Uppercase all from a lowercase letter within words (e.g. upperCASe => uPPERCASE)
- Find:
(\w)([A-Z]+)
- Replace:
$1\U$2
- Uppercase previous (e.g. upperCase => UPPERCase)
- Find:
(\w+)([A-Z])
- Replace:
\U$1$2
- Lowercase previous (e.g. LOWERCase => lowerCase)
- Find:
(\w+)([A-Z])
- Replace:
\L$1$2
- Uppercase the rest (e.g. upperCase => upperCASE)
- Find:
([A-Z])(\w+)
- Replace:
$1\U$2
- Lowercase the rest (e.g. lOWERCASE => lOwercase)
- Find:
([A-Z])(\w+)
- Replace:
$1\L$2
- Shift-right-uppercase (e.g. Case => cAse => caSe => casE)
- Find:
([a-z\s])([A-Z])(\w)
- Replace:
$1\l$2\u$3
- Shift-left-uppercase (e.g. CasE => CaSe => CAse => Case)
- Find:
(\w)([A-Z])([a-z\s])
- Replace:
\u$1\l$2$3
Solution 7 - Visual Studio-Code
Use the shortcut Ctrl + Shift + P to open the command palette prompt.
In the command, palette start typing the text casing you wish to transform e.g lowercase or uppercase then select the appropriate option that you are presented as shown in the figure below.
Solution 8 - Visual Studio-Code
I think this is a feature currently missing right now.
I noticed when I was making a guide for the keyboard shortcut differences between it and Sublime.
It's a new editor though, I wouldn't be surprised if they added it back in a new version.