VS Code 快捷方式从 Markdown 和 Latex 中的选定文本中删除标记

发布于 2025-01-10 18:18:43 字数 4287 浏览 5 评论 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/}"
                    }
                }
            ]
        }
    }
}

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.


Edit:

The solution provided by @rioV8 is even better than what I hoped for. Instead of selecting the italic/bold text first, you can simply position the cursor somewhere inside the markup text and press the keybinding to transform it back to normal text.

The corresponding entries in keybindings.json now look like this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城花音 2025-01-17 18:18:43

使用扩展名 选择依据 和命令 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/}" } }
        ]
    }
}

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/}" } }
        ]
    }
}
追风人 2025-01-17 18:18:43

我花了很长时间来解决 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 引用的“选择依据”和“多命令”扩展

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

I spent too long figuring this out for LaTeX. For those who want the keybindings for keybindings.json, here is what I ended with:

{
    "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/}"
          }
        }
      ]
    }
  },

Note1: This relies on the "Select By" and "multi-command" extensions, cited by @rioV8

Note2: ctrl+shift+_ is "undo _". Also, undo underline is "ctrl+y" because "ctrl+shift+U" would not work on my machine. If anyone has insight as to how I could change this, please let me know.

月朦胧 2025-01-17 18:18:43

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

使用我编写的扩展,查找并转换,创建此键绑定(在您的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"

latex 和 markdown 还原样式


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

Here is a pretty simple solution - one keybinding, one extension. You do not need to select anything.

Using an extension I wrote, Find and Transform, make this keybinding (in your 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
  }
}

Other options:

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

latex and markdown revert styling


If you wanted to make individual keybindings for each case, the regex's would be very simple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文