8

When you have the cursor in front of a ], ) or } and you type that character, instead of inserting it vscode just moves past that character, producing ]*cursor here* instead of ]*cursor here*]. Becacuse of this, every time I actually need to insert a closing bracket I need to move to the end of the )))) to type it, instead of just being able to type it directly. So is there a way to disable this behavior(without disabling bracket auto-completion)?

Here is the same question, but for sublime text, and this guy mentions it as a side effect of auto-closing brackets.

alvitawa
  • 394
  • 1
  • 4
  • 12
  • Can you provide an example of what you are referring to? – ifconfig Oct 05 '17 at 16:49
  • @ifconfig 1. Type `)` 2. place the cursor right before the `)` 3. Type `)` again. 4. Result -> `)` with cursor after the `)`. Desired Result -> `))` with cursor in the middle. Don't know how to put it more plainly than that. – alvitawa Oct 05 '17 at 18:49
  • 1
    The answer is (now) [yes](https://stackoverflow.com/a/46370345/95852), you can get the behavior you want. Without any clever keybindings or other tricks. – John Y Sep 25 '19 at 15:44

3 Answers3

9

I received a solution from github of vscode project.
It works for me. Edit your keybindings.json add the text below:

{
"key": "]",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
    "snippet": "]"
}
},
{
"key": "Shift+]",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
    "snippet": "}"
}
},
{
"key": "Shift+0",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
    "snippet": ")"
}
}

Notice: "Shift+0" for en keyboard (, edit it for your keyboard layout.

CL. Wang
  • 158
  • 3
  • 8
  • 3
    I wish I could buy you a beer for this answer! –  Mar 20 '18 at 06:35
  • Great answer that fixes the problem as stated, but it really breaks the autoClosingBrackets feature because now when you touch type something like `() {};`, you end up with `() {};})", which is really not what you want. – robocat Jul 25 '19 at 03:10
  • @robocat - It may not be what *you* want, but I think some people actually are fine with that. Not that they really want extra closing brackets at the end, but some people are actually used to typing a left bracket, then pressing right arrow or End to get past the automatically generated right bracket. It's their workflow and what makes sense to them. Anyway, it may soon be moot because they are working on [more sophisticated bracket overtyping behavior](https://github.com/microsoft/vscode/issues/37315#issuecomment-515200477). – John Y Jul 25 '19 at 22:08
  • @john-y That link is fantastic news, it certainly looks like it will fix this properly. Cheers – robocat Jul 27 '19 at 00:50
4

It is indeed a side effect of the autoClosingBrackets setting of the editor.

If you go to File > Preferences > Settings to open the settings JSON file, you can search for "editor" or "autoClosing" and copy the entry to your user settings if you whish to change/disable it (it's enabled by default), or just copy this to disable it:

// Controls if the editor should automatically close brackets after opening them
"editor.autoClosingBrackets": false,

More on VS Code settings, as well as a list of the default settings can be found here: https://code.visualstudio.com/docs/getstarted/settings

If you disable this setting:

  • Typing a bracket or quote won't automatically add a matching, closing bracket or quote.
  • Typing a (closing) bracket before an existing one won't cause it to be "absorbed".
  • You'll have to type each closing bracket or quote yourself.
  • You won't be able to automatically enclose selected text with brackets or quotes by selecting it and typing just one bracket/quote. With this option disabled, the selected text will be replace with whatever you type.
z0ppa
  • 41
  • 2
  • 1
    I hate the last functionality you mentioned - the one about enclosing selected text instead of replacing it with typed bracket character. It's a shame you need to disable these features all at once, or none at all. – Tarec Jun 01 '18 at 16:43
0

TL;DR: currently it is not possible to disable this annoying feature.

I'v asked the same question here.

There is an open issue in their repo.

niryo
  • 1,275
  • 4
  • 15
  • 26