editor.action.indentLines does not work in VS Code - visual-studio-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

Related

How can I change the key bindings in VS Code to change the tab key to four spaces?

I'm running the latest version of VS Code and Windows 10. I'm learning Python and would like all of my indentations to be spaces. However, I'd like to use the tab key to enter those spaces.
I've tried many different variations in the keybindings.json file with no luck.
[
{
"key": "tab",
"command": "-tab"
},
{
"key": "tab",
"command": "type",
"args": {"text": " "}
}
]
With the above code, I was just trying out many spaces to check the difference. However, when I save this as the json file, it doesn't give expected behavior. It will indent the full amount, but when I backspace I'd expect it to go back one space. Yet, it backs up to where a standard tab would be.. hitting it again returns to the beginning of the line.
Is it possible for the tab key to simply be used as a macro for four spaces?
You don't need to change it in json files. You can search Tab Size in VSCode Settings and change its value to 4. If this does not work, click the Editor: Detect Indentation link and disable it. This should solve the problem.

VS code autocompletion customization - do not automatically select any suggestion

I would like to customize VScode autocompletion behaviour to suit my liking.
Namely:
When the suggestions list appears, I don't want any suggestion to be selected.
When I press Tab and Shift-Tab, I want to cycle through the suggestions (thus selecting one). Esc should unselect any selected suggestion (and can close the suggestions list, optionally).
When a suggestion is selected, any character should accept that suggestion (so edit my code with the text of that suggestion).
Item 2. is achieved by editing the keybinds.
I haven't found a way to get the behaviour of item 1.
For item 3., a dirty hack could be to exploit the editor.acceptSuggestionOnCommitCharacter setting, and using all characters as commit characters, but I haven't found how to edit which characters are commit characters.
Is there a way to achieve this behaviour using the settings?
If not, is there an extension that provides this behaviour?
Ideally, I would like to avoid coding my own extension, but I could resort to that if no other solution is available.
Note: this question is different from this one, because I do not want to press Return to accept a suggestion (unless i want to accept the suggestion and insert a new line).
edit: I believe this answer can implement item 3 using the dirty hack described above, I just have to copy the same keybind for all possible characters with the same "when" conditions, now I just need to find a way to get item 1. (and ideally find a better way to get item 3.).
edit again: to make my question clearer, I've recorded the desired behaviour from vim, with the keys pressed.
For item 1, you could press the up-arrow key de-select a suggestion.
or you could go to VSC, From Visual Studio, select “Tools” > “Options“.
Select “Text Editor” in the left pane.
Select the language you are using (C#, C++, Basic, etc.).
For C# and Basic, choose “IntelliSense“. ...
For C# and Basic, check the “Show completion list after a character is typed” to disable it.
For the 3rd item, you could just not write any parenthesis, < or >, { or }, [ or ], or < and >.
instead, just write what's inside of these. And the auto completion will put every sign in it's right place.
Hope I helped!
I'm also looking for the same. But the closest I came was to preview the suggestion, which I turned on: Put this in your settings.json in VScode
"editor.suggest.preview": true
But I don't think the actual insert feature is available in VScode yet.
for item 2:
first you will need to disable selecting a suggestion with Enter. for that, change the keybinding for "insertSnippet"
{
"key": "enter",
"command": "insertSnippet",
"when": "editorTextFocus && hasSnippetCompletions && !editorTabMovesFocus && !inSnippetMode"
},
then, to add navigation with Tab and Shift + Tab modify "selectNextSuggestion" and "selectPrevSuggestion"
*make sure that both shortcuts have "when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
{
"key": "down",
"command": "-selectNextSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
{
"key": "shift+tab",
"command": "selectPrevSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
for exiting the snippet selection Esc should alredy be the default key. If that's not the case, modify the "leaveSnippet" keybinding.
{
"key": "escape",
"command": "leaveSnippet",
"when": "editorTextFocus && inSnippetMode"
},
There is a new approach to how suggestions are handled coming in vscode v1.75, see Implement "Suggestion Mode" from Visual Studio. New setting:
Editor > Suggest: Selection Mode
Controls whether a suggestion is selected when the widget shows. Note
that this only applies to automatically triggered suggestions
(Editor: QUick Suggestions and Editor: Suggest on Trigger Characters) and that a suggestion is always selected when explicitly
invoked, e.g. via Ctrl+Space.
Options:
always Always select a suggestion when automatically Intellisense
never Never Always select a suggestion when automatically Intellisense
whenTriggerCharacter Select a suggestion only when triggering Intellisemse from a trigger character
whenQuickSuggestion Select a suggestion only when triggering Intellisense as you type
Demo 1: modes never and always - note that with never there is no selected item in the suggestion box and when I press Enter or Tab a newline is inserted into the code. Option always has a selected suggestion item automatically and Enter selects and inserts that.
Demo2: mode whenTriggerCharacter, note that although suggestions are shown none are selected and I can Enter and Tab and they are inserted. Only when a trigger character like . is entered is a suggestion selected and Enter and Tab will insert that selected suggestion item into the code (depending on your setting regarding using Enter as a suggestion completion).
Demo3: mode whenQuickSuggestion You will get selected suggestions as you type, except for trigger characters. Trigger characters will still show suggestions but none will be selected so you can Enter and Tab.
Note that in all cases above where there are suggestions shown but none are selected the DownArrow and UpArrow keys will scroll and select items in the suggestions.
There is another demo at the link above: https://github.com/microsoft/vscode/issues/139825#issuecomment-1364056148

How to toggle Signature help & intellisense VS Code?

I have VS Code on Win8.1.
I really don't like how the signature box opens up over the surrounding code in the editor. I do like the intellisense box. On the page (link above) it says Ctrl+Shift+Space to manually trigger signature help. But how can I close it? The problem is that it opens up without me triggering it.
I know I can close it by moving the cursor out from the function block. But that is a pain to have to do that all the time. I wish Ctrl+Shift+Space could toggle the box.
I remember when I used Visual Studio. There was a key-short-cut to toggle or to get opacity on the boxes. That was great. Is that possible in VS Code?
It is for JavaScript/React (if that's important)
EDIT:
I found an answer on Reddit that don't give me the good solution Visual Studio has. But it makes the box a bit transparent. But it is always transparent. Here is the solution for the half-way-workaround (cut-and-past from the reddit-post, user: tgreen7):
I found a way to do this in settings. if you go here https://code.visualstudio.com/docs/getstarted/theme-color-reference and search for "editorSuggestWidget.background" that is the setting you are looking to change.
So for example my background was originally the hex color: #21252B
And I changed my settings like so:
"workbench.colorCustomizations": {
"editorSuggestWidget.background": "#21252BAA"
}
and now my suggestion window has opacity. You can adjust the last two digits of the hex number to get your desired transparency (opacity).
escape will close it, and then Ctrl+shift+space to get it back.
If you want a toggle try this with whatever keybinding you want:
{
"key": "ctrl+shift+space", // whatever, even "escape"
"command": "editor.action.triggerParameterHints",
"when": "editorHasSignatureHelpProvider && editorTextFocus"
},
{
"key": "ctrl+shift+space", // use same keybinding as above here fro toggle
"command": "closeParameterHints",
"when": "editorHasSignatureHelpProvider && editorTextFocus && parameterHintsVisible"
},

Visual Studio Code - Indent single line with tabulator-key

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"
}

VSCode: Keyboard shortcuts for modifying user settings

I like the idea of CodeLens, VSCode's plugin that tells you reference counts on all your functions & variables. However, when I'm scanning code, the extra vertical margin added to include the "X references" line annoys me enough that I've disabled it. This is a shame, because it's useful information.
I'd like to be able to control whether CodeLens is active via keyboard shortcut. There seem to be no "editor.action.toggleCodeLens" in the keybindings, though, and trying
{ "key": "ctrl+l", "command": "editor.codeLens = false" }
or
{ "key": "ctrl+l", "command": "editor.codeLens = !editor.codeLens" }
produces the error command 'editor.codeLens = false' not found. Is there a way to do this?
Try this extensions: https://marketplace.visualstudio.com/items?itemName=rebornix.toggle
It allows you to setup custom keybindings to toggle any VSCode setting