How to Run Code in VS Code
One of the most popular source-code editors, Visual Studio Code, commonly called VS Code, is very beginner-friendly. Its user-friendly interface and advanced features make it a favorite for both beginners and veteran programmers alike.
If you’re new to VS Code and looking for a simplified guide on running code, you’re in the right place. We’ll guide you through setting up required software and running code using familiar languages like C/C++ and Python, as well as how to run command-line arguments.
The Shortcut to Run Code in VS Code
In VS Code, you only need to use a shortcut to run your code. That shortcut is Ctrl + Alt + N. There are a few more ways to run code.
Pressing F1 and then choosing Run Code also works. If you want to type it out after pressing F1, you’re free to do that as well.
Users can right-click the text editor and then select Run Code in the context menu. With only two clicks, your code will run.
“Run Code” is also an option in the editor title menu and the file explorer’s context menu.
Should you want to stop your code from running, the shortcut is Ctrl + Alt + M. Pressing F1 also lets you choose the Stop Code Run option. This option is also available in the editor title menu and Output Channel.
Lastly, you can right-click on the Output Channel to open the context menu and select Stop Code Run.
Learning these shortcuts and methods should make running and stopping code a little easier in various situations.
How to Run C Code in VS Code
Besides knowing C code and VS Code, you’ll have to download the C/C++ extension. Without it, you won’t be able to run C code within VS Code.
These are the instructions for running C code in VS Code:
Setting Up the Required Software
- Launch VS Code.
- Find the C/C++ extension on the extensions marketplace.
- Install the extension.
- Download and install MinGW.
- Make sure you 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 Path.
- Select Edit.
- From here, click on New.
- Paste the MinGW path and click the OK button.
- Click OK for the other popup windows.
Coding in VS Code
- Create a folder for your C code.
- Add the folder to VS Code and then 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 you complete the one-time software setup, you’ll be able to use VS Code and the C language easily. Just make sure the other required programs are correctly installed before running code, especially the packages.
How to Run Python Code in VS Code
Running Python code in VS Code is relatively easy. You’ll need an extension and a Python interpreter. The former is found in the VS Code extensions marketplace, but Python interpreters vary depending on how they’re installed.
Before you install the others, you should already have Python on your PC. Verifying it beforehand is also essential.
With the preliminaries out of the way, let’s get into 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 at the top-right corner of your editor.
The interpreter you install depends on your needs, but VS Code works incredibly well with Python regardless of your interpreter choice.
How to Run JS Code in VS Code
- Download and install NodeJS on your PC.
- 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.
- Type “
- If you want to check your code output, type “
node yourfilename” and wait.
Your directory and files will be named differently, so we only included these as placeholder names.
- Install Code Runner.
- Run the code with Ctrl + Alt + N or any other method.
- The Output Window will show your code.
If you only want to test a few lines of code and then save it, this method shines. You can finish within 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 for debugging or launching code files, and VS Code is entirely compatible with these types of codes and debugging. In this section, you’ll learn how to run code with arguments.
Before you do so, you have 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 running code with arguments:
- Open launch.json and 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.
Coding Made Easy
With this knowledge, running code in VS Code should become as natural as breathing – with enough practice. The various languages you can use with VS Code make it a powerful and straightforward IDE to pick up. As such, many developers highly recommend aspiring programmers learn how to use it.
Do you like using VS Code? What do you think about the interface? Let us know your thoughts in the comments section below.