Post

Create VS Code Hotkeys to bold and italic text

I am using VS Code to manage and write the LaTeX/markdown files. Every time I want to bold/italic something , I have to manually type commands like \textbf and brackets, which is extremely laborious and meaningless.

According to the offical documentation, we can define the key bindings by ourselves.

Basically, you need to modify the ‘keybinding.json’ in VS Code:

You can also open the keybindings.json file from the Command Palette (⇧⌘P) with the Preferences: Open Keyboard Shortcuts (JSON) command.

For instance, you can create a new hotkey with the following prompts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    {
        "key": "cmd+shift+B",
        "command": "editor.action.insertSnippet",
        "when": "editorLangId == latex && editorTextFocus",
        "args": {
            "snippet": "\\textbf{${TM_SELECTED_TEXT}$0}"
        }
    },
    {
        "key": "cmd+shift+I",
        "command": "editor.action.insertSnippet",
        "when": "editorLangId == latex && editorTextFocus", 
        "args": {
            "snippet": "\\textit{${TM_SELECTED_TEXT}$0}"
        }
    }

Then it will work.

Same methods can be applied to Markdown, you can have a try, remember to modify the snippets.

P.S. If you are using Windows/Linux, remember to replace cmd with ctrl.

This post is licensed under CC BY 4.0 by the author.