简体   繁体   English

如何使用当前行或代码块作为 Visual Studio Code 中的参数运行 shell 脚本?

[英]How to run a shell script with the current line or block of code as argument in Visual Studio Code?

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?有没有办法实现类似于“Python:在 Python 终端中运行选择/行”但当前代码行或代码块作为参数传递给可配置的 shell 脚本的方法?

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.如果需要,您可以将一些 arguments 添加到${selectedText}变量。

You can directly use the variable ${selectedText} in a keybinding that sends a command to the terminal:您可以在向终端发送命令的键绑定中直接使用变量${selectedText}

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

is a return so the command runs immediately.是一个return ,所以命令立即运行。

Or try this keybinding to select the current line first and send that to the terminal:或者首先尝试将此键绑定到当前行的 select 并将其发送到终端:

{
  "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": [
      
    ]
  }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM