I do both python and node coding. I have a keybinding for console.log() in Visual Studio Code.
{
"key": "ctrl+shift+l",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "console.log('$0',)"
}
}
I want this same shortcut to detect the .py extension and print print('',) and detect .js, .ts file extension and print console.log('',)
{
"key": "ctrl+shift+l",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus && editorLangId == javascript",
"args": {
"snippet": "console.log('$0',)"
}
},
{
"key": "ctrl+shift+l",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus && editorLangId == typescript",
"args": {
"snippet": "console.log('$0',)"
}
},
{
"key": "ctrl+shift+l",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus && editorLangId == python",
"args": {
"snippet": "print('$0',)"
}
},
Related
I have some custom shortcuts to move with cmd+up and cmd+down with intervals of 5 lines.
{
"key": "cmd+up",
"command": "cursorMove",
"args": {
"to": "up",
"by": "line",
"value": 5
},
"when": "editorTextFocus"
},
{
"key": "cmd+down",
"command": "cursorMove",
"args": {
"to": "down",
"by": "line",
"value": 5
},
"when": "editorTextFocus"
},
What I would like is when pressing shift+cmd+[up,down] to select 5 lines up and down. I've found that there are a few "commands" such as {cursorDownSelect, cursorPageDownSelect, CursorEndSelect} but none of them allow me to define some args to jump a few lines, does anybody know how to do it ?
You can add the select option to the cursorMove command. Search for cursorMove at this commands page: https://code.visualstudio.com/api/references/commands
{
"key": "cmd+up",
"command": "cursorMove",
"args": {
"to": "up",
"by": "line",
"value": 5
"select": true // the default is false
},
"when": "editorTextFocus"
},
{
"key": "cmd+down",
"command": "cursorMove",
"args": {
"to": "down",
"by": "line",
"value": 5,
"select": true
},
"when": "editorTextFocus"
}
I use sublime text editor for writing code, and I want to set up a key binding for arrow keys, so I don't need to move my right hand frequently. To do so, I added code in sublime-keymap:
[
{ "keys": ["alt+j"], "command": "move", "args": {"by": "characters", "forward": false} },
{ "keys": ["alt+l"], "command": "move", "args": {"by": "characters", "forward": true} },
{ "keys": ["alt+i"], "command": "move", "args": {"by": "lines", "forward":false} },
{ "keys": ["alt+k"], "command": "move", "args": {"by": "lines", "forward": true} },
]
But, alt+j and alt+k are not working. Please help.
I tried to key bind in VS code too, the same problem occurs.
I am using OS Windows 10; the hotkeys defined by windows is creating this issue?
For vscode only, the commands are a little different (in your keybindings.json):
{
"key": "alt+j",
"command": "cursorMove",
"args": {
"to": "left",
"by": "character"
},
"when": "editorTextFocus"
},
{
"key": "alt+l",
"command": "cursorMove",
"args": {
"to": "right",
"by": "character"
},
"when": "editorTextFocus"
},
{
"key": "alt+i",
"command": "cursorMove",
"args": {
"to": "up",
"by": "line"
},
"when": "editorTextFocus"
},
{
"key": "alt+k",
"command": "cursorMove",
"args": {
"to": "down",
"by": "line"
},
"when": "editorTextFocus"
}
This is for moving the cursor within an editor. If you wanted arrow key-like functionality in a list - like the Explorer files - you would have to add some more keybindings with different commands. But it looks like you just want within a text editor.
I am trying to make sure wherever you can use a cursor key to move the selection/cursor you can also use alt + h/j/k/l. I have managed it in many places, but can't get it working for the file explorer. I want to move up and down using j/k. Here is what I have so far, is this optimal, and how to I add the file explorer?
// Place your key bindings in this file to overwrite the defaults
[
// use alt + h, j, k and l as cursors
//
// when in textInputFocus mode move cursor
{
"key": "alt+k",
"command": "cursorUp",
"when": "textInputFocus && !suggestWidgetVisible"
},
{
"key": "alt+j",
"command": "cursorDown",
"when": "textInputFocus && !suggestWidgetVisible"
},
{
"key": "alt+h",
"command": "cursorLeft",
"when": "textInputFocus && !suggestWidgetVisible"
},
{
"key": "alt+l",
"command": "cursorRight",
"when": "textInputFocus && !suggestWidgetVisible"
},
// when in listFocus mode
{
"key": "alt+j",
"command": "list.focusDown",
"when": "listFocus"
},
{
"key": "alt+k",
"command": "list.focusUp",
"when": "listFocus"
},
// when in quick open mode
{
"key": "alt+j",
"command": "workbench.action.quickOpenSelectNext",
"when": "!editorFocus"
},
{
"key": "alt+k",
"command": "workbench.action.quickOpenSelectPrevious",
"when": "!editorFocus"
},
// when in suggestion mode
{
"key": "alt+j",
"command": "selectNextSuggestion",
"when": "editorTextFocus && suggestWidgetMultipleSuggestions && suggestWidgetVisible"
},
{
"key": "alt+k",
"command": "selectPrevSuggestion",
"when": "editorTextFocus && suggestWidgetMultipleSuggestions && suggestWidgetVisible"
},
// when in smartSelect mode
{
"key": "shift+alt+l",
"command": "editor.action.smartSelect.expand",
"when": "editorTextFocus"
},
{
"key": "shift+alt+h",
"command": "editor.action.smartSelect.shrink",
"when": "editorTextFocus"
},
]
The answer is here, I copy/pasted all cursor actions from latest definition and replaced with additional shortcut:
keybindings.json
[
// textInputFocus
{
"key": "alt+k",
"command": "cursorUp",
"when": "textInputFocus"
},
{
"key": "alt+j",
"command": "cursorDown",
"when": "textInputFocus"
},
{
"key": "alt+l",
"command": "cursorRight",
"when": "textInputFocus"
},
{
"key": "alt+h",
"command": "cursorLeft",
"when": "textInputFocus"
},
// historyNavigation
{
"key": "alt+k",
"command": "history.showPrevious",
"when": "historyNavigationEnabled && historyNavigationWidget"
},
{
"key": "alt+j",
"command": "history.showNext",
"when": "historyNavigationEnabled && historyNavigationWidget"
},
// listFocus
{
"key": "alt+k",
"command": "list.focusUp",
"when": "listFocus && !inputFocus"
},
{
"key": "alt+j",
"command": "list.focusDown",
"when": "listFocus && !inputFocus"
},
{
"key": "alt+l",
"command": "list.expand",
"when": "listFocus && !inputFocus"
},
{
"key": "alt+h",
"command": "list.collapse",
"when": "listFocus && !inputFocus"
},
// explorerViewletCompressedFocus
{
"key": "alt+l",
"command": "nextCompressedFolder",
"when": "explorerViewletCompressedFocus && explorerViewletVisible && filesExplorerFocus && !explorerViewletCompressedLastFocus && !inputFocus"
},
{
"key": "alt+h",
"command": "previousCompressedFolder",
"when": "explorerViewletCompressedFocus && explorerViewletVisible && filesExplorerFocus && !explorerViewletCompressedFirstFocus && !inputFocus"
},
// notificationFocus
{
"key": "alt+k",
"command": "notifications.focusPreviousToast",
"when": "notificationFocus && notificationToastsVisible"
},
{
"key": "alt+j",
"command": "notifications.focusNextToast",
"when": "notificationFocus && notificationToastsVisible"
},
{
"key": "alt+l",
"command": "notification.expand",
"when": "notificationFocus"
},
{
"key": "alt+h",
"command": "notification.collapse",
"when": "notificationFocus"
},
// suggestWidget
{
"key": "alt+k",
"command": "selectPrevSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
{
"key": "alt+j",
"command": "selectNextSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
// editorFocus
{
"key": "alt+k",
"command": "showPrevParameterHint",
"when": "editorFocus && parameterHintsMultipleSignatures && parameterHintsVisible"
},
{
"key": "alt+j",
"command": "showNextParameterHint",
"when": "editorFocus && parameterHintsMultipleSignatures && parameterHintsVisible"
},
// interactivePlaygroundFocus
{
"key": "alt+k",
"command": "workbench.action.interactivePlayground.arrowUp",
"when": "interactivePlaygroundFocus && !editorTextFocus"
},
{
"key": "alt+j",
"command": "workbench.action.interactivePlayground.arrowDown",
"when": "interactivePlaygroundFocus && !editorTextFocus"
},
// smartSelect
{
"key": "shift+alt+l",
"command": "editor.action.smartSelect.expand",
"when": "editorTextFocus"
},
{
"key": "shift+alt+h",
"command": "editor.action.smartSelect.shrink",
"when": "editorTextFocus"
}
]
I like the use the German umlauts "ö", "Ö", "ä", and "Ä" on my keyboard for coding in VSCode, i.e., use these keys to type square and curly brackets. Here is what I tried in keybindings.json:
{ "key": "ö", "command": "type", "args": { "text": "[" }, "when": "editorTextFocus" },
{ "key": "ä", "command": "type", "args": { "text": "]" }, "when": "editorTextFocus" },
{ "key": "Shift+ö", "command": "type", "args": { "text": "{" }, "when": "editorTextFocus" },
{ "key": "Shift+ä", "command": "type", "args": { "text": "}" }, "when": "editorTextFocus" },
{ "key": "Alt-ö", "command": "type", "args": { "text": "ö" }, "when": "editorTextFocus" },
{ "key": "Alt-ä", "command": "type", "args": { "text": "ä" }, "when": "editorTextFocus" },
{ "key": "Alt-Shift+ö", "command": "type", "args": { "text": "Ö" }, "when": "editorTextFocus" },
{ "key": "Alt-Shift+ä", "command": "type", "args": { "text": "Ä" }, "when": "editorTextFocus" }
VSCode complains:
You won't be able to produce this key combination under your current
keyboard layout.
Is there an easy way to teach VSCode to allow bindings for any key instead of just the predefined ones?
These are allowed, the pre-defined "Toggle Integrated Terminal" shortcut is Ctrl+ö after all. You just can't write the characters literally in JSON.
I usually prefer to use the JSON editor myself as well, but this is actually a case where the UI is quite helpful. In the "please enter desired key combination" popup, you can see that with a QWERTZ keyboard...
...ö turns in to oem_3
...ä turns in to oem_7
...ü turns in to oem_1
Thanks #Gama11 for the hint regarding the UI. I tried it and got the keys [Semicolon], [Quote], and [BracketLeft], for ö, ä, and ü for my German keyboard + layout.
Here is my working keybindings.json:
{ "key": "[Semicolon]", "command": "type", "args": { "text": "[" }, "when": "editorTextFocus" },
{ "key": "[Quote]", "command": "type", "args": { "text": "]" }, "when": "editorTextFocus" },
{ "key": "Shift+[Semicolon]", "command": "type", "args": { "text": "{" }, "when": "editorTextFocus" },
{ "key": "Shift+[Quote]", "command": "type", "args": { "text": "}" }, "when": "editorTextFocus" },
{ "key": "Alt+[Semicolon]", "command": "type", "args": { "text": "ö" }, "when": "editorTextFocus" },
{ "key": "Alt+[Quote]", "command": "type", "args": { "text": "ä" }, "when": "editorTextFocus" },
{ "key": "Shift+Alt+[Semicolon]", "command": "type", "args": { "text": "Ö" }, "when": "editorTextFocus" },
{ "key": "Shift+Alt+[Quote]", "command": "type", "args": { "text": "Ä" }, "when": "editorTextFocus" }
It works perfectly for the mapped umlaut keys and does not interfere with the regular ; and " keys.
Much like in ipython, is it possible in VScode to use tab to select options from intellisense instead of using arrow keys?
In your keybindings.json:
{
"key": "tab",
"command": "selectNextSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
{
"key": "down",
"command": "-selectNextSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
{
"key": "ctrl+tab",
"command": "selectPrevSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
{
"key": "up",
"command": "-selectPrevSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
}
Here is my one. (keybindings.json)
{
"key":"alt+0",
"command":"selectNextSuggestion",
"when":"suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
{
"key":"alt+9",
"command":"selectPrevSuggestion",
"when":"suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
}
Here is the answer from #Mark in the right format for your keybindings.json assuming your start from an empty keybindings.json (can be found easily via F1 menu):
[
{
"key": "tab",
"command": "selectNextSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
{
"key": "down",
"command": "-selectNextSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
{
"key": "ctrl+tab",
"command": "selectPrevSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
{
"key": "up",
"command": "-selectPrevSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
}
]