What is the shortcut for CTRL + CLICK in Visual Code? - visual-studio-code

What is the shortcut key to Ctrl+Click (Follow Link) in Visual Code? If it doesn't have one, is there any way to add it?
In Eclipse, if I press F3 I can open the file, like Ctrl+Click

In HTML files the command is "Open Link" (editor.action.openLink). This command does not have, by default, associated a key combination. You can assign one by using the Preferences -> Keyboard Shortcuts command from the menu (click on the gear on the bottom of the primary side bar).
In other contexts (source code in various programming languages), the combination Ctrl+click (or Cmd+click on macOS) when the cursor is on an identifier (constant name, variable name, function name, class name, etc.) is equivalent with command "Go to Definition" (editor.action.revealDefinition). This command has, by default, associated the key F12.

Do Command/Control P > keybindings.json and insert:
[
...,
{
"key": "ctrl+f12",
"command": "editor.action.openLink"
}
]
to set Ctrl F12 to open the link under the cursor.

Related

How to add multiple keyboard shortcuts to one command in VS Code?

How can I add multiple keyboard shortcuts to a single command in VS Code?
As an example (from the comments), ctrl + 0 & ctrl + 1 should both do the same command/action. So completely separate shortcuts doing the same command.
Let's say there are 2 bindings to close window like so:
Let's say we want to add one more keybinding like CMD+K, CMD+1. You could do that by right-clicking a command and choosing copy like so:
Then, click on an icon on the top right corner to open keyboard shortcuts JSON. The icon has an curved arrow on a page:
Your user-defined keybindings.JSON will show up. Type this in it:
// Place your key bindings in this file to override the defaults
[
{
"key": "cmd+k cmd+1",
"command": "workbench.action.closeWindow"
}
]
Save and close.
Now you will see 3 keybindings for the action like so:
Now, try your new keybinding.

why isn't the file save shortcut(ctrl + s) not working in visual studio code?

It doesn't have the shortcut key for save option enabled.
How can I enable the shortcut key for save option in microsoft visual code.
Because ctrl + s isn't working on my desktop.
Like the other answers specify, you'll need to set the key binding for File: Save to <ctrl|cmd>+s, but in my case I had to set it to some other random key binding first.
I think I used alt+shift+9, and then ctrl+s
Open your keyboard shortcuts menu by hitting CTRL + K CTRL + S (hold down CTRL and then press K and S), then search for file:save and you can double-click in the Keybinding column next to File:Save to enter whatever key combination you want:
Then you should see it in the file menu and it should work:
Just like the first comment on your question says, Ctrl+S seems to be assigned to 'Save All' instead. if you don't want it that way, try changing it in 'Preferences >', which is 4 blocks under the 'Save' option in the image shown in your question, and set 'Save All' to Ctrl+Shift+S and 'Save' to Ctrl+S.

Refactor local variable name using Visual Studio Code

I have this simple situation. I'd like to refactor the name of the role variable:
It looks like Visual Studio Code is smart enough to know that "roles" in the URL should not be touched.
I just want to refactor the name of the variable in a single file, in a single scope, not the whole file and definitely not multiple files!
If I use Ctrl + H, it will bring me to a menu which by default refactors the name in multiple files or a whole single file, but I just want to refactor the name in a single function scope!
Use rename symbol instead of the standard find/replace. Rename is bound to F2 by default.
Rename symbol will know to only touch the local roles references in your example. Visual Studio Code ships with rename support for JavaScript and TypeScript. Other languages may require installing a language extension.
Use Rename Symbol. It is defined by default with Ctrl + F2.
However, keep in mind that the variable, definition, function, etc., that you're changing, will be changed in the file itself, not only the scope. Visual Studio Code for now doesn't have an implementation for renaming a variable in a scope (for example, a variable inside a function). So keep that in mind.
Visual Studio Code - change all occurrences
For macOS users: Use Fn + ⌘ + F2 to rename a variable inside a code block.
To open your user and workspace settings, use the following Visual Studio Code menu command:
On Windows/Linux: Menu File → Preferences → Settings. On macOS: Code → Preferences → Settings.
You can also open the Settings editor from the Command Palette (Ctrl + Shift + P) with Preferences, open Settings or use the keyboard shortcut (Ctrl + ,).
In the search bar, enter keybindings.json, and add this below code:
{
"command": "editor.action.changeAll",
"key": "ctrl+f2",
"when": "editorTextFocus && !editorReadonly"
}
and
{
"command": "editor.action.rename",
"key": "f2",
"when": "editorHasRenameProvider && editorTextFocus && !editorReadonly"
}
In the keybindings.
F2 appears to work across all files, and Ctrl + F2 in the current file only.

