Command Runner 透彻指南(VSCode)

垃圾code-runner

使用g++编译

  1. 在 VSCode 中安装 Command Runner 插件

  2. 将 VSCode 的默认终端设为 PowerShell

  3. 在 VSCode 设置里的 settings.json 中把下面的代码加进去就行了(环境变量中没有g++就换成绝对路径)。

    "command-runner.terminal.autoFocus": true,
    "command-runner.commands": {
    "compile-cpp": "cls; cd '${fileDirname}'; echo 'Compiling...'; g++ -g -std=c++14 -O2 '-Wl,--stack=2147483647' '${fileBasenameNoExtension}.cpp' -o '${fileBasenameNoExtension}.exe'; $lastcompile = $?; if($lastcompile) { echo 'Compilation Completed!'; } else { echo 'Compilataion error!'; } echo `n; ",
    "run-cpp": "cls; cd '${fileDirname}'; if($lastcompile) { echo 'The program is running!'; echo '----------------------------------'; $start = Get-Date; .\\'${fileBasenameNoExtension}.exe'; $end = Get-Date; $success = $LastExitCode; echo `n----------------------------------; $cost = ($end-$start).TotalSeconds; echo \"Process exited after $cost seconds with return value $LastExitCode\"; } else { echo 'Source file is not compiled.'; } echo `n; ",
    "compile&run-cpp": "cls; cd '${fileDirname}'; echo 'Compiling...'; g++ -g -std=c++14 -O2 '-Wl,--stack=2147483647' '${fileBasenameNoExtension}.cpp' -o '${fileBasenameNoExtension}.exe'; $lastcompile = $?; if($lastcompile) { echo 'Compilation Completed!'; echo `n'The program is running!'; echo '----------------------------------'; $start = Get-Date; .\\'${fileBasenameNoExtension}.exe'; $end = Get-Date; $success = $LastExitCode; echo `n----------------------------------; $cost = ($end-$start).TotalSeconds; echo \"Process exited after $cost seconds with return value $LastExitCode\"; } else { echo 'Compilataion error!'; } echo `n; "
    },
  4. 在 VSCode 快捷键绑定里的 keybindings.json 中把下面代码加进去就行了

    {
    "key": "F9",
    "command": "command-runner.run",
    "args": {
    "command": "compile-cpp",
    "terminal": "runCommand"
    }
    },
    {
    "key": "F10",
    "command": "command-runner.run",
    "args": {
    "command": "run-cpp",
    "terminal": "runCommand"
    }
    },
    {
    "key": "F11",
    "command": "command-runner.run",
    "args": {
    "command": "compile&run-cpp",
    "terminal": "runCommand"
    }
    }

这些设置会给你搞上这些操作: (老DEVC++了)

F9 编译当前程序
F10 运行当前程序
F11 编译并运行

我按c++14,O2编译的,如果想改的话请改 "compile-cpp""compile&run-cpp" 里编译器g++的参数

自定义指令

在settings.json中:

"command-runner.commands" :{
"命令名称1": "命令",
//你可以用[],但是它tm会在你写的每个命令后面加上逗号...
//所以你先写出来要用的命令,然后用 && 连接压成1行就行了

"命令名称2": "命令",
}

在keybindings.json中:

//一组快捷键
{
"key" : "你要设的快捷键",
"command": "command-runner.run",//先调用Command Runner
"args": {
"command": "在command-runner.commands中的命令名称",
"terminal": "runCommand"//终端
}
}
//想多设几个就多copy几遍