316

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?

Dário
  • 2,002
  • 1
  • 18
  • 28
John
  • 3,451
  • 2
  • 16
  • 7

8 Answers8

457

Echoing justanotherdev's comment:

Mind-blowing and useful:

  1. Command Palette: CTRL + SHIFT + p (Mac: CMD + SHIFT + p)
  2. type >transform pick upper/lower case and press enter

enter image description here

P i
  • 29,020
  • 36
  • 159
  • 267
SimplyInk
  • 5,832
  • 1
  • 18
  • 27
  • 35
    It's possible to configure keybindings for this commands on File > Preferences > Keyboard Shortcuts – Dinei May 08 '17 at 15:59
  • 4
    pycharm uses the same command CTRL+SHIFT+ "u" to toggle. why create 2 shortcuts when one could be used? i realize this is not an answer, just a comment. – davidj411 Aug 24 '18 at 16:46
  • 1
    sometimes you have "Mixed casing Text" that you want to go directly to "ALL UPPERCASE" or "all lowercase", so transforming to uppercase and transforming to lowercase separately make more sense. – drigoangelo May 02 '20 at 04:47
  • very useful particularly with multiple selections – Max MacLeod May 17 '21 at 14:16
  • 1
    How to combine transform with "rename" meaning it automatically updates the same variables (F2). – Melroy van den Berg May 25 '22 at 19:39
90

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.

Marianna S.
  • 905
  • 6
  • 7
  • Cool! This is the answer the question is asking for! – Nowdeen Nov 19 '17 at 15:53
  • I went for `cmd+shift+u` and `cmd+shift+l` on macOS. Feels just right. – theory Mar 27 '18 at 15:03
  • For Linux ctrl+shift+u is not working, so instead use key ctrl+u for uppercase & ctrl+l for lowercase – Jitendra Pawar May 12 '18 at 05:33
  • 1
    Is it possible to have one keyboard shortcut cycle through each option instead of having separate keyboard shortcuts for each one? That is, can I set it up so that hitting `shift+f3` changes it ToUppercase, hitting `shift+f3` changes it ToLowercase, hitting it again changes it ToSnakecase, and hitting it again changes it ToTitlecase? – coip Jan 04 '22 at 21:16
  • 3
    To find this hidden JSON editor with VS Code 1.72.2 (Sept. 2022) or later, click on the blank page icon with the curved arrow, which points to that blank page. It's in the upper right hand corner after using File-> Preferences -> Keyboard Shortcuts. Look for the 3 dots icon. It's the 1st icon on the left, before the split pane icon. The alt text says "Open Keyboard Shortcuts (JSON)". That will open the keybindings.json file. Saving that file will automatically enable the changes, without relaunching VS Code. – Clomp Oct 15 '22 at 20:16
  • Thank you my good sir, this makes my life so much easier – Adriaan Mouton Jan 25 '23 at 11:02
  • Re: "An editor will appear with `keybindings.json file`": no, it won't. Version 1.75.1. – pmor Mar 02 '23 at 13:49
  • @Clomp I cannot find "Open Keyboard Shortcuts (JSON)". Help. UPD: found. – pmor Mar 02 '23 at 13:53
74

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.

simhumileco
  • 31,877
  • 16
  • 137
  • 115
Wayne Maurer
  • 12,333
  • 4
  • 33
  • 43
  • 1
    the "ctrl+t u" didn't work for me on the mac version. I used "ctrl+u" instead as it's not mapped by default to any action. – Shahar Jan 05 '17 at 08:23
  • 17
    For lazy people: select the text, ctrl + p, type `>transform` pick lower or upper case then press enter – justanotherdev Mar 23 '17 at 05:49
  • An addendum to @justanotherdev's comment: it's possible to configure keybindings for this commands on File > Preferences > Keyboard Shortcuts – Dinei May 08 '17 at 15:59
  • Isn't there a way to have these options show up in the right click menu (the way Nptepad++ does) ? – smooth_smoothie Nov 28 '18 at 15:07
  • @justanotherdev to be honest, that's a faster process for me though, plus it is already baked in and no need for 'Yet Another Extension' – KhoPhi Sep 01 '20 at 12:58