Shortcut for joining two lines

What's the keyboard shortcut for joining two lines in VS Code?
If the cursor is anywhere in the first line of
f(foo,
bar)
then when I hit the shortcut, I want to get
f(foo, bar)
Visual Studio Code now comes with a default key binding for joining lines, Ctrl + J.
You can find all keyboard shortcuts under Preferences > Open Keyboard Shortcuts.
You can overwrite it using the UI or by providing a different key combination for the command editor.action.joinLines in keybindings.json.
Press F1, and type Join Lines. By default there is no key bindings to it yet, but you can easily set it under 'Preferences > Keyboard Shortcuts', then search for Join Lines and set a shortcut.
You can simply:
Select the lines to be joined.
Hit Ctrl+Shift+P or F1.
Type join lines.
Since the best way is already answered I'm just adding an alternative.
If you want to work with defaults you can hit Ctrl+Del while caret is at the end of the first line.
Works with multi-select too if you want to join multiple lines.
Depending on how much clutter you have in your , try the following "keypress sequence" (you must have focus in an open editor tab for this to work1, and make sure to have your cursor/lines selected before doing this):
Ctrl+Shift+P JL 2
If your Command Palette ends up showing a clash of non-Join Lines entries when you finish typing, you may have to end up typing instead3:
Ctrl+Shift+P JOINSpace L
Ctrl+Shift+P JOINSpace LI
...
Ctrl+Shift+P JOINSpace LINES
...Manually select from the Palette using down arrow or mouse 4
In case you're thinking about setting your own keybind (since it is unset by default in Windows), here are the other Commands that have a keybind associated with them containing a J 5:
Command
Keybinding
When
workbench.action.search.toggleQueryDetails
Ctrl+Shift+J
inSearchEditor || searchViewletFocus
View: Toggle Panel
Ctrl+J
---
Unfold All
Ctrl+K Ctrl+J
editorTextFocus && foldingEnabled
Notebook: Join With Previous Cell
Shift+Alt+Win+J
notebookEditorFocused
Notebook: Join With Next Cell
Alt+Win+J
editorTextFocus && foldingEnabled
I suggest using Ctrl+Alt+J or Ctrl+Shift+Alt+Jif you end up going this route, since it doesn't seem to clash with existing defaults and is similar to what people are already used to.
Alternatively, if you tend to use a different text editor or IDE,
[File > Preferences > Keymaps] (Ctrl+K Ctrl+M) offers a selection of alternative keymaps (these are extensions, which must be installed), including (as of now, sorted by current rating):
IntelliJ IDEA (by Keisuke Kato)
Sublime Text (by Microsoft)
Atom (by Microsoft)
Eclipse (by Alphabot Security)
Visual Studio (by Microsoft)
Delphi (by Alessandro Fragnani)
Notepad++ (by Microsoft)
Vim (by vscodevim)
Emacs (by hirosun)
1 In other words, don't be in a "non-editor" window like Settings or Keyboard Shortcuts
2 Alternatively, Command Palette can also be opened by selecting [View > Command Palette...] instead of Ctrl+Shift+P
3 This could occur due to having 3rd-party Commands containing the letters j and l. Command Palette can also be found alternatively by selecting [View > Command Palette...]
4 Hopefully you don't end up with this case.
5 These are all listed under [File > Preferences > Keyboard Shortcuts] (Ctrl+K Ctrl+S)

Make selected block of text uppercase

