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?
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"
}
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
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.
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
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.
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",
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...\\\"'\"",
}
Related
I setup WSL Ubuntu but I can't run my code in the right path with WSL .
How to run my code in the right path with WSL ?
This is my settings.json
"code-runner.runInTerminal": true
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\wsl.exe"
"code-runner.executorMap":
{
"c": "cd $(wslpath $dir) & gcc $fileName -o $fileNameWithoutExt & ./$fileNameWithoutExt"
"cpp": "g++ $fileName -o $fileNameWithoutExt -std=c++11 & ./$fileNameWithoutExt"}\
"python": "python3 -u ./$fileName"
}
When I run my code with "ctrl+alt+N"
The output became:
jamesqian#J:/mnt/c/Users/Jamesqian/Desktop/tmp$ cd $(wslpath "c:\Users\Jamesqian\Desktop\tmp\") & gcc test.c -o test & ./test
wslpath is here
im totally new to this extension. trying to set it up for C++.
what exactly do i need to add into my settings.json file?
currently i have tried these 2 settings:
"code-runner.executorMap":
{
"cpp": "cd $dir && g++ -std=c++14 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
AND
"cpp": "cd $dir && g++ *.cpp -o $fileNameWithoutExt && $fileNameWithoutExt.exe"
},
But I'm getting these errors:
C:\Users\w6\Documents\1. BME\Semester 4\Basics Of Programming 2\Labs\Lab 4>cd "c:\Users\6\Documents\1. BME\Semester 4\Basics Of Programming 2\Labs\Lab 4\" && g++ *.cpp -o Task 2 Circle && Task 2 Circle.exe
g++: error: 2: No such file or directory
g++: error: Circle: No such file or directory
I'm using the shell script defined here (https://code.visualstudio.com/Docs/setup), and while I can type code . to open up VS Code, it does not select the current directory to show in the tree.
With the 0.3.0 update this issue has been fixed. Please use this updated command:
code () {
VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $*
}
Update for our VS Code 1.0 release: please use the command Install code command in path from the command palette (View | Command Palette).
I've tested the sample on the setup page and it works fine.
code () {
if [[ $# = 0 ]]
then
open -a "Visual Studio Code"
else
[[ $1 = /* ]] && F="$1" || F="$PWD/${1#./}"
open -a "Visual Studio Code" --args "$F"
fi
}
I did however add it to the bottom of my ~/.bashrc instead of my .bash_profile. The .bash_profile simply sources the .bash_profile so it should be equivalent.
if [ -e ~/.bashrc ]
then
. ~/.bashrc
fi
You can test that you are running in a bash shell by executing:
echo $SHELL
This should return /bash/bash.
I'm using a here tag in a Jenkins build step to send my deploy commands over ssh, and unfortunately the build is passing even when the commands inside the here tag don't finish successfully:
ssh user#host <<EOF
cd /path/to/app
git pull
bower install
npm install
grunt build
cp -r /path/to/app/dist/* /path/to/dist/
forever restartall
exit
EOF
Is there a better way to approach this problem?
You are not catching any error codes inside your "here document".
Last command is exit and without an exit code, it will default to 0 which is success.
Since the last command of your ssh is a success, the whole command is treated as success, and build is a success.
Easiest way to fix that: chain all commands with && like so:
cd /path/to/app && git pull && bower install && npm install && grunt build && cp -r /path/to/app/dist/* /path/to/dist/ && forever restartall && exit
Best way to fix that: write a proper shell script, with error handling, and execute that. If you are too lazy to error handle every line, you can start the script with set -e which will fail the shell script on any individual error
Edit:
#!/bin/bash
appPath="/path/to/app"
distPath"/path/to/dist"
echo "My great deployment script"
echo "Deploying ${appPath} to ${distPath}
if [[ ! -w "${appPath}" ]]; then
echo "${appPath} is not writable, quitting"
exit 1
else
cd ${appPath} && git pull || { echo "Failed on 'git pull'"; exit 2; }
bower install || { echo "Failed on 'bower install'"; exit 3; }
npm install || { echo "Failed on 'npm install'"; exit 4; }
grunt build || { echo "Failed on 'grunt build'"; exit 5; }
if [[ -w "${distPath}" ]]; then
cp -r ${appPath}/dist/* ${distPath}/ || { echo "Failed on 'copy'"; exit 1 }
forever restartall || { echo "Failed on 'forever restartall'"; exit 6 }
echo "Deployment successful"
exit 0
fi
fi
You then execute it with: ssh user#host 'bash -s' < myfile.sh (if the file is local)
Or if you place the file remotely, then just: ssh user#host '/path/to/remote/myfile.sh'
I need to install heroku-toolbelt without sudo. I am trying to install it in virtual env.
I modified install.sh file (changed paths)
> #!/bin/bash {
> HEROKU_CLIENT_URL="https://s3.amazonaws.com/assets.heroku.com/heroku-client/heroku-client.tgz"
>
> echo "This script requires superuser access to install software."
> echo "You will be prompted for your password by sudo."
>
> # clear any previous sudo permission
> #sudo -k
>
> # run inside sudo
> #sudo sh <<SCRIPT
>
> # download and extract the client tarball rm -rf
> /users/user/Documents/mypy/usr/local/heroku mkdir -p
> /users/user/Documents/mypy/usr/local/heroku cd
> /users/user/Documents/mypy/usr/local/heroku
>
> if [[ -z "$(which wget)" ]]; then
> curl -s $HEROKU_CLIENT_URL | tar xz else
> wget -qO- $HEROKU_CLIENT_URL | tar xz fi
>
> mv heroku-client/* . rmdir heroku-client
>
> SCRIPT
>
> # remind the user to add to $PATH
> if [[ ":$PATH:" != *":/users/user/Documents/mypy/usr/local/heroku/bin:"* ]]; then echo "Add the Heroku CLI to your PATH using:" echo "$ echo
> 'PATH=\"/users/user/Documents/mypy/usr/local/heroku/bin:\$PATH\"' >>
> ~/.profile"
> fi
>
> echo "Installation complete" }
But the script is not working.
I had a similar issue when installing the tool belt in a unbuntu vitural env using PuTTY as the SSH. Before I installed I used the command sudo -k to clear permissions before pasting the link. I know that should not be an issue since it is int he script but it worked. I hope it helps.