英文:
How to run a shell script with the current line or block of code as argument in Visual Studio Code?
问题
有没有一种方法可以实现类似于“Python:在Python终端中运行选择/行”但其中当前行代码或代码块作为参数传递给可配置的Shell脚本?
英文:
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?
答案1
得分: 1
你可以使用扩展Command Variable将所选文本传递给任务命令
{
  "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}"
      }
    }
  ]
}
如有需要,您可以向${selectedText}变量添加一些参数。
英文:
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.
答案2
得分: 1
你可以直接在发送命令到终端的键绑定中使用变量 ${selectedText}:
{
  "key": "alt+t", // 选择一个你想要的按键绑定
  "command": "workbench.action.terminal.sendSequence",
  "args": {
    "text": "echo '${selectedText}'\u000D"
  }
}
\u000D 表示回车键,因此命令会立即运行。
或者尝试以下键绑定,首先选择当前行并将其发送到终端:
{
  "key": "alt+t",
  "command": "runCommands",
  "args": {
    "commands": [
      "cursorHome",
      "cursorEndSelect",
      {
        "command": "workbench.action.terminal.sendSequence",
        "args": {
          "text": "echo '${selectedText}'\u000D"
        }
      }
    ]
  }
}
或者在任务中使用:
"tasks": [
  {
    "label": "echoMe",
    "type": "shell",
    "command": "echo ${selectedText}",
    "args": []
  }
]
英文:
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": [
      
    ]
  }
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论