我使用 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/}"
}
}
]
}
}
}