remap keys vscode to emit different values - visual-studio-code

In my previous setup, I was somehow able to remap ²-key (completely useless) into emitting -> (=aka pointer access in C), but I can't find how I did it, neither I can find action in the documentation how to "emit" value upon pressing a key.
How should I do that?

In keybindings.json
{
// "key": "²",
"key": "alt+2",
"command": "editor.action.insertSnippet",
// "when": "resourceExtname == .c", // if you want this
"args": {
"snippet": "->$0"
}
},
I'm not sure how you are inserting the superscript ² which could change the answer, but this example works.

Related

how to put increased number for multiline selection in visual studio code

I want to add increased number for multi selected caret in visual studio code.
now, When I type it write same words.
But I would like to add increased number by some shortkey so that I don't need to update each one manually.
Preferred result should be like this.
I want to know if this is possible in vs code.
Thanks
You do not need an extension for your use case, although that may make it easier. Here is how to do it without an extension.
Find: (?<=index:\s*)\d+ : this selects only the digits following index: .
Alt+Enter will select all those digits.
Now you can run a simple snippet to replace those digits with an increasing number that could be 0-based or 1-based. Make this keybinding to insert the snippet (in your keybindings.json):
{
"key": "alt+m", // whatever keybinding you want
"command": "editor.action.insertSnippet",
"args": {
"snippet": "$CURSOR_NUMBER" // this will increment and is 1-based
}
}
Trigger the above keybinding. Demo:
Here is an extension approach, using an extension I wrote, Find and Transform, that makes this easy. Make this keybinding:
{
"key": "alt+m", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"find": "(?<=index:\\s*)\\d+", // same find regex
"replace": "${matchNumber}", // this variable will increase, 1-based
"isRegex": true
}
}
That combines the find and replace in one step.
Here is another method so you do not need to hardcode the starting point.
{
"key": "alt+m", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"preCommands": [
"editor.action.addSelectionToNextFindMatch",
"editor.action.clipboardCopyAction"
],
"find": "(?<=index:\\s*)\\d+",
"replace": [
"$${",
// whatever math you want to do here
"return Number(${CLIPBOARD}) + ${matchIndex};",
"}$$",
],
"isRegex": true,
"postCommands": "cancelSelection"
}
}
Put the cursor next to or select the number you want as the starting point. The number could be anywhere in the document actually.
You can use the extension Regex Text Generator
Define the following key binding
{
"key": "ctrl+shift+f9", // or any other key combo
"when": "editorTextFocus",
"command": "regexTextGen.generateText",
"args": {
"generatorRegex" : "{{=i+1}}"
}
}
place the multi cursors after index:
press the key combo
accept or modify the inputs
look at the preview, press Enter if you like it, Esc to abort
You can do it with Increment Selection or Text Pastry

Bind a command to go to a specific symbol in VS Code

My issue:
I would like to bind a few keyboard shortcuts to navigate to specific symbols in my currently open file within VS Code.
Example:
I want to navigate to the symbol <template> by pressing cmd+1.
I want to navigate to the symbol <script> by pressing cmd+2.
I want to navigate to the symbol <style> by pressing cmd+3.
By default, you can do that by pressing cmd+p and then in the palette typing #template which will jump to the template tag within the open file.
So a solution might look something like this in my keybinding.json:
{
"key": "cmd+1",
"command": "workbench.action.gotoSymbol",
"args": ["#template"],
}
However, the args part of this does not work. I'm wondering if anyone knows a good solution for setting up key bindings like this so I can navigate around my files with shortcuts.
Thanks in advance!
Try this:
{
"key": "cmd+1",
"command": "workbench.action.quickOpen",
"args": "#<template>",
},
The command workbench.action.gotoSymbol won't take an argument, but workbench.action.quickOpen will. I don't know why there is that difference, but as you know if you start the Quick Open Panel with # you get object references. And the rest of your text <template> for example will automatically be entered into the input box.
It will filter for those symbols but will not jump to the first one. If you want that additional functionality, you would have to consider a macro which would select the first entry.
There is an alternative that allows you to navigate to anything by using a matching regex. See the extension Select By which allows to jump to any string. For example (in settings.json):
"selectby.regexes": {
"goTo<template>": {
"moveby": "<template>",
}
}
and your keybinding (keybindings.json):
{
"key": "ctrl+1", // <template>`
"command": "moveby.regex",
"args": ["go<template>", "moveby", "next", "start", "wrap"],
// "when": "editorTextFocus && editorLangId == javascriptreact"
}
You can set it up to jump to the previous instance as well:
{
"key": "ctrl+alt+1", // previous <template>
"command": "moveby.regex",
"args": ["goTo<template>", "moveby", "prev", "start", "wrap"],
// "when": "editorTextFocus && editorLangId == javascriptreact"
}
either way it should now "wrap", it just depends on how many <templates> you have in the file and which direction you want to go to first.
I found the solution to my problem, which is pretty funny because it's an existing extension for VS Code that does exactly what I am looking for:
https://marketplace.visualstudio.com/items?itemName=andersonmfjr.vue-jumptotag
To bind the keyboard shortcuts in keybindings.json
{
"key": "cmd+1",
"command": "extension.jumptotemplate",
},
{
"key": "cmd+2",
"command": "extension.jumptoscript",
},
{
"key": "cmd+3",
"command": "extension.jumptostyle",
},
That does exactly what I was looking for.

Toggling the maximization of the current window (for any window type)

