Home > Mobile >  VS Code Shortcut to remove Markup from selected text in Markdown and Latex
VS Code Shortcut to remove Markup from selected text in Markdown and Latex

Time:03-02

I use Visual Studio Code to edit Markdown and Latex files. I added the following entries to my keybindings.json file to make the selected text either italic or bold:

    // Markdown Bold Text when Editor has Selection
    {
        "key": "cmd b",
        "command": "editor.action.insertSnippet",
        "when": "editorHasSelection && editorLangId != 'latex'",
        "args": {
            "snippet": "**${TM_SELECTED_TEXT}**"
        }
    },
    // Latex Bold Text when Editor has Selection
    {
        "key": "cmd b",
        "command": "editor.action.insertSnippet",
        "when": "editorHasSelection && editorLangId == 'latex'",
        "args": {
            "snippet": "\\textbf{${TM_SELECTED_TEXT}}"
        }
    },
    // Markdown Italic Text when Editor has Selection
    {
        "key": "cmd i",
        "command": "editor.action.insertSnippet",
        "when": "editorHasSelection && editorLangId != 'latex'",
        "args": {
            "snippet": "*${TM_SELECTED_TEXT}*"
        }
    },
    // Latex Italic Text when Editor has Selection
    {
        "key": "cmd i",
        "command": "editor.action.insertSnippet",
        "when": "editorHasSelection && editorLangId == 'latex'",
        "args": {
            "snippet": "\\emph{${TM_SELECTED_TEXT}}"
        }
    }

Now my question is if it is possible to create a snippet and assign a keybinding to it that reverts these operations, i.e. transform selected italic or bold text to normal text.

I have looked into incorporating regular expressions into VSCode snippets that strip off the stars in case of Markdown or keep everything inside the curly braces in case of Latex, but could not find any approaches that are transferable to the use case above.

CodePudding user response:

with the extension Select By and the command selectby.regex there is the option to create a selection around the cursor given a regular expression.

And with the extension multi-command you can combine this with a snippet that transforms the selected text.

For Markdown Bold it would be something like

{
    "key": "cmd shift b",
    "command": "extension.multiCommand.execute",
    "when": "editorHasSelection && editorLangId != 'latex'",
    "args": { 
        "sequence": [
            { "command": "selectby.regex",
              "args": { "surround": "\\*\\*.*?\\*\\*" } },
            { "command": "editor.action.insertSnippet",
              "args": { "snippet": "${TM_SELECTED_TEXT/\\*\\*(.*?)\\*\\*/$1/}" } }
        ]
    }
}
  • Related