Batch file 如何在VS代码中设置shorcut键以在工作目录中运行.bat文件?

Batch file 如何在VS代码中设置shorcut键以在工作目录中运行.bat文件?,batch-file,visual-studio-code,vscode-settings,vscode-tasks,finite-element-analysis,Batch File,Visual Studio Code,Vscode Settings,Vscode Tasks,Finite Element Analysis,在我的研究中,我使用有限元法,我们使用Abaqus的解算器,它使用Fortran生成的子程序,我使用vs代码编辑它们。所以为了运行我的代码,我制作了一个批处理文件,使这个过程更简单(避免在cmd中键入许多指令) 在这张图片中,您可以看到我工作目录中的文件,如果我可以设置一个快捷方式来执行principal.bat文件,另一个快捷方式直接从vs代码中终止.bat文件,这将节省我很多时间: 非常感谢您的帮助,我已经尝试为此设置task.json文件,但我有点不知所措。您可以添加调试配置,然后使用F5

在我的研究中,我使用有限元法,我们使用Abaqus的解算器,它使用Fortran生成的子程序,我使用vs代码编辑它们。所以为了运行我的代码,我制作了一个批处理文件,使这个过程更简单(避免在cmd中键入许多指令)

在这张图片中,您可以看到我工作目录中的文件,如果我可以设置一个快捷方式来执行principal.bat文件,另一个快捷方式直接从vs代码中终止.bat文件,这将节省我很多时间:
非常感谢您的帮助,我已经尝试为此设置task.json文件,但我有点不知所措。

您可以添加调试配置,然后使用F5启动该配置。
类型
cppvsdbg
在这里并不重要,因为您不能(也不想)调试批处理文件

Shift-F5终止批处理过程

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Start batch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "cmd.exe",
            "args": [
                "/c",
                "principal.bat"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false
        }
    ]
}
您可以使用扩展名向终端键入批处理文件命令

将此添加到您的
设置中。json

  "multiCommand.commands": [
    {
      "command": "multiCommand.callPrincipal",
      "sequence": [
        { "command": "workbench.action.terminal.sendSequence",
          "args": { "text": "principle.bat\u000D" }
        }
      ]
    },
    {
      "command": "multiCommand.callTerminate",
      "sequence": [
        { "command": "workbench.action.terminal.sendSequence",
          "args": { "text": "terminate.bat\u000D" }
        }
      ]
    }
  ]
然后在
keybindings.json

  {
    "key": "shift+alt+F1",  // or any other key combo
    "command": "multiCommand.callPrincipal"
  },
  {
    "key": "shift+alt+F2",  // or any other key combo
    "command": "multiCommand.callTerminate"
  }
您可以添加
when
子句来限制键绑定的有效性

  {
    "key": "shift+alt+F1",  // or any other key combo
    "command": "workbench.action.terminal.sendSequence",
    "args": { "text": "principle.bat\u000D" }
  },
  {
    "key": "shift+alt+F2",  // or any other key combo
    "command": "workbench.action.terminal.sendSequence",
    "args": { "text": "terminate.bat\u000D" }
  }

编辑

当您只需要一个终端命令时,不需要使用
多命令扩展名
,您可以定义keybinding的参数

  {
    "key": "shift+alt+F1",  // or any other key combo
    "command": "workbench.action.terminal.sendSequence",
    "args": { "text": "principle.bat\u000D" }
  },
  {
    "key": "shift+alt+F2",  // or any other key combo
    "command": "workbench.action.terminal.sendSequence",
    "args": { "text": "terminate.bat\u000D" }
  }

或者,您可以定义两个任务来启动批处理文件,并使用终端运行它们。|运行任务…

如果指定一个作为生成任务,则可以使用该任务的快捷键


我找不到运行命名任务的命令,因此它可以用于keybinding。

有趣。命令后的
\u000D
做什么?@Tryer这是
输入
键谢谢。如果我需要在linux系统下运行一个shell文件,这是否有效?i、 例如,我可以说
/principle.sh\u000D
@Tryer是的,无论终端接受什么,您都可以在
文本
参数中输入谢谢!我用这两个参数为每个命令设置了两个任务:“text”:“cd\”${fileDirname}\“\u000D”和“text”:“abaqus job=analisis user=2D.for\u000D”