Vscode: Support quote wrapping in plain text mode

Created on 16 Aug 2017  路  10Comments  路  Source: microsoft/vscode

editor-autoclosing feature-request verified

Most helpful comment

I agree plain text shouldn't be different, because let's suppose you selected some text and then pressed ' or ":

  • If your intention was to replace the selected text with quote marks, but were surprised to see it's surrounded by quotes, then all you need to do is press delete once, you'd do it on reflex and won't even think about it. Plus it's very uncommon for anybody to replace selection with quote marks in the first place.

  • If on the other hand, your intention was to surround it with quotes, but were surprised to see it's replaced, then there's no reflex you can perform to fix the situation. You'd have to undo, then go to the beginning and the end of the selection to add quote marks. Very bad experience, very slow.

All 10 comments

Isn't this doable via keybindings? Example for ' " [ ( {:

{
    "key": "shift+9",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus && editorHasSelection",
    "args": {
        "snippet": "(${1:${TM_SELECTED_TEXT}})"
    }
},
{
    "key": "shift+0",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus && editorHasSelection",
    "args": {
        "snippet": "(${1:${TM_SELECTED_TEXT}})"
    }
},
{
    "key": "[",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus && editorHasSelection",
    "args": {
        "snippet": "[${1:${TM_SELECTED_TEXT}}]"
    }
},
{
    "key": "]",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus && editorHasSelection",
    "args": {
        "snippet": "[${1:${TM_SELECTED_TEXT}}]"
    }
},
{
    "key": "shift+[",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus && editorHasSelection",
    "args": {
        "snippet": "{${1:${TM_SELECTED_TEXT}}}"
    }
},
{
    "key": "shift+]",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus && editorHasSelection",
    "args": {
        "snippet": "{${1:${TM_SELECTED_TEXT}}}"
    }
},
{
    "key": "shift+'",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus && editorHasSelection",
    "args": {
        "snippet": "\"${1:${TM_SELECTED_TEXT}}\""
    }
},
{
    "key": "'",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus && editorHasSelection",
    "args": {
        "snippet": "'${1:${TM_SELECTED_TEXT}}'"
    }
},

It seems to be doable with what's referred to as "Smart Bracket Matching", check the documentation here with provided example for TypeScript:

https://code.visualstudio.com/docs/extensionAPI/language-support#_smart-bracket-matching

This seems to be the way it's implemented in supported languages. I don't see why this could not be the editor default.

```
{
...

"surroundingPairs": [
["{", "}"],
["[", "]"],
["(", ")"],
["<", ">"],
["'", "'"],
[""", """],
["", ""]
]
}

Thank you @usernamehw!

Your snippet works for single quotes. How do I get the same behavior for double quotes?

{
    "key": "'",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus && editorHasSelection",
    "args": {
        "snippet": "'${1:${TM_SELECTED_TEXT}}'"
    }
}

Is there a strong reason why "Plain Text" should behave differently to every other file type/editor?

It's especially confusing because bracket surrounding (with (), {}, []) works in plaintext the way it does in all the other syntaxes. The lack of single and double quotes seems really counterintuitive. Can you provide us with a use case that would make this undesirable behaviour to have on by default?

Edit: ...but in the meantime, @usernamehw's suggestion for keybindings.json works great for me.

for double quote

  {
    "key": "shift+'",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus && editorHasSelection",
    "args": {
      "snippet": "\"${1:${TM_SELECTED_TEXT}}\""
    }
  }

I absolutely agree this behavior should be default like other popular editors suck as Sublime Text and Atom.

I agree plain text shouldn't be different, because let's suppose you selected some text and then pressed ' or ":

  • If your intention was to replace the selected text with quote marks, but were surprised to see it's surrounded by quotes, then all you need to do is press delete once, you'd do it on reflex and won't even think about it. Plus it's very uncommon for anybody to replace selection with quote marks in the first place.

  • If on the other hand, your intention was to surround it with quotes, but were surprised to see it's replaced, then there's no reflex you can perform to fix the situation. You'd have to undo, then go to the beginning and the end of the selection to add quote marks. Very bad experience, very slow.

Is it only just plain text mode where this feature doesn't work? Because for me it didn't work in Markdown either and I had to use @usernamehw's Keyboard shortcuts - which worked just fine!

I think the existing setting editor.autoSurround should have another option all. Right now you can set this to brackets or quotes which makes it work in plaintext for one of them. But never for both.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

curtw picture curtw  路  3Comments

omidgolparvar picture omidgolparvar  路  3Comments

shanalikhan picture shanalikhan  路  3Comments

villiv picture villiv  路  3Comments

philipgiuliani picture philipgiuliani  路  3Comments