ChatGPT解决这个技术问题 Extra ChatGPT

How to switch text case in visual studio code

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?

I can't find any relevant shortcuts in key bindings. Probably this function is not supported in the current version.
Update: it is supported now. Go to File > Preferences > Keyboard Shortcuts and search for "Transform".

P
P i

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

https://i.stack.imgur.com/QHwJg.png


It's possible to configure keybindings for this commands on File > Preferences > Keyboard Shortcuts
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.
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.
very useful particularly with multiple selections
How to combine transform with "rename" meaning it automatically updates the same variables (F2).
s
simhumileco

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.


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.
For lazy people: select the text, ctrl + p, type >transform pick lower or upper case then press enter
An addendum to @justanotherdev's comment: it's possible to configure keybindings for this commands on File > Preferences > Keyboard Shortcuts
Isn't there a way to have these options show up in the right click menu (the way Nptepad++ does) ?
@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'
M
Marianna S.

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.


Cool! This is the answer the question is asking for!
I went for cmd+shift+u and cmd+shift+l on macOS. Feels just right.
For Linux ctrl+shift+u is not working, so instead use key ctrl+u for uppercase & ctrl+l for lowercase
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?
A
Alex Baban

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:

"Sublime Text Keymap and Settings Importer"

https://marketplace.visualstudio.com/items?itemName=ms-vscode.sublime-keybindings


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.
ctrl+k ctrl+u is usually reserved for uncomment... i've set mine to alt+k alt+u and alt+k alt+l.
G
GM456742

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 on Transform to Lowercase or Transform 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 or ctrl+shift+l for lowercase. Press Enter on your keyboard to save and exit. Do same for the other option. Enjoy KEYBINDING


C
CPHPython

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:

https://i.stack.imgur.com/XTgqv.gif

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


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.
M
Mwiza

Use the shortcut Ctrl + Shift + P to open the command palette prompt.

https://i.stack.imgur.com/op2XC.png


T
Tobiah Zarlez

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