skip to Main Content

i want to create a macro for wrapping a selected line of code in if statement in vs code. i stumbled upon this youtube video about macros in vs code so i tried imitating the ‘ifWrap’ in the video but for some reason it just doesn’t seem to work. first download the macro-commander extension. then paste the this code in settings.json file and create the snippet need for it. and that’s it, everything should have worked but it didn’t so here is the code,

"editor.action.clipboardCutAction",
      {
        "command": "editor.action.insertSnippet",
        "args": {
          "name": "iff"
        }
      },
      {
        "command": "type",
        "args": {
          "text": "true"
        }
      },
      "cursorLineEnd",
      "cursorDown",
      "editor.action.clipboardPasteAction"

this is the snippet that it uses(it doesn’t have scope i don’t know why but it is not allowed for some reason),

"iff":{
    "prefix": "iff",
    "body": [
        "if ($1) {",
        "t$0",
        "}"
    ],
    "description": "iff",
}

please help me and thank you.

i tried finding article related to it, tried asking questions to chatgpt, notebooklm, gemini but it didn’t worked.

2

Answers


  1. I think problem is with comma after "description": "iff". This macro is in JSON format, and the last item in JSON should not have a trailling comma. So, just try to remove the last comma 🙂

    Login or Signup to reply.
  2. use variable $TM_SELECTED_TEXT in snippet

    • select text
    • type snippet prefix
    • type tab
    • selected text will be inserted
    "iff":{
        "prefix": "iff",
        "body": [
            "if ($TM_SELECTED_TEXT) {",
            "t$0",
            "}"
        ],
        "description": "iff"
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search