17

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. - https://marketplace.visualstudio.com/items?itemName=ms-vscode.sublime-keybindings
Alex Baban
  • 11,312
  • 4
  • 30
  • 44
  • 1
    I don't know if it's new, but when I did this all I had to do was put "uppercase" in the search box that appeared, then click a '+' to add a new mapping. Repeat with "lowercase" to cover the other direction and I was all set. – torpy Nov 02 '18 at 19:54
  • ctrl+k ctrl+u is usually reserved for uncomment... i've set mine to alt+k alt+u and alt+k alt+l. – sam yi Feb 05 '21 at 15:55
15

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. enter image description here

Mwiza
  • 7,780
  • 3
  • 46
  • 42
14

For those who fear to mess anything up in your vscode json settings this is pretty easy to follow.

  1. Open "File -> Preferences -> Keyboard Shortcuts" or "Code -> Preferences -> Keyboard Shortcuts" for Mac Users

  2. In the search bar type transform.

  3. By default you will not have anything under Keybinding. Now double-click on Transform to Lowercase or Transform to Uppercase.

  4. 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 or ctrl+shift+l for lowercase.

  5. Press Enter on your keyboard to save and exit. Do same for the other option.

  6. Enjoy KEYBINDING

GM456742
  • 141
  • 1
  • 4
12

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:

Replacing different text cases in one selection

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:

  1. Capitalize words
  • Find: (\s)([a-z]) (\s matches spaces and new lines, i.e. " venuS" => " VenuS")
  • Replace: $1\u$2
  1. Uncapitalize words
  • Find: (\s)([A-Z])
  • Replace: $1\l$2
  1. Remove a single camel case (e.g. cAmelCAse => camelcAse => camelcase)
  • Find: ([a-z])([A-Z])
  • Replace: $1\l$2
  1. Lowercase all from an uppercase letter within words (e.g. LowerCASe => Lowercase)
  • Find: (\w)([A-Z]+)
  • Replace: $1\L$2
  • Alternate Replace: \L$0
  1. Uppercase all from a lowercase letter within words (e.g. upperCASe => uPPERCASE)
  • Find: (\w)([A-Z]+)
  • Replace: $1\U$2
  1. Uppercase previous (e.g. upperCase => UPPERCase)
  • Find: (\w+)([A-Z])
  • Replace: \U$1$2
  1. Lowercase previous (e.g. LOWERCase => lowerCase)
  • Find: (\w+)([A-Z])
  • Replace: \L$1$2
  1. Uppercase the rest (e.g. upperCase => upperCASE)
  • Find: ([A-Z])(\w+)
  • Replace: $1\U$2
  1. Lowercase the rest (e.g. lOWERCASE => lOwercase)
  • Find: ([A-Z])(\w+)
  • Replace: $1\L$2
  1. Shift-right-uppercase (e.g. Case => cAse => caSe => casE)
  • Find: ([a-z\s])([A-Z])(\w)
  • Replace: $1\l$2\u$3
  1. Shift-left-uppercase (e.g. CasE => CaSe => CAse => Case)
  • Find: (\w)([A-Z])([a-z\s])
  • Replace: \u$1\l$2$3
CPHPython
  • 12,379
  • 5
  • 59
  • 71
  • 2
    What a wonderful thing! This answer is the best! Reminds me of DreamWeaver 8 where I would revamp entire sites with regex - especially bloated MS stuff. Anyway, this was super useful in scripting DB2-to-(lowercase)Postgres SQL change-over. – Neil Gaetano Lindberg Nov 16 '20 at 14:22
1

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.

Source: https://code.visualstudio.com/Docs/customization

Tobiah Zarlez
  • 1,680
  • 1
  • 12
  • 13