visual-studio-codevscode-keybinding

How to not surround text if I press `*`


I have the setting "editor.autoSurround": "languageDefined", in vscode.

I want to replace selected text instead of surrounding if I press *.

how can I do that?

I have tried:

{
    "key": "*",
    "command": "extension.multiCommand.execute",
    "args": {
        "sequence": [
        "editor.action.clipboardCutAction",  // Cut (delete) the selected text
        {
            "command": "type",
            "args": { "text": "*" }             // Insert the asterisk `*`
        }
        ]
    },
    "when": "editorTextFocus && editorHasSelection"   // Ensure this only happens when text is selected
}

But it is not working.


Solution

  • It seems it is quite difficult to override the autoSurround options of a language. Here is one way.

    Download and install this extension I wrote: Custom Language Properties.

    It allows you to change the line and block comment characters for a language but unfortunately vscode does not support modifying the autoSurround properties of a language via an extension.

    Run this command from the extension from an open markdown file:
    Custom Language Properties: Show language configuration for current editor

    It will open the language-configuration.json file that is built-in to vscode for markdown files.

    In that file you will see a "surroundingPairs" key and one of its values is

    [ "*", "*" ]

    You can comment that out, save the file and reload vscode. And then * will no longer surround the selection but will replace the selection as you want.


    Does this keybinding work for you:

    {
      "key": "shift+8",
      "command": "type",
      "args": {
        "text": "*"
      },
      "when": "editorTextFocus && editorHasSelection"
    }
    

    I used shift+8 since on my keyboard that is an *.

    I see from using the Toggle Keyboard Shortcuts Troubleshooting command that if you just use * as your "key" that it looks for a keybinding of shift+8 and can't find one - so just use shift+8 as the "key".