How to open a file or a folder in VS Code in a Linux terminal

Share your love

Visual Studio Code is one of the most effective Linux development tools used for code editing by most programmers using Linux as their main operating system.

A code editing tool is used to write, edit, and debug your code.

When it comes to Visual Studio Code, it supports the majority of the web and software development technologies that include JavaScript, Python, Go, HTML, and PHP, among many others.

Therefore, you do not need to switch between different code editing tools when writing a project requiring different types of technologies or programming languages.

Does Visual Studio Code work on Linux?

Visual Studio Code works very efficiently and fast on Linux.

Running the program, opening files, editing code, and debugging the code for errors works very smoothly on Debian Linux systems such as Ubuntu.

I have tested the performance of Visual Studio Code on a Kali Linux machine, and it works great.

Running the program or opening a directory with the editor happens within seconds.

It ends up saving too much time for me. Thus, I have to focus on writing code instead of waiting for code editing software to open or hanging in the middle of a debug process.

One would think that Visual Studio Code is only available for Windows users, but you can easily install and start to use Visual Studio Code on Debian operating systems such as Ubuntu, Kali Linux, Linux Mint, e.t.c.

Well, that is not true.

Visual Studio Code installs and works very well on Linux operating systems.

Before we start getting into the details of opening files and directories with Visual Studio Code, we need to install the code editing application first.

Let me show you how to install Visual Studio Code on Debian Linux that uses the apt package manager.

How to install Visual Studio Code on Debian Linux (Ubuntu, Kali Linux, Linux Mint)

Head to the Visual Studio Code download page to download the Visual Studio deb package.

Select the latest .deb version and download it to the downloads folder or anywhere you can access it.

After the download completes, navigate into the Downloads folder or the directory in which you stored your file using the File Manager or the Terminal.

Navigating using the File Manager

Open nautilus File Manager

Head over to the location of the downloaded file

Right-click anywhere on the folder and select Open in Terminal

Opening the Terminal using Linux nautilus

Navigating using the Terminal

Opening the Terminal window by pressing CTRL + ALT + T

Type the following to navigate into the Downloads folder where Visual Studio Code is located:

cd ~/Downloads

After navigating into the directory with the Visual Studio Code file, we will use the apt package manager to install the .deb package.

Type the following to install the Visual Studio Code .deb file we downloaded:

sudo apt install ./code*.deb

Ensure your file starts with the word ‘code’ for the above code to work.

Otherwise, note the downloaded file’s name and specify it after the keyword ‘install.’

For example, if your file name is thgythg.deb, type the following instead:

sudo apt install ./thgythg.deb

You should see something like this:

Installing Visual Studio Code on Linux

Confirm that you have successfully installed Visual Studio Code by typing:

code --version

Hit Enter.

You should see the version of the Visual Studio Code you downloaded earlier displayed on the Terminal.

That confirms that Visual Studio Code has been installed on your Debian Linux system.

Now, let’s see how you can run Visual Studio Code from the Linux Terminal

How to run Visual Studio Code in Linux Terminal

Running Visual Studio Code in a Terminal window is as easy as typing code and hitting enter.

Opening Visual Studio is as easy as that.

Open a new Terminal window and type the following:

code

Hit Enter.

You should see a new Visual Studio Code window open.

Within seconds, you should see a Release Note displayed when opening the code editor the first time.

Otherwise, you should see a window to set up a new project.

How to run Visual Studio Code on Linux

Let’s open files and directories with Visual Studio Code using the Terminal.

How to open a file in Visual Studio Code from Linux Terminal

To open a file in Visual Studio Code from the Terminal, you need to type the keyword code with the name of the file that you want to open.

So, if you have a file with the name myfile.txt, you should type code myfile.txt on the Terminal and hit Enter.

For demonstration, open a new Terminal window and type the following:

cd ~/Desktop

Here, we are navigating inside the Desktop folder of our machine.

Next, create a file with the name myfile.txt using the touch command:

Type the following and hit Enter to create the file:

touch myfile.txt

After creating the file, let’s open the file with Visual Studio Code using the Terminal.

Type the following:

code myfile.txt
Editting files on Linux using Visual Studio Code

Voila!

A new Visual Studio Code opens with the file opened for editing.

Opening files with Visual Studio Code from the Terminal is as easy as that.

How to open a folder in Visual Studio Code from Linux Terminal

Opening a folder in Visual Studio Code from the Terminal involves typing code followed by the name of the directory you want to open.

So, if the directory name is my_dir, you would type code my_dir to open the directory in Visual Studio Code.

Open a new Terminal window, navigate into the Desktop folder, and create a new directory called my_dir.

Follow these steps:

Navigate into the Desktop folder using the Terminal:

cd ~/Desktop

Create a new folder using the mkdir command

mkdir my_dir

Navigate into the directory and create a new file called main.py

cd my_dir

touch main.py

Type ls to display the contents of the directory

ls

You should see the new file (main.py) that we created.

To open the directory using Visual Studio Code, type

code .

and hit Enter.

Opening a directory with VS Code on Linux using the Terminal

Visual Studio Code should open the my_dir directory, and you should see the main.py file.

You may click on the file to start editing.

To know more about the basic Terminal commands such as touch, mkdir, ls, cd, e.t.c used in Linux, I have written an article on that. Link here.

Ways of opening directories in Visual Studio Code from the Terminal

code .

Used to open a directory that you are currently navigated into.

If you are inside ~/Desktop/my_dir/ and you type code ., Visual Studio Code will open, displaying the contents of the my_dir directory.

code directory_name

Another way to open a folder with Visual Studio Code is to specify the directory you want to open after the word code.

For example, if you are navigated into the Desktop directory (parent directory) with two directories (my_dir and my_second_dir), you may use the code direcotry_name command to open either of the directories.

If you wish to open the my_second_dir folder, you will use the following command when you’re navigated inside the Desktop folder:

code my_second_dir

Otherwise, you may specify a full directory path if you’re not navigated into the directory parent location.

Here’s how you would open a directory with Visual Studio Code by specifying the full path of the directory on the Terminal:

code ~/Desktop/my_dir

OR

code /home/username/Downloads/first_folder/second_folder/
code -r directory_name

We may use the -r flag if you wish not to open a new Visual Studio Code.

Using the -r flag allows you to open a new directory using the current Visual Studio Code window that is open.

code -n directory_name

By default, Visual Studio Code opens a new window whenever you open a new directory or file using the Terminal.

However, you may specify using the -n flag to open a new window when opening a directory or file.

code -n flag is used to open a new Visual Studio Code window by opening a new instance of VS Code.

And that’s basically it for this tutorial.

I hope you are comfortable using the Linux Terminal and Visual Studio Code.

Happy coding with less frustration!

Share your love
Badi
Badi

Badi here, creator of ngangasn.com— A website dedicated to providing helpful information and how-to's of web development and hosting. Inspired by a fascination to write the most efficient code to make a computer laugh, & humans, Steve has a passion for organizing characters to create code and informative content.
What makes me happy?
Well, nothing like the feeling of finally figuring out that one pesky bug that's been driving me crazy.

Leave a Reply

Your email address will not be published. Required fields are marked *