pythonvisual-studio-codevscode-snippets

How to create a shortcut to print a variable (vscode)


How can I create a custom shortcut for generating code that will print a selected variable in vscode?

[1] arr = [1,2,3,4]  # I press double left mouse button on 'arr'
[2] print(arr)       # Then I press <magic shortcut> (Ctrl+p for example)
                     # And vscode generate [2] row automatically

You can provide your method for fast debugging by print().


Solution

  • If, instead of selecting the variable, you just put the cursor at the end of the line, you can do this with a simple keybinding that inserts a snippet and no need for a macro. Keybinding:

    {
      "key": "alt+w",
      "command": "editor.action.insertSnippet",
      "args": {
                  // works with cursor end of line, no selection
                  // output: print(arr)
        "snippet": "\n${TM_CURRENT_LINE/(\\s*)(\\w*)\\b.*/print($2)/}"
      }
    },
    

    If you want this output print(“arr”: arr), use this keybinding:

    {
      "key": "alt+w",
      "command": "editor.action.insertSnippet",
      "args": {
                  // works with cursor end of line, no selection
                  // output: print(“arr”: arr)
        "snippet": "\n${TM_CURRENT_LINE/(\\s*)(\\w*)\\b.*/print(\"$2\": $2)/}"
      }
    },
    

    For these simpler versions, the variable must be the first word in the line.


    Older answer:

    Unfortunately, this seems difficult to do with a simple snippet. A new snippet would be inserted where the cursor is - and under your scenario that would be on your chosen variable - and then the rest of that first line is still there after the snippet.

    It is relatively easy to do with a macro extension which allows you to perform multiple commands, like multi-command or another.

    After installing the extension, in your settings:

      "multiCommand.commands": [
    
       {
         "command": "multiCommand.printVariable",
    
         "sequence": [
           "editor.action.clipboardCopyAction",
           "editor.action.insertLineAfter",
           {
             "command": "type",
             "args": {
               "text": "print("
             }
           },
           "editor.action.clipboardPasteAction",
           {
             "command": "type",
             "args": {
               "text": ")"
             }
           },
         ]
       }
    },
    

    and then set up some keybinding in keybindings.json:

    {
      "key": "alt+q",
      "command": "extension.multiCommand.execute",
      "args": { "command": "multiCommand.printVariable" },
    
      // use the following if you wish to limit the command to python files
      "when": "resourceExtname == .py"
    },
    

    demo of macro snippet

    As the demo gif shows, the selected text can be anywhere on the line and if there is code on the line immediately below the print() statement will be inserted where you expect.

    Caution: This will save your selected variable to the clipboard so that will be overwritten.


    If your variable is always at the beginning of the line and selected, you can use the simpler macro:

    "multiCommand.commands": [
    
     {
       "command": "multiCommand.printVariable",
       "sequence": [
         {
          "command": "editor.action.insertSnippet",
          "args": {
                     // selected variable is at beginning of line
              "snippet": "${TM_CURRENT_LINE}\nprint(${TM_SELECTED_TEXT})"
            }
          },
          "cursorEndSelect",    // select to end and delete
          "editor.action.clipboardCutAction"
        ]
      }
    ]