Visual Studio Code - Indent single line with tabulator-key - visual-studio-code

I would like to know if its possible to indent a single line with the tab-key without deleting the marked text.
In the first part of the GIF you see Visual Studio Code and in the second part Atom. Atom shows the desired behaviour.
Thus far it is possible to indent multiple lines this way in VS Code, it also works with backtab, but not with tab and a single line.
Is this a bug or normal behavior??
My Setup:
Visual Studio Code: Version 1.25.1 (MacOS 10.13.6 High Sierra)
Visual Studio Code: Version 1.25.1 (Ubuntu 18.04 LTS)

You could use this default keybinding:
{
"key": "ctrl+]",
"command": "editor.action.indentLines",
"when": "editorTextFocus && !editorReadonly"
}
to tab single or multilines. If you want that bound to tab you could modify it to:
{
"key": "tab",
"command": "editor.action.indentLines",
"when": "editorHasSelection && editorTextFocus && !editorReadonly"
}
I added the editorHasSelection clause so it operates only when something is selected on your line, but then you would lose the normal simple tab behavior (that you don't like).

From my understanding, this is the expected behavior. To indent a single line, you'd need to either:
place cursor at beginning of the line and then tab
select the entire line (Mac: Command+i, Windows/Linux: Ctrl+i) and then tab
use the indent line command, which can be done with the words selected as shown in your GIF (Mac: Command+], Windows/Linux: Ctrl+])
There may be an extension available that gives you your desired behavior, though.

Just adding another flavor here:
In case you want tab to work like shift-tab (where you don't have to highlight anything), AND if you're using tab as the key to accept autocomplete suggestions, use this setting:
{
"key": "tab",
"command": "editor.action.indentLines",
"when": "!suggestWidgetVisible && editorTextFocus && !editorReadonly"
}

Related

How to make backspace behave as backspace in Visual Studio Code

In Visual Studio Code I indent using two manually inserted blanks. When I press backspace this deletes two blanks at the same time.
How can I turn off this behavior? I want a backspace to "eat up" exactly ONE blank, especially if the blank is a blank and not a tab.
The question is just the opposite to Deleting tabs when using tabs as spaces.
with extension multi-command you can create a keybinding that does what you want
add to keybindings.json (at the end) this will overrule the default backspace behavior
{
"key": "backspace",
"command": "extension.multiCommand.execute",
"when": "editorTextFocus && !editorHasSelection",
"args": {
"sequence": [
"cursorLeft",
"deleteRight"
]
}
}
Edit
Added && !editorHasSelection to get the default behavior when there is a selection

VSCode how to know when editor is not split?

Until now, I used cmd+2 to split the editor on Visual Studio Code and then cmd+1 and cmd+2 to move between the split editors. This shortcut stopped working for some reason, and I can't set it on the keyboard shortcut menu either.
I checked with another keyboard, so this is not an hardware issue.
Anyhow, I tried to set new keybindings to split the screen and move between the editors, like this:
[
{
"key": "cmd+shift+]",
"command": "workbench.action.navigateRight",
"when": ""
},
{
"key": "cmd+shift+]",
"command": "workbench.action.splitEditorRight",
"when": ""
},
{
"key": "cmd+shift+[",
"command": "workbench.action.navigateLeft",
},
]
The only problem, I couldn't find what I need to write in the when option.
I want my shortcut to split the editor only if the editor is not split yet, and to move between the editors when it is.
How can I achieve that?
The multipleEditorGroups when clause context returns true when there's more than one editor group open.
You don't need to put a "when" for neither of them. Just checked vscode defaults and they don't have any.

VS Code - Key Bindings - my own Block comment

I am using VS Code Version: 1.40.0.
for quicken up my development I would need to set my own keybinding for block comment when I am in .phtml file.
I managed to get into keybindings.json, put this inside:
{
"key": "shift+alt+q",
"command": "editor.action.blockComment",
"blockComment": [ "{*<!--", "-->*}" ],
"when": "editorTextFocus && !editorReadonly && resourceExtname == .phtml"
}
I got the part
"blockComment": [ "{*<!--", "-->*}" ],
from here How to customize comment block characters in visual studio code?.
It might be a complete trash. I just tried. It doesn't work, of course.
Optimal solution:
Even better would be, if the default key parameter would stay the same (shift+alt+a) for toggle block comment and in .phtml files i would get my desired result ("{*<!-- -->*}").
If I think about it, there is default block comments for .css, .html etc, so there must be a way to put my condition somewhere, rigth?
I would be very glad for any help. Thanks in advance
As far as I know the best would be to write your own Language Extension plugin for .phtml files and set the desired comment pairs in its configuration.
Not a real solution but until then, here's my supper ugly workaround (use at your own risk):
Put this in your keybindings.json
{
"key": "ctrl+numpad_divide",
"command": "editor.action.insertSnippet",
"args": {
"snippet": "${TM_SELECTED_TEXT/^(\\s*)({\\*<!-- (.*) -->\\*})?(.*)/$1${3:-{*<!-- }$4${4:+ -->*\\}}/s}"
},
"when": "editorTextFocus && editorHasSelection && !editorReadonly && resourceExtname =~ /phtml?$/"
}
This way ctrl + / can be used for comment/uncomment (toggle) the selected code as you described. Of course you can set the key binding to something else like alt + / to keep the default block comment behaviour.
Tested with Visual Studio Code v1.50.1

When clause to map <enter> to stage a file in VSCode

I'm trying to switch to VSCode and one of the things that's bothering me is that I can't hit enter to stage one or more selected files. What is the correct when clause to use?
If you have the file selected in the Source Control: Changes panel this seems to work:
{
"key": "enter",
"command": "git.stage",
"when": "sideBarFocus && activeViewlet == 'workbench.view.scm'"
}
from vscode when clauses: active views/panels. This assumes the source control panel and a file therein has focus.

editor.action.indentLines does not work in VS Code

According to the default keyboard shortcuts documentation there is this shortcut:
{
"key": "cmd+]",
"command": "editor.action.indentLines",
"when": "editorTextFocus && !editorReadonly"
}
However it does not indent when I use it and adding a tab character instead.
I assume it is supposed to reindent the line according to the indentation rules (I use 4 spaces) - is that correct?
Thank you for help in advance!
Yes, that command will respect whatever indentation settings you have enabled for the current document. Try looking in the status bar for the current indentation setting:
Spaces:
Tabs:
Click on the spaces/tabs item to change the indentation setting for the file.
If you are still seeing unexpected behavior, please open a bug