How to make Code Runner run in external terminal (Command Prompt)?

怎甘沉沦 提交于 2020-12-12 07:02:13

问题


So, basically the Code Runner in Visual Studio Code can run in the integrated terminal. How can I make it to run in external terminal, which is command prompt because I need to present my program's output to my classmates, so it's not convenient to display it through the integrated one.

I know there's a software like Dev-C++ that can run in external terminal, but I love to use this VS Code because of its clean UI, and the Code Runner plugin is pretty good doing its job. How can I do it just with one-click? Is there any configuration?


回答1:


Supposing that you are using Windows, All you need to do is to put the "start" command before the .exe file you want to run in the code-runner.executorMap option. Here is an example:

"code-runner.executorMap": {
        "cpp": "g++ $fullFileName -o $fileNameWithoutExt.exe && start $fileNameWithoutExt.exe"
}



回答2:


If anybody wondering how to edit "code-runner.executorMap"":{} nether of above answers provide a way to get into settings.json. So I'm writing this answer just to clarify how you can get into the code runner settings.

go to the File -> Preferences -> Settings and search for 'executorMap' in search tab -> then click Code-runner:Executor Map and edit the code as follows for C,

"code-runner.executorMap": {
        "c": "g++ $fullFileName -o $fileNameWithoutExt.exe && start $fileNameWithoutExt.exe"
}

For C++ edit the code as follows,

"code-runner.executorMap": {
        "cpp": "g++ $fullFileName -o $fileNameWithoutExt.exe && start $fileNameWithoutExt.exe"
}

Note: This solution is for windows environments




回答3:


If you are running linux then you can paste this in code-runner.executorMap

    "code-runner.executorMap": {
        "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && xterm -hold ./$fileNameWithoutExt",
        "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && xterm -hold ./$fileNameWithoutExt"
    }

you can use this if you have xterm. -hold flag is used to keep the window open after the code is executed




回答4:


On Windows, when Default Shell, in VSCode, is set to PowerShell 7 (pwsh.exe) I use:

"code-runner.executorMap": {
    "c": "cd $dir && gcc -Wall -Wextra -pedantic -std=c18 $fileName -o $fileNameWithoutExt.exe && Start-Process -FilePath pwsh -ArgumentList \"-Command  &{.\\$fileNameWithoutExt.exe; pause}\"",
}

When Default Shell, in VSCode, is set to Command Prompt (cmd.exe) I use

"code-runner.executorMap": {
     "c": "cd $dir && gcc -Wall -Wextra -pedantic -std=c18 $fileName -o $fileNameWithoutExt.exe && Start pwsh -Command  \"& {.\\$fileNameWithoutExt.exe; pause}\"",
}

In both cases "pause" will keep the external PowerShell 7 terminal open after the code is executed.



来源:https://stackoverflow.com/questions/58221544/how-to-make-code-runner-run-in-external-terminal-command-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!