I know I can change the line-numbering in Visual Studio Code to relative by adding the line "editor.lineNumbers": "relative", to the settings.json file, but I am looking for a way to bind it to a keybinding such that I can toggle between absolute (on) and relative (relative). I've messed with keybindings, but I can't find a command that can temporarily change settings.
I'd prefer to be able to have one key that toggles it between the two, but if there had to be a key to turn it to absolute and another to turn it to relative that would be fine.
Using the extension Settings Cycler, you can toggle between on and relative using a keyboard shortcut by inserting the following entry in your keybindings.json file:
{
"key": "ctrl+l",
"command": "settings.cycle",
"when": "editorTextFocus",
"args": {
"id": "relativeLineNumbers",
"values": [
{
"editor.lineNumbers": "on"
},
{
"editor.lineNumbers": "relative"
}
]
}
}
Related
When a popup like a video below is opened through this source action in the vscode editor, how do I navigate the menu using the j and k keys?
image vscode editor
temporary solution
I ended up solving this problem with a better touch tool or a karabiner keymapping tool. If you set it like the image below, you can navigate with ctrl + j,k keys. Of course, there are also side effects. You cannot use ctrl + j,k with the vim command. But since I don't use that command, this was enough to solve my problem.
keymapping image
It looks like in v1.71 these command id's will be changing, see Align new code action widget command names with:
We should try to align these command names with that ones that already
exist for the suggestion widget
onEnterSelectCodeAction -> acceptSelectedCodeAction
(acceptSelectedSuggestion)
focusNextCodeAction -> selectNextCodeAction
(selectNextSuggestion)
focusPreviousCodeAction -> selectPrevCodeAction
(selectPrevSuggestion)
Coming to vscode v1.70 are some commands for navigating the code actions menu (or quickfix menu - previous answer covers both).
Sample keybindings:
{
"key": "ctrl+k",
"command": "focusNextCodeAction", // in v1.70
// "command": "selectNextCodeAction" // in v1.71
"when": "codeActionMenuVisible"
},
{
"key": "down",
"command": "-focusNextCodeAction", // in v1.70
// "command": "-selectNextCodeAction", // in v1.71
"when": "codeActionMenuVisible"
},
{
"key": "ctrl+j",
"command": "focusPreviousCodeAction", // in v1.70
// "command": "selectPrevCodeAction", // in v1.71
"when": "codeActionMenuVisible"
},
{
"key": "ctrl+up",
"command": "-focusPreviousCodeAction", // in v1.70
// "command": "selectPrevCodeAction", // in v1.71
"when": "codeActionMenuVisible"
},
There [wasn't, see above] a built-in way to do that, see the github issue Missing keybinding for navigation in Quick Fix contextual menu.
There are a couple of workarounds mentioned in that issue, including the extension Keyboard Quickfix, made specifically for this issue.
Whenever there is some missing or needs correction in code, visual studio code shows a yellow bulb to show some quick fixes suggestions.
Is there any way or shortcut by which we can navigate up and down suggestion options shown as in screenshot above without using the arrow keys?
In vscode v1.71 there are three new commands you can use for navigating in the QuickFix menu to go to previous or next source code actions.
selectNextCodeAction // to focus the nextcode action
selectPrevCodeAction // to focus the previous code action
acceptSelectedCodeAction // to run the focused/selected code action
You can see the default keybindings below that have been removed. Note the - before the command name in two of the keybindings, that removes those keybindings.
You can make these keybindings (in your keybindings.json):
{
"key": "alt+u", // whatever you want here
"command": "selectPrevCodeAction", // for v1.71
"when": "CodeActionMenuVisible"
},
{
"key": "up", // default removed
"command": "-selectPrevCodeAction", // for v1.71
"when": "CodeActionMenuVisible"
},
{
"key": "alt+d", // whatever you want here
"command": "selectNextCodeAction", // for v1.71
"when": "CodeActionMenuVisible"
},
{
"key": "down", // default removed
"command": "-selectNextCodeAction", // for v1.71
"when": "CodeActionMenuVisible"
}
--------
There is also an extension presented as a fix:
Keyboard QuickFix, but it shouldn't be necessary anymore.
Go to "Keyboard shortcuts" and search for selectNextSuggestion.
You can rebind this command to any shortcut you like.
Same goes for selectPrevSuggestion.
I use cmd + j for up and cmd cmd + k for down ^^
There are many keyboard shortcuts and edit options in Visual Studio Code, I can move cursor: by character, by word, to beginning/end of line. But, if I'm editing a long line of text, how to faster jump to the middle of this line ?
Found similar question here, but for vim users - Visually select to the middle of line
Probably, it's possible to implement this option (jump to middle) with Vim extension. If so, then how to do it ?
Is there a way to make such behaviour natively without any extensions ?
Actually, this is built-in to vscode, using the cursorMove command. Set up this keybinding in your keybindings.json:
{
"key": "alt+c",
"command": "cursorMove",
"args": {
"to": "wrappedLineColumnCenter",
"select": true // default is false
}
}
You can use the extension Select By v1.0.0.
You can create a keybinding that calculates the new cursor position
{
"key": "ctrl+i ctrl+m", // or any other key binding
"when": "editorTextFocus",
"command": "moveby.calculation",
"args": {
"charNrEx": "currentLine.length / 2"
}
}
I believe that this is not covered by the Preview feature. I simply want to open a file for editing via Quick Open (or any way?) and replace the contents of the active tab, closing the open file and replacing it with the new one.
This behavior is central to the way I edit. Currently, I'm always opening new tabs that I don't want. It's the only barrier left between Code and the way I've used Vim for 15 years. I imagine that this is scriptable, but would like to avoid going down that road. Please tell me I'm missing something.
(1) The drastic approach: search for these in your settings:
Workbench > Editor > Limit: Enabled enable this
Workbench > Editor > Limit: Value set to 1
Drastic, because it will limit you to only 1 editor tab, probably not what you want but it does reuse the active (and only tab) of course.
(2) The macro approach:
Using a macro extension like multi-command put this into your settings.json
"multiCommand.commands": [
{
"command": "multiCommand.openFileInActiveEditor",
"sequence": [
"workbench.action.closeActiveEditor",
"workbench.action.acceptSelectedQuickOpenItem",
"workbench.action.closeQuickOpen" // if you want to close the quickopen panel immediately
]
}
]
and in keybindings.json:
{
"key": "alt+0", // whatever you want
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.openFileInActiveEditor" },
"when": "inFilesPicker && inQuickOpen"
},
It appears that you cannot override the usual right keybinding from the quickOpen panel so I set it to alt+right instead but you can pick whatever you want.
#Mark's answer almost gets you there, but it doesn't work with new (one tab) panes. Here's a modified version of his settings.json edit that does.
Install the multi-command extension
Put this in settings.json
"multiCommand.commands": [
{
"command": "multiCommand.openFileInActiveEditor",
"sequence": [
"workbench.action.acceptSelectedQuickOpenItem",
"workbench.action.previousEditor",
"workbench.action.closeActiveEditor",
"workbench.action.closeQuickOpen"
]
}
]
Put this in keybindings.json and replace the dummy value for the key key with your desired key combination
{
"key": "some+key+combination",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.openFileInActiveEditor" },
"when": "inFilesPicker && inQuickOpen"
},
In chrome and most other browsers/editors, we can go to a particular tab by pressing the Command key and the number. For example: If we press Command+1 we will go to the first tab, Command+2 takes to the second tab, etc.
Is it possible to get such a key mapping for Visual studio code ?
Since VSCode uses ctrl+number by default (which changes desktops in macOS), you can use cmd+number with custom keybindings:
Paste this in your keybindings.user:
{
"key": "cmd+1",
"command": "workbench.action.openEditorAtIndex1"
},
{
"key": "cmd+2",
"command": "workbench.action.openEditorAtIndex2"
},
{
"key": "cmd+3",
"command": "workbench.action.openEditorAtIndex3"
},
{
"key": "cmd+4",
"command": "workbench.action.openEditorAtIndex4"
},
{
"key": "cmd+5",
"command": "workbench.action.openEditorAtIndex5"
},
{
"key": "cmd+6",
"command": "workbench.action.openEditorAtIndex6"
},
{
"key": "cmd+7",
"command": "workbench.action.openEditorAtIndex7"
},
{
"key": "cmd+8",
"command": "workbench.action.openEditorAtIndex8"
},
{
"key": "cmd+9",
"command": "workbench.action.openEditorAtIndex9"
},
This is available now, with the latest version of Visual Studio Code. See: https://github.com/Microsoft/vscode/issues/24753#issuecomment-294518439
On macOS, the shortcuts default to ctrl + n, not cmd + n. You can fix that in Code -> Preferences -> Keyboard Shortcuts.
Another note: most other apps activate the last tab with cmd + 9. You get that behavior by using workbench.action.lastEditorInGroup instead of workbench.action.openEditorAtIndex9.
Unfortunately, no. But you can configure shortcuts to move between tabs, such as going to the previous, next, previously modified, etc. tabs. You can make changes in the Options menu under Keyboard.
This comes standard with the "Emacs Keymap" extension as well as a multitude of other "never touch the mouse" keybindings that I, personally, find keep my stress level way down while navigating code.
Now all I need to find is a way to put the tab's number on the tab itself (which is what I was searching when I found this question).