In VSCode, when I have:
/*
* Comment
*/
If I select it and hit tab, I get:
/*
* Comment
*/
If instead I had hit shift-tab, I get:
/*
* Comment
*/
Same happens with Ctrl-] and Ctrl-[ (if those are supposed to make a difference)
I hoped turning off autoIndent would stop this, but no dice. I also turned off C++ formatting in the JSON config:
{
"editor.autoIndent": false,
"editor.detectIndentation": false,
"C_Cpp.formatting": "Disabled"
}
There's an extension which shifts text by one character at a time which is a sort of proof-of-concept you could override your tab key with something like that. But it doesn't seem you should need an extension to disable this formatting.
Is editor.autoIndent: false supposed to do what I want, and just broken?
UPDATE: I have also raised this as an issue on the VSCode GitHub
If you set the Tab size to 1, it will do the same job as the extension you referenced.
You can set the Tab or Space size by clicking on the bottom-right corner:
Click on Spaces:4. Then, select Indent Using Spaces or Indent Using Tabs and choose the size 1.
UPDATE:
I found an approach that fully satisfies your requirement (though it's through an extension). After choosing a Tab/Space size of 1, install and load the multi-command extension to perform the 1-space indentation 'four' times. Then, go to your settings.json (File > Preferences > Settings) and add these two commands:
{
"macros": {
"tab4times": [
"tab",
"tab",
"tab",
"tab"
],
"shifttab4times": [
"outdent",
"outdent",
"outdent",
"outdent"
]
}
}
Then, in the keybindings.json file (CTRL+P and then type keybindings.json), modify the CTRL+] and CTRL+[ keys to execute the newly created commands:
[
{
"key": "ctrl+]",
"command": "macros.tab4times",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+[",
"command": "macros.shifttab4times",
"when": "editorTextFocus && !editorReadonly"
}
]
After saving these configurations, go to your text. Now press the CTRL+] and CTRL+[ to see you desired behavior of indentation and outdentation, respectiovely.
Hope it helps.
Related
Is it possible to customize the location of the comment symbol ('#' when using Python) in VSCode?
For example, if my code is:
def my_func():
value = 1
and I press CMD-/ on line 2, I get:
def my_func():
# value = 1
I would prefer to get:
def my_func():
# value = 1
Is there a way to modify the default behavior?
VSCode: 1.67.1
MacOS: 12.3.1
There is no built-in way to do that. You will need an extension. See https://stackoverflow.com/a/59448448/836330 for a previous answer using a different extension. Here is a better answer using an extension I made in the meantime, Find and Transform. But there are restrictions as noted below.
Make this keybinding (in your keybindings.json):
{
"key": "alt+/", // unfortunately, this cannot be ctrl+/
"command": "findInCurrentFile",
"args": {
{
"key": "alt+r",
"command": "findInCurrentFile",
"args": {
"preCommands": [
"cursorEnd",
"cursorHomeSelect",
"cursorHomeSelect"
],
"replace": "${LINE_COMMENT}${TM_CURRENT_LINE}",
"restrictFind": "line" // works on multiple lines, see the demo
},
"when": "editorLangId == python" // if you want to limit it to a language
},
}
You can use whatever keybinding you want, but not Ctrl+/ because then toggle off will not work.
Ctrl+/ will work to toggle off comments if you do not use it in the keybinding above to add comments.
Note: For this to work well you need to disable the following setting (which I have shown disabled for a particular language, python):
"[python]": {
"editor.comments.insertSpace": false
}
That goes into your settings.json.
I already found that the "command": "editor.action.duplicateSelection"
will duplicate the selection right next to it.
I want to duplicate the selected text to a new line. The selection may not be the entire line.
If you are talking about a selection that is less than the entire line, there is no built-in way to duplicate selected text to the next line. It can be done with a macro extension which enables you to run multiple commands at once.
Using the macro extension multi-command try this keybinding (in your keybindings.json):
{
"key": "alt+i", // whatever keybinding you want
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"editor.action.clipboardCopyAction",
"editor.action.insertLineAfter",
"editor.action.clipboardPasteAction",
{ // to add text after the selection
"command": "type", // you could also put this before the paste command
"args": { "text": " myText here after paste " }
}
]
}
}
That will copy the selected text, insert a blank line after it and paste that text there. Demo:
Demo with adding static text to the duplicated text:
Click File > Preferences > Keyboard Shortcuts:
Look for the Copy Line Down keyboard shortcut.
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.
I know there is a shortcut for comment and uncomment code block (SHIFT + ALT + A), but is there a way to quickly select (or even remove without select) block comment without using mouse or keyboard to select and press the delete/backspace button? For example:
/*
This is a large block of code with at least 50 lines of code!
:
:
*/
Is there a keyboard shortcut where I can place my cursor anywhere in the block comment and remove it in just a few keystrokes? Thanks!
You can set a macro to do this pretty easily.
First, use the excellent Select By extension (#rioV8) to select the text between and including the block comment markers /* and */. Put his into your settings:
"selectby.regexes": {
"BlockCommentSelect": {
"backward": "\/\\*",
"forward": "\\*\/",
"forwardInclude": true,
"backwardInclude": true,
"showSelection": true
}
},
You can use that with a keybinding like:
{
"key": "alt+s", // whatever keybinding you wish
"command": "selectby.regex",
"args": ["BlockCommentSelect"],
"when": "editorTextFocus"
},
You could stop here and use your keybinding to select the text and then Shift+Alt+A to toggle off the block comment.
Or you could add the selectby.regex1 to a macro and do it the selection and toggling off in one step. Here using the macro extension multi-command put this into your settings as well as the above selectby.regexes setting:
"multiCommand.commands": [
{
"command": "multiCommand.BlockCommentOff",
"sequence": [
{
"command": "selectby.regex",
"args": ["BlockCommentSelect"]
},
"editor.action.blockComment"
]
},
]
and then a keybinding to trigger that macro (in your keybindings.json):
{
"key": "shift+alt+A", // trigger the macro with whatever keybinding if you wish
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.BlockCommentOff" },
"when": "editorTextFocus && !editorHasSelection"
},
Here I used Shift+Alt+A to trigger the macro. And I used the when clause !editorHasSelection because if you have a selection maybe you want to block comment only that selection (inside another block comment!!).
Demos: (1) Just the first method where selectby selects your text and you manually toggle it off, and then (2) using the macro version to do it in one step.
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: