8

I would like to customize VScode autocompletion behaviour to suit my liking.

Namely:

  1. When the suggestions list appears, I don't want any suggestion to be selected.
  2. 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).
  3. 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. desired behaviour as experienced in vim

Mark
  • 143,421
  • 24
  • 428
  • 436
maahl
  • 547
  • 3
  • 17
  • As an addition to item 3, is there any way to accept the suggestion *and* print the typed character? I tried `"command": "acceptSelectedSuggestion && type"` and an additional parameter `"args": {"text":"/"}` for the type command, but that command isn't being accepted. Both `acceptSelectedSuggestion` and `type` commands work correctly individually. – Anchith Acharya Oct 02 '20 at 09:01

4 Answers4

1

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!

Noor
  • 9
  • 3
1

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.

pltc
  • 5,836
  • 1
  • 13
  • 31
Pawan
  • 93
  • 1
  • 2
  • 10
1

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:

  1. always Always select a suggestion when automatically Intellisense
  2. never Never Always select a suggestion when automatically Intellisense
  3. whenTriggerCharacter Select a suggestion only when triggering Intellisemse from a trigger character
  4. 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.

suggestionMode: whenTriggerCharacter

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).

suggestion modes: always and never

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

Mark
  • 143,421
  • 24
  • 428
  • 436
0

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