A keybindings.json per workspace in Visual Studio Code - visual-studio-code

Is it possible to have a keybindings.json as part of the workspace settings instead of the user settings?
Since I have specific tasks associated to my workspace, I would also like to be able to assign workspace-specific shortcuts. Because I would use these shortcuts on my separate Windows and Linux environments, I would not prefer to see these key binding definitions end up amongst my environment-specific user settings.

This is not possible. Here Alexandru Dima (alexandrudima) explains why they do not want to add this feature and furthermore suggests to create your custom extension that contributes only keybindings to share them.

Not a perfect solution, but I found a workaround to create a keybinding that can be activated at a workspace level.
In the workspace .vscode/settings.json file, set a new setting to true. (Because the setting is not registered by vscode or any extension, it will be displayed faded out in the editor - This is OK):
"workspaceKeybindings.myAwesomeTask.enabled": true // setting can be anything you want
Then create the keyboard shortcut in the user keybindings.json, and add a "when" condition for the setting you created:
{
"key": "ctrl+; ctrl+n",
"command": "workbench.action.tasks.runTask",
"args": "My Awesome Task",
"when": "config.workspaceKeybindings.myAwesomeTask.enabled" // Must be "config.{settingName}"
},
This keybinding can be set as active in any workspaces you want.

as of July 10, 2022, it doesn't seem possible to have a .vscode/keybindings.json per workspace, however it is possible to get keybindings that are active only when you're in a certain folder:
In your keybindings file, you can put:
[
{
"key": "...",
"command": "..." ,
"when": "resourceDirname == '/Users/johndoe/path/to/folder'",
...
},
]
You also have all these other contexts:
resource: "vscode-userdata:/Users/johndoe/path/to/file.json"
resourceDirname: "/Users/johndoe/path/to/"
resourceExtname: ".json"
resourceFilename: "file.json"
resourceLangId: "jsonc"
resourcePath: "/Users/johndoe/path/to/file.json"
resourceScheme: "vscode-userdata"
and more...
To find all possible context keys:
Run the Developer: Toggle Developer Tools vscode command
Then run the Developer: Inspect Context Keys vscode command and then click anywhere
Expand the context object in the console, and you should see a full list of context key-value pairs

Related

cannot find real location of %APPDATA%\Code\User\settings.json in explorer

I know I can edit settings.json in vscode but I want to actually locate the physical file. According to vscode doc https://code.visualstudio.com/docs/getstarted/settings the user settings file is located here:
Windows %APPDATA%\Code\User\settings.json
but when going to it with explorer I cannot see any settings.json
Settings file locations
Depending on your platform, the user settings file is located here:
Windows %APPDATA%\Code\User\settings.json
from Settings documentation
But how do figure out where %APPDATA% is on your system? One easy way is to create a keybinding (in your keybindings.json) like this:
{
"key": "alt+q", // whatever you want
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "echo ${env:APPDATA}"
}
},
When this is run the variable ${env:APPDATA} will be resolved and printed out.
It will be something like this:
C:\Users\Cerberus\AppData\Roaming
so for the full path add \Code\User\settings.json to the end of that. Or use this in the keybinding:
"text": "echo ${env:APPDATA}\\Code\\User\\settings.json"
outputs: C:\Users\Rodan\AppData\Roaming\Code\User\settings.json

VSCode debugging specific configuration with CodeLLVM

I was able to generate a launch.json file with the VSCode Rust debugger; The tool is smart enough to create a configuration array with multiple configurations, one for each package in my project; so far so good. Except that a Start Debugging always choose the first configuration in the list.
Do I need to place the configuration I want to launch at the beginning of the array, or is there a way to specify a specific name in the list?
Open the Run and Debug sidebar (, Ctrl/Cmd+Shift+D), and in the list choose the configuration to launch.
For one time, you can open the Command Palette (Ctrl/Cmd+Shift+P), and choose "Debug: Select and Start Debugging". Then choose the desired configuration.
If you want to bind it to a shortcut, edit your keybindings.json and add:
{
"key": /* shortcut, e.g. */ "ctrl+alt+k ctrl+alt+k",
"command": "debug.startFromConfig",
"args": /* full launch configuration. you cannot rely on configurations specified in launch.json, unfortunately */ {}
}
Unfortunately, there's no way to apply keybindings per-workspace. See A keybindings.json per workspace in Visual Studio Code.
See Allow key binding for individual launch configurations for more.

Is there any keyboard shortcut to switch (or choose) a given launch target?

I have a Visual Studio Code project configured with multiple launch targets.
I'm aware I can select them on the Run dropdown in the Run view, however, I'd like to perform this operation via keyboard.
Is it possible? I can't find the action/keyboard shortcut in the keyboard shortcuts editor.
Although there is no direct shortcut, it's possible to assign one to Select and Start Debugging, which yields essentially the same effect.
See the extension Launch A Config. You can make multiple keybindings to any of your launch configs. Example keybinding:
{
"key": "alt+f",
"command": "launches.RunNodeCurrentFile"
},
where in your settings.json you link that command to sone launch config name:
"launches": {
"RunNodeCurrentFile": "Launch File",
"Test": "Run Extension Tests",
// and more
},

cmd+c is not working on Visual Studio Code

I have following in settings:
{ "key": "cmd+c", "command": "editor.action.clipboardCopyAction",
"when": "textInputFocus" },
This is driving me crazy, have tried everything but am unable to make copy command work in Visual studio code. I have to write click and then select copy. Shortcut does not show in VSC. Does anyone know why?
Open your Keyboard Shortcuts by going to File > Preferences > Keyboard Shortcuts.
In the search bar at the top, type "cmd+c" to search for all shortcuts using those two keys. Now look at the "When" column for all instances of "textInputFocus". You need to make sure that the only command mapped to cmd+c during textInputFocus is the Copy command.
Below is an example where ctrl+c will fail to copy on my Windows setup because a second command is mapped to ctrl+c during textInputFocus.
I don't know why, but in my case I needed to remove the following default keybinding in order for Command+C to start functioning again, therefor my keybindings.json file now looks like this:
[
{
"key": "cmd+c",
"command": "-search.action.copyMatch",
"when": "fileMatchOrMatchFocus"
}
]
(Perhaps using VSCode on a Mac machine and a Linux machine is related, however but this is a wild guess).
In my case with macOS (every time after cmd+C had clicked the behavior was like the keyboard turned on "insert" mode). I deleted the default shortcut, and without specific changes turned back the totally same shortcut, after restart VSCode all works fine

How to add semicolon to the end of the line in visual studio code

