How can I change the key bindings in VS Code to change the tab key to four spaces? - visual-studio-code

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.

Related

How to hide options menu when cursor is inside single or double quotes in sublimetext

I am trying to use the autofilename plugin, but for some reason it replaces me or incorrectly corrects the file path, I think I know that it is because of the options that automatically appear as references when the cursor is inside single or double quotes by example:
As you can see, several options that do not exist such as directory or file names are shown, those names are mostly names that exist in class and id, among other things.
So how can it be avoided that only when the cursor is inside single or double quotes that these options do not appear.
As I mentioned I am trying to use the autofilename plugin but it automatically deletes or corrects me wrong data, I am using sublime text 4113
Probably the easiest way to do this is by assigning a custom key mapping to the filename autocomplete function. First, select Preferences → Key Bindings and add the following, ensuring that they are inside the outer [ ] characters (the file has to be valid JSON):
{
"keys": ["ctrl+super+alt+a"],
"command": "afn_show_filenames",
"context":
[
{
"key": "afn_use_keybinding",
"operator": "equal",
"operand": true
}
]
},
You can change the key combo to whatever you wish, just make sure it's not overwriting another Sublime or operating system key binding.
Next, open Preferences → Package Settings → AutoFileName → Settings—User and add the following line:
"afn_use_keybinding": true,
Now, to open the filename autocomplete, simply press CtrlWinAltA (on Windows and Linux) or Ctrl⌘AltA (on OSX/macOS).

VSCode keyboard shortcut to navigate from search bar to highlighted selection

I'm constantly searching code within a file in VSCode.
Is there a keyboard shortcut to navigate from the search bar to the highlighted selection in the editor? That would make things much more efficient for me.
I know I can copy a highlighted selection from the editor to the search bar using CMD + F.
If you are talking about moving from the find widget to the first match, it looks like you have this option, assuming you don't want to just close the find widget with escape:
the workbench.action.focusActiveEditorGroup command is unbound you could use that. But you have to hit escape twice to unselect the find match if you don't want it to remain selected.
That's a pain though so you might a macro that does it all at once. Like (in settings):
"multiCommand.commands": [
{
"command": "multiCommand.focusEditorFromFind",
"sequence": [
"workbench.action.focusActiveEditorGroup",
"cancelSelection",
"cancelSelection",
]
}
]
and keybindings.json:
{
"key": "shift+e",
"command": "multiCommand.focusEditorFromFind",
"when": "editorFocus && findWidgetVisible"
}
So after typing your find term, just Enter until you get to the particular find match you want to switch focus to, and the Shift+E or whatever keybinding you go with above.
Maybe I am just missing it but it seems odd there isn't an easier way to toggle focus from the find widget.

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 Create Custom Key Binded Snippets in VS Code

I am a huge Sublime Text user, and learned ways to improve my productivity using customizations in Sublime text. But as VScode is becoming popular day by day, wanted to check if there is any way which I can bind the shortcut keys to the custom actions.
For example, I select a word ABC in any file in VSCode and hit CTRL+B, and it places my own defined values around it like it should become
<b>ABC</b>
I had created the following snippet in Sublime Text, which when I wrote in Visual Studio Code - keybindings.json nothing worked.
{
"keys": [
"ctrl+b"
],
"command": "insert_snippet",
"args": {
"contents": "<b>${0:$SELECTION}</b>"
}
}
This will work in your keybindings.json:
{
"key": "ctrl+b",
"command": "editor.action.insertSnippet",
"when": "resourceExtname == .html", // this is optional
"args": {
"snippet": "<b>${TM_SELECTED_TEXT}</b>"
}
},
The optional when clause is if you want to limit the snippet's operation to .html files.
More general though is to use the emmet command which is built-in: Emmet: Wrap with Abbreviation in the command palette. Select your text, open the command palette, find that command and trigger it - type b or whatever your element is and it will wrap the selected text with the opening and closing elements.
[Note that there is a command workbench.action.toggleSidebarVisibility already bound to Ctrl-B, but the snippet above version seems to take precedence - meaning you lose the toggleSidebarVisibility keybinding functionality - that may be acceptable to you?]

Visual Studio Code Surround With

