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

8

5

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?

Knackwurst answered 3/10, 2019 at 14:42 Comment(0)
A
5

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"
}
Aindrea answered 9/10, 2019 at 21:21 Comment(2)
Thanks for this, it works! But how can I change it to use on Powershell Terminal?Knackwurst
Never mind, I've found it. Just put powershell after the word start.Knackwurst
Y
6

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

Yenta answered 20/10, 2020 at 15:51 Comment(0)
A
5

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"
}
Aindrea answered 9/10, 2019 at 21:21 Comment(2)
Thanks for this, it works! But how can I change it to use on Powershell Terminal?Knackwurst
Never mind, I've found it. Just put powershell after the word start.Knackwurst
T
1

For Linux :

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

If you want to execute your code with gnome terminal:

"code-runner.executorMap": {
        "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && gnome-terminal -- bash -c './$fileNameWithoutExt ; read line'",
        "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && gnome-terminal -- bash -c './$fileNameWithoutExt ; read line'",
}
        

use this if you are running ubuntu or if you have gnome terminal. 'read line' is used to keep the window open untill any key is pressed after the code is executed

If you want to execute your code with Xterm:

    "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

Tenno answered 3/5, 2020 at 7:11 Comment(2)
Is this same for Windows?Knackwurst
No, it is only for Linux and mac. But the answer given by @Edoardo works for windows :)Tenno
I
1

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.

Ixtle answered 17/7, 2020 at 4:0 Comment(0)
C
1

If you're using Code Runner you can add this to your settings.json:

    "code-runner.executorMap":{
        "cpp": "cd $dir && g++ -O2 -std=c++17 $fileName -o $fileNameWithoutExt && start cmd \"/k ; $fileNameWithoutExt\""
    },

This will keep the cmd after the execution of program.

Centare answered 14/2, 2021 at 9:34 Comment(0)
T
0

I don't know why konsole -- bash -c "command" command is now working for kde konsole So, if you use kde then install gnome-terminal first If you want to run an external terminal then add this to settings.json vscode

"code-runner.runInTerminal": true,
"code-runner.preserveFocus": false,
"code-runner.executorMap": {
    "c": "cd $dir && echo $PWD > /tmp/vsDir && echo $fileName > /tmp/vsFile && gnome-terminal -- bash -c 'dir=\"$(cat /tmp/vsDir)\" && cd $dir;File=\"$(cat /tmp/vsFile)\" && gcc $File -lm;./a.out;echo Hit Enter to EXIT;read lines;rm *.out' && exit",
    "cpp": "cd $dir && echo $PWD > /tmp/vsDir && echo $fileName > /tmp/vsFile && gnome-terminal -- bash -c 'dir=\"$(cat /tmp/vsDir)\" && cd $dir;File=\"$(cat /tmp/vsFile)\" && g++ $File -lm;./a.out;echo Hit Enter to EXIT;read lines;rm *.out' && exit",
    "java": "cd $dir && echo $PWD > /tmp/vsDir && echo $fileName > /tmp/vsFile && echo $fileNameWithoutExt > /tmp/javaRun && gnome-terminal -- bash -c 'dir=\"$(cat /tmp/vsDir)\" && cd $dir;File=\"$(cat /tmp/vsFile)\" && javac $File;binary=\"$(cat /tmp/javaRun)\";java $binary;echo Hit Enter to EXIT;read lines;rm *.class' && exit",
    "python": "cd $dir && echo $PWD > /tmp/vsDir && echo $fileName > /tmp/vsFile && gnome-terminal -- bash -c 'cd;source /home/$USER/anaconda3/bin/activate; conda activate MyPy38;dir=\"$(cat /tmp/vsDir)\" && cd $dir;File=\"$(cat /tmp/vsFile)\" && python -u $File;echo Hit Enter to EXIT;read lines' && exit",
  },
  "terminal.integrated.inheritEnv": false

If you don't want to remove the binary or exe or class file then remove rm *.out or rm *.outor similar commands from it.

For Anaconda Python source /home/$USER/anaconda3/bin/activate; is for activate base anaconda env and then active my env name MyPy38 at conda activate MyPy38;. If you run python from the default python3 and pip libreary then replace the command with

"python": "cd $dir && echo $PWD > /tmp/vsDir && echo $fileName > /tmp/vsFile && gnome-terminal -- bash -c 'cd;dir=\"$(cat /tmp/vsDir)\" && cd $dir;File=\"$(cat /tmp/vsFile)\" && python3 -u $File;echo Hit Enter to EXIT;read lines' && exit",

or, You can use project base python envs "Like suppose python interpreter directory is /From/Base/To/Python/Binary/ directory " so, It will be

"python": "cd $dir && echo $PWD > /tmp/vsDir && echo $fileName > /tmp/vsFile && gnome-terminal -- bash -c 'cd;dir=\"$(cat /tmp/vsDir)\" && cd $dir;File=\"$(cat /tmp/vsFile)\" && /From/Base/To/Python/Binary//python -u $File;echo Hit Enter to EXIT;read lines' && exit",
Theodicy answered 24/2, 2022 at 16:31 Comment(0)
D
0

For those using Xfce terminal on Linux:
This will also pause the terminal after execution, requiring you to press any key to exit. Your program name and path can contain spaces as well.
You can change it to any other language with slight modifications.

"code-runner.executorMap": {
"cpp" : "cd $dir && g++ \"$fileName\" -o \"$fileNameWithoutExt\" && xfce4-terminal -e \"bash -c '\\\"./$fileNameWithoutExt\\\";echo;read -n 1 -s -r -p \\\"Press any key to exit...\\\"'\"",
}
Digenesis answered 27/10, 2022 at 11:46 Comment(0)
M
0
{
    "code-runner.runInTerminal": true,
    "code-runner.executorMap": {
        "c": "cd $dir && g++ $fileName -o $fileNameWithoutExt.exe && start $dir$fileNameWithoutExt.exe",
        "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt.exe && start $dir$fileNameWithoutExt.exe"
    }
}
Materi answered 20/9, 2023 at 4:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.