0

我使用 Visual Studio Code 来编辑 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 提供的解决方案比我希望的还要好。无需先选择斜体/粗体文本,您只需将光标定位在标记文本内的某处,然后按下键绑定即可将其转换回普通文本。

现在对应的条目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/\\*\\*(.*?)\\*\\*/$1/}"
                    }
                }
            ]
        }
    },
    // 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{(.*?)}/$1/}"
                    }
                }
            ]
        }
    },
    // 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/\\*(.*?)\\*/$1/}"
                    }
                }
            ]
        }
    },
    // 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{(.*?)}/$1/}"
                    }
                }
            ]
        }
    }
}
4

3 回答 3

1

使用扩展名Select By和命令,可以选择在给定正则表达式的情况下在光标周围selectby.regex创建选择。

通过扩展多命令,您可以将其与转换所选文本的片段结合起来。

对于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/\\*\\*(.*?)\\*\\*/$1/}" } }
        ]
    }
}
于 2022-03-01T17:03:53.130 回答
0

我花了太长时间为 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}$0}" }
        }
      ]
    }
  },
  {
    "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": "$0${TM_SELECTED_TEXT/[^ \na-zA-Z0-9]textbf{(.*?)}/$1/}"
          }
        }
      ]
    }
  },


  {
    "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}$0}" }
        }
      ]
    }
  },
  {
    "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": "$0${TM_SELECTED_TEXT/[^ \na-zA-Z0-9]textit{(.*?)}/$1/}"
          }
        }
      ]
    }
  },


  {
    "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}$0}" }
        }
      ]
    }
  },
  {
    "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": "$0${TM_SELECTED_TEXT/[^ \na-zA-Z0-9]underline{(.*?)}/$1/}"
          }
        }
      ]
    }
  },

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

注意2:ctrl+shift+_ 是“撤消_”。此外,撤消下划线是“ctrl+y”,因为“ctrl+shift+U”在我的机器上不起作用。如果有人对我如何改变这一点有见解,请告诉我。

于 2022-03-02T18:12:48.133 回答
0

这是一个非常简单的解决方案 - 一个键绑定,一个扩展。您无需选择任何内容。

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

{
  "key": "alt+r",                    // whatever keybinding you want
  "command": "findInCurrentFile",
  "args": {
    
    "find": "(\\*\\*|\\\\textbf{|\\*|\\\\emph{)(.+?)([*}]+)",
    "replace": "$2",                // 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"

乳胶和降价恢复样式


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

于 2022-03-02T20:43:05.920 回答