I want to define a single and universal keyboard binding that allows me to toggle the maximization of the current window, regardless of the type of window.
At a minimum, a window could be:
Any single editor group
The panel itself
By maximization, note that I mean in both height and width so that the window takes over the full application window with the exception of the side bar (i.e. if the side bar is already visible, it remains visible at all times).
By toggling I mean of course going back and forth between the original layout (i.e. remembering it), and the maximization result.
My thinking about this problem
There are several command IDs that I believe could help here but none of them seem to do what I want, e.g.
workbench.action.toggleEditorWidths
workbench.action.maximizeEditor
workbench.action.toggleMaximizePanel
workbench.action.toggleEditorVisibility
I suspect that
there are more commands that could help here
there is probably a way to accomplish this with the right boolean logic of when clauses and joint execution of command IDs (?)
Try this in your keybindings.json (chose your desired keybinding):
{
"key": "alt+m",
"command": "workbench.action.toggleMaximizedPanel",
"when": "terminalFocus"
},
{
"key": "alt+m",
"command": "workbench.action.closePanel",
"when": "editorFocus && panelIsOpen"
},
{
"key": "alt+m",
"command": "workbench.action.togglePanel",
"when": "editorFocus && !panelIsOpen"
},
It works well - has no effect on the SideBar or ActivityBar - except for one thing. The workbench.action.togglePanel command in the third keybinding will appear to toggle the maximized editor but that command shifts focus to the opened panel. Which might not be what your expect.
If you don't want that focus shift to the panel when un-maximizing the editor I think you will have to add a macro to the mix. Disable the second and third keybindings and add this to your settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.toggleEditorWthFocus",
"sequence": [
"workbench.action.togglePanel",
"workbench.action.focusActiveEditorGroup",
"workbench.action.toggleEditorWidths" // will toggle other editor groups width
// but other editor group width goes to a built-in minimum, not 0
]
}
],
using the multi-command macro extension.
and use these as your two keybindings:
{
"key": "alt+m",
"command": "workbench.action.toggleMaximizedPanel",
"when": "terminalFocus"
},
//{
// "key": "alt+m",
// "command": "workbench.action.closePanel",
// "when": "editorFocus && panelIsOpen"
//},
// {
// "key": "alt+m",
// "command": "workbench.action.togglePanel",
// "when": "editorFocus && !panelIsOpen"
// },
{
"key": "alt+m",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.toggleEditorWthFocus" },
"when": "editorTextFocus && !panelIsOpen"
},
I know you said you want the SideBar unchanged, but if you go the macro route I think it would be pretty easy to toggle that as well.
The comments above note that if you have more than one editor group, unfocused editor groups' width will be minimized, but it goes to some built-in minimum and not to 0 width. I don't think there is a way to toggle other editor groups on/off other than by closing other editor groups which can be done but you probably don't want that.
Demo with split:

vscode custom key binding length issue

I am binding custom keys with keybindings.json file like below,
{
"key": "s a",
"command": "workbench.action.files.saveAll",
"when": "vim.mode == 'Normal'"
}, {
"key": "q u",
"command": "workbench.action.closeActiveEditor",
"when": "vim.mode == 'Normal'"
}
It seems that vscode supports only custom keys length to 2.
Is there any way to binding keys (length) more than that?
for example
{
"key": "s a v e",
"command": "workbench.action.files.saveAll",
"when": "vim.mode == 'Normal'"
}, {
"key": "q u i t",
"command": "workbench.action.closeActiveEditor",
"when": "vim.mode == 'Normal'"
}
thank you.
I ran into the same problem but could not find the fix to this issue despite my researches, curious about this one...
Meanwhile, I use a workaround with betterTouchTool for Mac Os and it does the job perfectly (you can assign key sequences per app or globally). For instance I set the key sequence pry to be replaced by binding.pry on the fly (with keybindings.json it was triggered too early, as soon as I hit the 'r'), but you can as well trigger any other action.
I guess it's not the only app allowing this kind of customisation but I use this one a lot and didn't search any further.
Hope this helps!

Replace with next value (ctrl shift dot) in Visual Studio Code

Does anyone know how "Replace with next value" (ctr + shift + dot) works ?
I am unable to get this shortcut to do anything on my vscode.
I tried googling examples of this shortcut and asking on vscode gitter/reddit but couldn't get any answers.
It would be greatly appreciated if someone could provide the steps of using this shortcut.
If I press the shortcut after highlighting an integer or float literal (16 or 5.2 for example) it subtracts 1 from it.
Strangely, Replace with Previous Value adds 1 to the value while Replace with Next Value subtracts 1.
I don't know if it has any other purpose.
Add to the accepted answer:
If you use it on a boolean, it toggles between true and false.
No need to "highlight" the number, putting the cursor on it is enough.
As noted by #Félix Caron, this is how to swap the two keys.
keybindings.json
{ "key": "ctrl+shift+oem_period", "command": "editor.action.inPlaceReplace.up", "when": "editorTextFocus && !editorReadonly" },
{ "key": "ctrl+shift+oem_comma", "command": "editor.action.inPlaceReplace.down", "when": "editorTextFocus && !editorReadonly" },
Moreover, in my case as I have breadcrumbs enabled "breadcrumbs.enabled": true,, the replaceUp key triggered breadcrumbs instead, so I had to unbind it.
{ "key": "ctrl+shift+oem_period", "command": "-breadcrumbs.toggleToOn", "when": "!config.breadcrumbs.enabled" },
{ "key": "ctrl+shift+oem_period", "command": "-breadcrumbs.focusAndSelect", "when": "breadcrumbsPossible" },