I press Shift+Enter, but not working, Ctrl+Enter start a new line, but not add semicolon at the end of previous line. Is there a shortcut?
Now there is an extension called Colonize:
Shift+Enter Insert semicolon at the end of line and continue on the same line
Alt+Enter Insert semicolon at the end of line and continue on the new line
Ctrl+Alt+Enter Insert semicolon and stay at the same position
Visual Studio Code doesn't appear to directly support adding a semi-colon to the end of a line via a keyboard shortcut, meaning it'll need a custom extension to support it. Fortunately, Sergii Naumov has created one for us, named (appropriately) 'Trailing Semicolon'.
To install this extension:
Launch Visual Studio Code
Press "Ctrl+P" (or "Command+P" on the Mac)
type "ext install Trailing Semicolon" to search for the extension
With the extension visible below the search box, click the Install Extension icon in the bottom right of the extension. (It's a cloud with an arrow pointing down...)
You will need to restart Visual Studio Code to enable the extension (Visual Studio Code should prompt you to restart once the extension has been successfully downloaded.)
According to Sergii, the default keybinding for the extension is "cmd+;". On my Mac, this meant pressing "Command+;", which worked great.
On my Windows 10 PC, I tried several key combinations ("Window+;" "Ctrl+;" "Alt+;" etc.), and nothing worked. I opened the Keyboard Shortcut preferences (File > Preferences > Keyboard Shortcuts) and searched for the new extension. I found it listed at the end of the keybindings:
{ "key": "win+;", "command": "extension.trailing-semicolon",
"when": "editorTextFocus" }
The "win" key binding apparently doesn't work. I copied this binding to the keybindings.json file, changed "win+;" to "ctrl+;" and it worked like a charm!
Hope that helps.
I wrote an extension to mimic IntelliJ's complete statement.
Pressing ctrl+; (cmd+; on mac) appends ; to the line end,
and moves cursor to line end.
If the line already ends with ;, pressing ctrl+; just moves cursor to line end.
There is also experimental support for complete structures like class, interface, function, if, switch, for, and while. (Not understanding semantic of languages, so it may not work as you expected.)
You can install it as VSIX at GitHub.
The version vscode marketplace is outdated. (Unfortunately my M$ account has been suspended, thus I cannot update it.)
There is no way to do it by default that I could find. I just had to make do as best I could.
I ended up adding a binding via File>Preferences>Keyboard Shortcuts, and then pressing ; after the shortcut. Having the semicolon is part of the binding is as close as I could get... at least my finger is already over that key
// Place your key bindings in this file to overwrite the defaults
[
{
"key": "ctrl+;",
"command": "cursorEnd",
"when": "editorTextFocus"
}
]
This simply means that Ctrl+; brings you to the end of the line, and then tap ; again.
Update (May 2021)
Started using VSCode Vim keybindings extension a few months ago. Highly recommend it. Out of interest, semi-colon end of the line is A; with vim keys⌨️
at 2021
settings >search for colon
You will find a javascript option to insert the semi-colon.
Try install the following ext then use shortcut: Ctr+; (Cmd+;)
There is a way to have semicolon appended automatically by VSCode.
By default this property is disabled:
For Windows ctrl + ,
For Mac cmd + ,
Settings -> in Search bar type: 'semicolon' -> change values to 'insert' for both JS and TS, save and close.
Now every time on save, VSCode will look for missing semicolons inside the file you're in and append them.
I just started using Visual Studio Code and felt this requirement myself yesterday. After a quick google search I found this nice extension called "Prettier". Being a little new to VSCode it took me a few hours to get it all setup but it works like a charm now. Here are the steps and my setup. I hope it helps others.
My coding environment: VSCode running on a Windows 10 desktop environment connecting to my codebase SMB share hosted on my development machine which is running Ubuntu server 18.04.
Solution Steps
Install node on the Windows desktop
Run, npm install -g prettier
Install the Prettier extension in VSCode
Edit the settings.json file for VSCode and add the following
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
// Set the default
"editor.formatOnSave": true
Add the .prettierrc file at the root of my codebase on the ubuntu host (e.g.: /var/www/html/tutorials) with the following basic styling configuration
{
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 80
}
Restart VSCode and open the code file
Use the existing VSCode keyboard shortcuts to apply formatting to the entire file (Ctrl+K Ctrl+F) or to a selection (Shift+Alt+F)
Or simply saving the file Ctrl+S adds the formatting while saving the file with no additional work required
Viola!
(March 2022) Missing semi colons can be added automatically without the needing of an extension (*with a little catch ;) They are added every time your code is formatted.
Go to settings: Look for this settings if you are writing javascript:
And/or go to settings: Look for this settings if you are writing typescript:
Go to settings: Look for this settings if you are writing javascript:
Format your code and semi colons will be added \o/ Yeah
*The catch: As the settings says: Your workspace needs to have typescript 3.7 or newer.
I know this is a really old post but Prettier - code formatter for vs code adds the semi colon (amongst other formatting).
ext install esbenp.prettier-vscode
If you need semicolons on Javascript/Typescript using vscode do:
Settings
type "semicolon"
you will have options for Javascript & Tyepscript
select the "insert" option as desired
You can also select semicolons for css, less, or scss
/*******/
You can select the "Show matching extensions" and get some extra help with extensions that address the same issue.
Go to settings: Look for this settings if you are writing javascript: enter image description here
And/or go to settings: Look for this settings if you are writing typescript: enter image description hereGo to settings: Look for this settings if you are writing javascript:
Format your code and semi colons will be added \o/ Yeah
*The catch: As the settings says: Your workspace needs to have typescript 3.7 or newer.
This solution works for me
Add the following configuration in settings.json:
"css.completion.completePropertyWithSemicolon": true
or you can cancel by:
"css.completion.completePropertyWithSemicolon": false
other
"scss.completion.completePropertyWithSemicolon": true,
"less.completion.completePropertyWithSemicolon": true
Add semicolon to the end of the line and go to the next line
Shortcode: ctrl+;
Install plugin: multicomando
ext install ryuta46.multi-command
Edit file: keybindings.json
// Place your key bindings in this file to override the defaults
[
{
"key": "ctrl+;",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"cursorEnd",
{ "command": "type", "args": { "text": ";" } },
"lineBreakInsert",
"cursorDown"
]
},
"when": "editorTextFocus"
}
]
This plugin promotes a sequence of commands. Infinite possibilities!
I hope it helps someone!
You can try Semicolon Insertion Shortcut.
It's in Visual Studio's Preferences. Solution from sbi's answer here:
Go to Tools/Options/Environment/Keyboard.
Switch the "Use new shortcut in:" dropdown to "Text Editor".
Pick the Edit.BreakLine command.
In the Press shortcut keys edit pane press Shift+Enter.
Worked for me!
Ctrl+Shift+P and open Preferences:Open keyboard shortcuts (JSON) in VS Code
and insert
{
"key": "tab",
"command":"cursorEnd",
"when":"editorTextFocus"
}
tab is optional, ofc.
Put whatever you want from the keys on the keyboard.