skip to Main Content

Is there a way to achieve something similar to "Python: Run Selection/Line in Python Terminal " but where the current line of code, or block of code, is passed as an argument to a configurable shell script?

2

Answers


  1. You can use the extension Command Variable to pass the selected text to the task command

    {
      "version": "2.0.0",
      "tasks": [
        {
          "label": "echo selected text",
          "type": "shell",
          "command": "echo",
          "args": [ "${input:selectedText}" ],
          "problemMatcher": []
        }
      ],
      "inputs": [
        {
          "id": "selectedText",
          "type": "command",
          "command": "extension.commandvariable.transform",
          "args": {
            "text": "${selectedText}"
          }
        }
      ]
    }
    

    If needed you can add some arguments to the ${selectedText} variable.

    Login or Signup to reply.
  2. You can directly use the variable ${selectedText} in a keybinding that sends a command to the terminal:

    {
      "key": "alt+t",         // whatever you want for a keybinding
      "command": "workbench.action.terminal.sendSequence",
      "args": {
        "text": "echo '${selectedText}'u000D"
      }
    }
    

    u000D is a return so the command runs immediately.

    Or try this keybinding to select the current line first and send that to the terminal:

    {
      "key": "alt+t",
      "command": "runCommands",
      "args": {
        "commands": [
          "cursorHome",
          "cursorEndSelect",
          {
            "command": "workbench.action.terminal.sendSequence",
            "args": {
              "text": "echo '${selectedText}'u000D"
            }
          }
        ]
      }
    }
    

    Or in a task:

    "tasks": [
      {
        "label": "echoMe",
        "type": "shell",
    
        "command": "echo ${selectedText}",
        "args": [
          
        ]
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search