I can't find any way to surround a selection with something in VS Code.
For example doing something like that : text => "text" just by selecting the word text and typing key "
Another example with the following text :
mon
tue
wed
thu
fri
sat
sun
By selecting all of theses words :
mon|
tue|
wed|
thu|
fri|
sat|
sun|
and typing " I would like to perform something like this :
"mon"
"tue"
"wed"
"thu"
"fri"
"sat"
"sun"
Selecting some text and pressing " already works in VSCode to surround a single item, and works for multi-line selections as well.
NOTE: this is language dependent. The language syntax must define opening and closing braces, e.g. quotes, braces, etc. So this will not work in a "plaintext" file, for example. Change your language mode with CTRL+SHIFT+P and type Change Language Mode ENTER and select something like JavaScript where this is supported.
What you are after though is not really that efficient like that. Your best bet is to use multi-cursors.
Place the cursor at the start of the first line, press CTRL+ALT+DOWN to add another cursor below on the next line. Keep doing that until you have a cursor in front of all your words.
Then just type " then END then " and all your lines are surrounded by quotes.
NB: To check if you have a key bound, and what it is, you can always press CTRL+SHIFT+P and type Add Cursor Below and if there's a keybinding it will show to the right of that text.
In VS Code hold Command + Shift + P
then write:
"> Preferences: Open Keyboard Shortcuts (JSON)"
In this area that you are allowed to modify, paste this inside the brackets:
{
"key": "ctrl+p",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "\"${TM_SELECTED_TEXT}\""
}
}
** note that in this example the key is set to Ctrl + p, you can change the key to whatever you prefer
Maybe you can try this extension, you can write your own custom wrappers:
https://marketplace.visualstudio.com/items?itemName=yatki.vscode-surround
A simple yet powerful extension to add wrapper templates around your code blocks.
Features
Supports multi selections
Fully customizable
Custom wrapper functions
You can assign shortcuts for each wrapper function separately
Nicely formated
Demo 1: Choosing wrapper function from quick pick menu
Demo 2: Wrapping multi selections
Using Yuri Aps' suggestion, I added the following JSON to keybindings.json. This provides the functionality Ronan Lamour requested for any file type, and without requiring an extension. It works for either single or multiple selections when using either single or double quotes. Coming from Sublime, this is helpful since it reproduces functionality Sublime provides natively.
{
"key": "'",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "'${TM_SELECTED_TEXT}'"
}
},
{
"key": "shift+'",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "\"${TM_SELECTED_TEXT}\""
}
},
I was coming from (neo)vim switching to VS Code, and was using Tim Pope's wonderful "vim-surround" plugin for vim before. I found a port of that plugin for VS Code. It's very useful, and incredibly efficient once you learn the shortcuts, in my opinion!
Links:
Original plugin by Tim Pope for vim
Port of plugin to VS Code
If you use vim or vim bindings in VS Code, please enjoy!
Edit: the VSCodeVim plugin includes the surround functionality automatically, so if you have that plugin installed, you don't really need the vscode-surround plugin.
Update 15-02-2022:
VS Code has introduced Surround with snippets for JS/TS natively.
It may not be totally related with the question but it may help someone who landed in this question with the intent of "surround with" in vs code.
This extension also exists if you want custom surround with text.
https://marketplace.visualstudio.com/items?itemName=sifue.surrounding.
I just installed it and got it working perfectly
Select the word you want to surround it with and enter Ctrl + Alt + T. Then just key in whatever key you want to surround it with.
A more generic solution: in keybindings.json:
{
"key": "alt+m",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "$1${TM_SELECTED_TEXT}$1$0"
}
}
Whatever you type after triggering the keybinding will be added to both ends of all selections.
Just tab to the end of the word(s) when you are done and, if you had multiple cursors Esc to remove extra cursors leaving just one.
Since GitHub supports math in Markdown now, I need to wrap my formulas with dollar signs:
$E = mc^2$
When I select a formula and press dollar sign $ on my keyboard I get my formula wrapped automatically. Here is one way to achieve it:
Open Keyboard Shortcuts menu:
Press on Open Keyboards Shortcuts (JSON) button:
In shortucts.json file, which opens, paste this snippet:
{
"key": "shift+4",
"command": "editor.action.insertSnippet",
"when": "editorHasSelection",
"args": {
"snippet": "$${TM_SELECTED_TEXT}$"
}
}