Can I make a multi-line selection of text all capitals in Visual Studio Code?
In full Visual Studio it's CTRL+SHIFT+U to do this.
The extension that exists that I have seen only do non-multi-line blocks.
NOTE: THE UI OF VISUAL STUDIO CODE WHEN THIS QUESTION WAS ASKED (5 OR MORE YEARS AGO) HAS CHANGED.
The question is about how to make CTRL+SHIFT+U work in Visual Studio Code. Here is how to do it in version 1.57.1 or above.
Steps:
Open Visual Studio Code.
Press CTRL+SHIFT+P.
Type
open keyboard shortcuts
Select
Open keyboard shortcuts (json)
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.
Update August 2021
There is a UI to see and update keyboard shortcuts:
File-> Preferences -> Keyboard Shortcuts.
Find "Transform to Uppercase":
Click the + icon.
In the popup, press the desired key combination and hit enter:
Do the same for lower case.
Note
In the new versions (eg 1.57.x) of VS Code, Ctrl+Shift+L is a shortcut for bulk selecting all selected text occurrences. So you can use another combination, like Ctrl+Shift+/ etc.
Whenever you want to do something in VS Code and don't know how, it's a good idea to bring up the command palette with CTRL+SHIFT+P (CMD+SHIFT+P on mac), and try typing in a keyword for you want. Oftentimes the command will show up there so you don't have to go searching the net for how to do something.
Highlight the text you want to uppercase. Then hit CTRL+SHIFT+P to bring up the command palette. Then start typing the word "uppercase", and you'll see the Transform to Uppercase command. Click that and it will make your text uppercase.
Creator of the change-case extension here. I've updated the extension to support spanning lines.
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 ctrl+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. They also work with multi-line blocks.
The change-case extension is still useful for other text transformations, e.g. camelCase, PascalCase, snake_case, kebab-case, etc.
Update on March 8, 2018 with Visual Studio Code 1.20.1 (mac)
It has been simplified quite a lot lately.
Very easy and straight forward now.
From "Code" -> "Preferences" -> "Keyboard shortcuts"
From the search box just search for "editor.action.transformTo",
You will see the screen like:
Click the "plus" sign at the left of each item,
it will prompt dialog for your to [press] you desired key-bindings,
after it showing that on the screen, just hit [Enter] to save.
In Linux and Mac there are not default shortcuts, so try to set your custom shortcut and be careful about don't choose a hotkey used (For example,
CTRL+U is taken for uncomment)
File-> Preferences -> Keyboard Shortcuts.
Type 'transfrom' in the search input to find transform shortcuts.
Edit your key combination.
In my case I have CTRL+U CTRL+U for transform to uppercase and CTRL+L CTRL+L for transform to lowercase
Just in case, for Mac instead of CTRL I used ⌘
Change letter case in Visual Studio Code
Updated answer
Show All Commands: Ctrl+Shift+P
and start typing "upper" or "lower" whichever command is highlighted, press Enter:
Note 1. The next time you use the function, it is usually enough to type the first letter of "upper" or "lower" words.
Note 2. You can also assign your own shortcut to these functions as they currently don't have any by default:
Original answer from 2017 (no longer valid for newer VSC versions)
To upper case: Ctrl+K, Ctrl+U
and to lower case: Ctrl+K, Ctrl+L.
Mnemonics:
K like the Keyboard
U like the Upper case
L like the Lower case
I think you can use
Select text
Ctrl+Shift+P
Enter Transform to Uppercase
Without defining keyboard shortcuts
Select the text you want capitalized
Open View->Command Palette (or Shift+Command+P)
Start typing "Transform to uppercase" and select that option
Voila!
At Sep 19 2018, these lines worked for me:
File-> Preferences -> Keyboard Shortcuts.
An editor will appear with keybindings.json file. Place the following JSON in there and save.
// Place your key bindings in this file to overwrite the defaults
[
{
"key": "ctrl+shift+u",
"command": "editor.action.transformToUppercase",
"when": "editorTextFocus"
},
{
"key": "ctrl+shift+l",
"command": "editor.action.transformToLowercase",
"when": "editorTextFocus"
},
]
I'm using the change-case extension and it works fine.
I defined the shortcuts:
{
"key": "ctrl+shift+u",
"command": "extension.changeCase.upper",
"when": "editorTextFocus"
},
{
"key": "ctrl+u",
"command": "extension.changeCase.lower",
"when": "editorTextFocus"
},
Standard keybinding for VS Code on macOS:
Selection to upper case ⌘+K, ⌘+U
and to lower case: ⌘+K, ⌘+L.
All key combinations can be opened with ⌘+K ⌘+S (like Keyboard Settings), where you can also search for specific key combinations.
Select the text to transform.
Use Ctrl + L to selected the whole line
Open Show all commands.
Linux and Windows: Ctrl + Shift + P, Mac: ⇧⌘P
Type in the command, e.g. lower, upper, title
Hit Enter
On a Mac, in Visual Studio Code, its very easy to add a key binding to perform this action, it is not linked to a hotkey combo as a default though.
In the menu bar, navigate to: Code > Preferences > Keyboard Shortcuts
In the search bar that comes up, type: Uppercase
A entry will come up called "Transform to Uppercase"
Hover your mouse over that entry and click the plus sign just to the left of the words "Transform to Uppercase"
In the box that comes up push the keys you want to bind that action to (Cmd +Shift + U is taken so I chose Ctrl + Shift + U) then press enter and you're good to go.
Note this is working at the time of this writing in May of 2021