1
votes

I am currently trying to experiment with using VSCode at the moment, and i cannot figure out how to properly define macros and bind them to specific keybinds.

I am used to using Sublime text, and i have defined a few macros that help me type much quicker and make less mistakes

The macros that i would like to get are the following:

  1. alt+shift+q : which types \(\) and set the cursor in the middle (between the first ( and the second \).
  2. alt+shift+s : which types \[\] and set the cursor in the middle (between the first [ and the second \). Additionally, if it is possible, i would like it to also toggle a math preview function using latex-workshop.toggleMathPreviewPanel.
  3. alt+shift+a : which types the following
\begin{align*}
    \item 
\end{align*}

The \item is preceded by a tab, and it sets the cursor after the \item

I have managed to get the first macro by doing as follows

  • Installing the macros package by geddski.
  • Creating the following macro, and inserting it in the settings.json:
"macros": {
        "latex_inline_math": [
            {
                "command": "type",
                "args": {
                    "text": "\\(\\)"
                }
            },
            "cursorLeft",
            "cursorLeft"
        ],
}
  • Binding it to the keybind by inserting in the keybindings.json like this:
{ // to get \(\)
    "key": "alt+shift+q",
    "command": "macros.latex_inline_math"
},

But i cannot figure out how to get the macros 2. and 3.

Also, if there is a better way to write the macro that i wrote, please let me know

1

1 Answers

3
votes

You can define the following 3 keybindings

  {
    "key": "shift+alt+q",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus",
    "args": { "snippet": "\\($0\\)" }
  },
  {
    "key": "shift+alt+s",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus",
    "args": { "snippet": "\\[$0\\]" }
  },
  {
    "key": "shift+alt+a",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus",
    "args": { "snippet": "\\begin{align*}\n\t\\item$0\n\\end{align*}" }
  }

If you also want a preview you can use the extension multi-command

Define this key binding

  
{
    "key": "shift+alt+s",
    "command": "extension.multiCommand.execute",
    "args": { 
        "sequence": [
            "latex-workshop.toggleMathPreviewPanel",
            { "command": "editor.action.insertSnippet", "args": { "snippet": "\\[$0\\]" } }
        ]
    }
}