代码一键运行
安装好Code Runner之后,打开你所要运行的文件,有多种方式来快捷地运行你的代码:
- 键盘快捷键 Ctrl+Alt+N
- 快捷键 F1 调出 命令面板, 然后输入 Run Code
- 在编辑区,右键选择 Run Code
- 在左侧的文件管理器,右键选择 Run Code
- 右上角的运行小三角按钮
停止代码运行
如果要停止代码运行,也有如下几种方式:
- 键盘快捷键 Ctrl+Alt+M
- 快捷键 F1 调出 命令面板, 然后输入 Stop Code Run
- 在Output Channel,右键选择 Stop Code Run
Run in Terminal
在 GitHub Issue 中,用户问到最多的问题就是乱码和怎么支持输入。通过设置,我们可以把代码放到 VS Code 内置的 Terminal 来运行,这两个问题就能迎刃而解了。
选择 文件 -> 首选项 -> 设置,打开VS Code设置页面,找到 Run Code configuration,勾上 Run In Terminal 选项。设置之后,代码就会在 Terminal 中运行了。
自定义运行逻辑
对于一些语言,用户希望能自定义代码的运行逻辑。比如说,在 Code Runner 中,C++的默认编译器用的是 g++,也许你希望使用 Clang。那么你可以在 VS Code 设置页面,找到 Executor Map 设置项,并且选择 在settings.json中编辑。
在 settings.json 中,添加 code-runner.executorMap 设置,然后就可以对不同的语言设置自定义的运行逻辑了。下面就是对 Java 配置的一个例子:
如果你想自定义代码运行逻辑,你可以用到下面的一些变量,在运行时,Code Runner会把相应的变量进行替换:
- $workspaceRoot
- $dir
- $dirWithoutTrailingSlash
- $fullFileName
- $fileName
- $fileNameWithoutExt
code runner指定c/c++指定.exe文件生成路径
"code-runner.executorMap": {
"c": " cd $dir && g++ $fileName -o ..\\out\\$fileNameWithoutExt.exe && ..\\out\\$fileNameWithoutExt.exe",
},
launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\out\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "D:\\MinGW\\bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe 生成活动文件 ver(1)"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "D:\\MinGW\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${workspaceFolder}\\out\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\MinGW\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "编译器: D:\\MinGW\\bin\\gcc.exe"
},
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件 ver(1)",
"command": "D:\\MinGW\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${workspaceFolder}\\out\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\MinGW\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
]
}