用于从 Markdown 和 Latex 中的选定文本中删除标记的 VS 代码快捷方式

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

我使用 Visual Studio 代码来编辑 Markdown 和 Latex 文件。 我将以下条目添加到我的 keybindings.json 文件中,以使所选文本为斜体或粗体:

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

现在我的问题是,是否可以创建一个片段并为其分配一个键绑定,恢复这些操作,即将选定的斜体或粗体文本转换为普通文本。

我研究过将正则表达式合并到 VSCode 片段中,这些片段在 Markdown 的情况下去掉星号,或者在 Latex 的情况下将所有内容保留在花括号内,但找不到任何可转移到的方法上面的用例。


编辑:

@rioV8 提供的解决方案比我希望的还要好。您无需先选择 italic/bold 文本,只需将光标定位在标记文本内的某处,然后按快捷键将其转换回普通文本即可。

keybindings.json 中的相应条目现在如下所示:

{
    // Revert Markdown Bold Text
    {
        "key": "cmd+shift+b",
        "command": "extension.multiCommand.execute",
        "when": "editorLangId != 'latex'",
        "args": {
            "sequence": [
                {
                    "command": "selectby.regex",
                    "args": {
                        "surround": "\*\*.*?\*\*"
                    }
                },
                {
                    "command": "editor.action.insertSnippet",
                    "args": {
                        "snippet": "${TM_SELECTED_TEXT/\*\*(.*?)\*\*//}"
                    }
                }
            ]
        }
    },
    // Revert Latex Bold Text
    {
        "key": "cmd+shift+b",
        "command": "extension.multiCommand.execute",
        "when": "editorLangId == 'latex'",
        "args": {
            "sequence": [
                {
                    "command": "selectby.regex",
                    "args": {
                        "surround": "\\textbf{.*?}"
                    }
                },
                {
                    "command": "editor.action.insertSnippet",
                    "args": {
                        "snippet": "${TM_SELECTED_TEXT/\\textbf{(.*?)}//}"
                    }
                }
            ]
        }
    },
    // Revert Markdown Italic Text
    {
        "key": "cmd+shift+i",
        "command": "extension.multiCommand.execute",
        "when": "editorLangId != 'latex'",
        "args": {
            "sequence": [
                {
                    "command": "selectby.regex",
                    "args": {
                        "surround": "\*.*?\*"
                    }
                },
                {
                    "command": "editor.action.insertSnippet",
                    "args": {
                        "snippet": "${TM_SELECTED_TEXT/\*(.*?)\*//}"
                    }
                }
            ]
        }
    },
    // Revert Latex Italic Text
    {
        "key": "cmd+shift+i",
        "command": "extension.multiCommand.execute",
        "when": "editorLangId == 'latex'",
        "args": {
            "sequence": [
                {
                    "command": "selectby.regex",
                    "args": {
                        "surround": "\\emph{.*?}"
                    }
                },
                {
                    "command": "editor.action.insertSnippet",
                    "args": {
                        "snippet": "${TM_SELECTED_TEXT/\\emph{(.*?)}//}"
                    }
                }
            ]
        }
    }
}

扩展名 Select By and the command selectby.regex there is the option to create a selection around the cursor 给定一个正则表达式。

使用扩展程序 multi-command,您可以将其与转换所选文本的片段结合使用。

对于Markdown Bold它会像

{
    "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/\*\*(.*?)\*\*//}" } }
        ]
    }
}

我花了太长时间为 LaTeX 解决这个问题。对于那些想要 keybindings.json 的键绑定的人,这是我的结尾:

