One of the most popular source code editors, Visual Studio Code, commonly known as VS Code, is very beginner-friendly. Its user-friendly interface and advanced features make it a favorite of beginners and seasoned programmers alike.
If you're new to VS Code and looking for a simplified guide to running code, you've come to the right place. We'll walk you through setting up the required software and running the code using familiar languages like C/C++ and Python, as well as running command-line arguments.
The shortcut to run code in VS Code
In VS Code, you just need to use a shortcut to run your code. This shortcut is Ctrl+Alt+N. There are a few other ways to run code.
Pressing F1 and then choosing "Run Code" also works. If you want to type it after pressing F1, you are also free to do so.
Users can right-click on the text editor and then select "Run Code" from the context menu. In just two clicks, your code will run.
“Run code” is also an option in the editor title menu and the file explorer context menu.
If you want to stop your code execution, the shortcut is Ctrl + Alt + M. Pressing F1 also lets you choose the “Stop Code Execution” option. This option is also available in the editor title menu and the output channel.
Finally, you can right-click on the output channel to open the context menu and select "Stop Code Execution".
Learning these shortcuts and methods should make it easier to run and stop the code in various situations.
How to Run C Code in VS Code
In addition to knowing C code and VS code, you will need to download the C/C++ extension. Without it, you won't be able to run C code in VS Code.
Here are the instructions for running C code in VS Code:
- Launch VS Code.
- Find the C/C++ extension on the extension marketplace.
- Install the extension.
- Download and install MinGW.
- Make sure to install the "Mingw32-base package" and "Ming32-gcc-g++ package" options.
- Copy the path to the “bin” folder in MinGW.
- Go to Windows advanced system settings.
- Select “Environment Variables”.
- In the “System Variables” section, click on “Path”.
- Select "Edit".
- Choose a new path.
- Paste the MinGW path and click the "OK" button.
- Click "OK" for other pop-ups.
Coding in VS Code
- Create a folder for your C code.
- Add Folder to VS Code.
- Hover your mouse over the C code folder and click the “+” button.
- Write the file name.
- Start coding in C.
- Run the code with Ctrl+Alt+N or any of the other methods mentioned above.
Once the one-time software setup is complete, you will be able to easily use VS Code and the C language. Just make sure that other required programs are properly installed before running the code, especially the packages.
How to run Python code in VS Code
Running Python code in VS Code is relatively easy. You will need a Python extension and interpreter. The first is found in the VS Code extension market, but Python interpreters vary depending on how they are installed.
Before installing the others, you should already have Python on your PC. Checking it beforehand is also essential.
With the preliminaries done, let's move on to the coding process.
- With a command prompt, create an empty folder and open it.
- Open VS Code in the new empty folder.
- Choose the Python interpreter for VS Code to use.
- Create a Python source file.
- Start coding in Python.
- Run the Python code by clicking the "Play" button in the upper right corner of your editor.
The interpreter you install depends on your needs, but VS Code works incredibly well with Python regardless of your choice of interpreter.
How to run JS code in VS Code
- Download and install NodeJS on your computer.
- Launch VS Code and create a new folder.
- Write in JS and name the file with the .js extension.
- Save the changes.
- Open a VS Code terminal.
- If you want to check your code output, type "node yourfilename" and wait.
Your directory and files will be named differently, so we've only included them as placeholder names.
- Install Code Runner.
- Run the code with Ctrl+Alt+N or any other method.
- The output window will display your code.
If you only want to test a few lines of code and then save it, this method shines. You can finish in a minute and have a file ready to store for future use.
How to run code with arguments in VS Code
Command line arguments are intended for debugging or launching code files, and VS Code is fully compatible with these types of code and debugging. In this section, you will learn how to run code with arguments.
Before doing so, you need to get “launch.json”. If you don't have it, you can create it by following these steps:
- Go to “Run”.
- Select "Add configuration".
The following steps are for executing code with arguments:
- Open launch.json.
- Add your arguments.
- Select the file to debug.
- Click "Run and Debug" to debug your code.
These are the basics of running your code with arguments. In this case, we're talking about Python since it's the simplest example.
With this knowledge, running code in VS Code should become as natural as breathing – given enough practice. The different languages you can use with VS Code make it a powerful and easy to learn IDE. As such, many developers urge budding programmers to learn how to use it.
Do you like using VS Code? What do you think of the interface? Let us know your thoughts in the comments section below.