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