{
    "key": "ctrl+B",
    "command": "extension.multiCommand.execute",
    "when": "editorTextFocus && editorLangId == 'latex'",
    "args": {
      "sequence": [
        {
          "command": "selectby.regex",
          "args": { "surround": "[a-zA-Z0-9]+" }
        },
        {
          "command": "editor.action.insertSnippet",
          "args": { "snippet": "\textbf{${TM_SELECTED_TEXT}[=10=]}" }
        }
      ]
    }
  },
  {
    "key": "ctrl+shift+B",
    "command": "extension.multiCommand.execute",
    "when": "editorTextFocus && editorLangId == 'latex'",
    "args": {
      "sequence": [
        {
          "command": "selectby.regex",
          "args": { "surround": "[^ \na-zA-Z0-9]textbf{.*?}" }
        },
        {
          "command": "editor.action.insertSnippet",
          "args": {
            "snippet": "[=10=]${TM_SELECTED_TEXT/[^ \na-zA-Z0-9]textbf{(.*?)}//}"
          }
        }
      ]
    }
  },


  {
    "key": "ctrl+I",
    "command": "extension.multiCommand.execute",
    "when": "editorTextFocus && editorLangId == 'latex'",
    "args": {
      "sequence": [
        {
          "command": "selectby.regex",
          "args": { "surround": "[a-zA-Z0-9]+" }
        },
        {
          "command": "editor.action.insertSnippet",
          "args": { "snippet": "\textit{${TM_SELECTED_TEXT}[=10=]}" }
        }
      ]
    }
  },
  {
    "key": "ctrl+shift+I",
    "command": "extension.multiCommand.execute",
    "when": "editorTextFocus && editorLangId == 'latex'",
    "args": {
      "sequence": [
        {
          "command": "selectby.regex",
          "args": { "surround": "[^ \na-zA-Z0-9]textit{.*?}" }
        },
        {
          "command": "editor.action.insertSnippet",
          "args": {
            "snippet": "[=10=]${TM_SELECTED_TEXT/[^ \na-zA-Z0-9]textit{(.*?)}//}"
          }
        }
      ]
    }
  },


  {
    "key": "ctrl+U",
    "command": "extension.multiCommand.execute",
    "when": "editorTextFocus && editorLangId == 'latex'",
    "args": {
      "sequence": [
        {
          "command": "selectby.regex",
          "args": { "surround": "[a-zA-Z0-9]+" }
        },
        {
          "command": "editor.action.insertSnippet",
          "args": { "snippet": "\underline{${TM_SELECTED_TEXT}[=10=]}" }
        }
      ]
    }
  },
  {
    "key": "ctrl+Y",
    "command": "extension.multiCommand.execute",
    "when": "editorTextFocus && editorLangId == 'latex'",
    "args": {
      "sequence": [
        {
          "command": "selectby.regex",
          "args": { "surround": "[^ \na-zA-Z0-9]underline{.*?}" }
        },
        {
          "command": "editor.action.insertSnippet",
          "args": {
            "snippet": "[=10=]${TM_SELECTED_TEXT/[^ \na-zA-Z0-9]underline{(.*?)}//}"
          }
        }
      ]
    }
  },

注意 1:这依赖于“Select By”和“multi-command”扩展,引用自@rioV8

注2:ctrl+shift+_ 是“撤消_”。另外,撤消下划线是“ctrl+y”,因为“ctrl+shift+U”在我的机器上不起作用。如果有人知道我该如何改变它,请告诉我。

这是一个非常简单的解决方案 - 一个键绑定,一个扩展。您不需要select任何东西。

使用我编写的扩展程序 Find and Transform,进行此键绑定(在您的 keybindings.json 中):

{
  "key": "alt+r",                    // whatever keybinding you want
  "command": "findInCurrentFile",
  "args": {
    
    "find": "(\*\*|\\textbf{|\*|\\emph{)(.+?)([*}]+)",
    "replace": "",                // replace find match with capture group 2
    "isRegex": true,
    "restrictFind": "nextSelect"    // replace next occurrence and select the result 

    // demo below uses "nextSelect" just to make it easy to see
  }
}

其他选项:

    "restrictFind": "once"        // replace next occurrence in that line 
    "restrictFind": "next"        // replace next occurrence anywhere in the document
    "restrictFind": "line"        // replace All occurrences on the current line
    "restrictFind": "document"    // replace all occurrences in the entire document
    // and more, see the README on "restrictFind"


如果您想为每种情况制作单独的键绑定,正则表达式将非常简单。