Some tips to make your VS Code more efficient (2023)

VS Code can be more friendly than you think

There are many IDEs available for writing Python code, such as Visual Studio Code (VS Code), PyCharm, Atom, Spyder, IntelliJ IDEA, etc. I have tried and used several different IDEs in the past and finally switched to VS Code for writing Python code. I prefer VS Code because it’s very simple to use, has very flexible settings, has a large number of third-party extensions, has a large community, etc. You can find more advantages when you start to use it.

However, if you are new to VS Code and don’t set it up properly, you may find it pretty annoying sometimes and may not be as handy as you expect, especially when you have been used to some other IDEs. In this post, I will share some of the tips I have got in the past years with VS Code. If you have started using VS Code and are not quite happy with it yet, this post can let you work more efficiently with it and like it more.

Some tips to make your VS Code more efficient (1)

Tip 1 — Disable the preview mode.

By default, when you open a file from the left sidebar’s file browser or by Ctrl + P, the file is opened in the Preview mode. When you open another file, the file opened previously in the preview mode will be closed, and the new file will be opened in the preview mode. The title of a tab in preview mode is italic. If you want to edit a file, you need to double-click the file name in the sidebar or the tab.

I personally find the preview mode very annoying and always disable it. If you also want to disable the preview mode, you can click File => Preferences => Settings, and search for “disable preview”. Then uncheck the options to enable Preview.

Some tips to make your VS Code more efficient (2)

Tip 2 — Install the Python extension.

The Python extension provides rich support for Python, including features such as IntelliSense (Pylance), linting, debugging, code navigation, code formatting, refactoring, variable explorer, test explorer, and more!

Some tips to make your VS Code more efficient (3)

Tip 3 — Install the Material Icon Theme extension.

The Material Icon Theme extension shows the icons of files based on file extensions in the file browser.

Some tips to make your VS Code more efficient (5)

Tip 4— Use Ctrl + P to find files and use Ctrl + Shift +P to find settings.

Some tips to make your VS Code more efficient (6)
Some tips to make your VS Code more efficient (7)

Tip 5— Set rulers to your editor.

According to PEP8, the comments should wrap at 72 characters and code at 79. It will be helpful to have some rulers in the editor which can help you wrap the comments or code at corresponding lengths.

Press Ctrl + Shift +P, then search for “settings”, and then choose “Preferences: Open Settings (JSON)”:

Some tips to make your VS Code more efficient (8)

Add "editor.rulers": [72, 79] to the end of the JSON file, save the file, and then you will see the rulers:

Some tips to make your VS Code more efficient (9)

Tip 6 — Learn to use “Find All References”.

It’s common that we need to check the occurrences of a function or class across the whole repository. It can be achieved with the “Find All References” utility. It can be triggered in three ways:

  1. Right-click on the function/class, then choose “Find All References”.
Some tips to make your VS Code more efficient (10)

2. Put the mouse on the function/class, then press Alt + Shift + F12.

3. Press Ctrl + Shif + F, then search for the function/class, or anything you want to search for:

Some tips to make your VS Code more efficient (11)

Tip 7 — Open definitions in different ways.

You can open the definition of a function/class in three ways.

  1. Put the mouse on the function/class, then press F12, the definition will be opened in a different tab in the same window, if the preview mode is disabled.
  2. Hold the Ctrl key, then left-click the function/class. It has the same effect as way 1 above.
  3. Hold Alt + Ctrl keys, and left-click the function/class, the definition will be opened in a new window. This is the preferred way because the code can be checked and compared more conveniently side by side.
Some tips to make your VS Code more efficient (12)

Tip 8 — Choose a Python interpreter from a virtual environment.

You can create virtual environments with venv or conda and choose a corresponding one in VS Code as the Python interpreter. Normally, VS Code can recognize the environments automatically. If not, you need to enter the interpreter path which is the path to the python executable file in your virtual environment. If you don’t select a virtual environment, the third-party libraries installed in it cannot be understood by VS Code and cannot be colored accordingly. Also, you would not be able to check the source code of the third-party libraries.

To select a Python interpreter, press Ctrl + Shift + P, select “Python: Select Interpreter”.

Some tips to make your VS Code more efficient (13)

Then select a virtual environment for your project. It’s recommended to use conda to create a virtual environment because you can install different versions of Python in different environments.

Some tips to make your VS Code more efficient (14)

Tip 9— Set PYTHONPATH for VS Code in order to import local modules.

If you cannot import local packages or modules, you would need to add the path to the packages or modules to the special environment variable PYTHONPATH, which is used to add additional directories to the sys.path directory list. As a universally applicable case, you can add the current folder and its parent folder to PYTHONPATH, which will make all the modules and packages in the current folder, as well as the current folder itself importable in your Python code:

$ export PYTHONPATH=$PYTHONPATH:.:..

This command will make PYTHONPATH effective only in the current shell, if you want it to work in every new shell, you can add it to $HOME/.bashrc.

Tip 10 — Create your own code snippets.

If you use the pdb or rpdb module to debug your Python code, you may have got tired of setting the breakpoints repeatedly. Luckily, in VS Code, you can create your own code snippets to save you the work of repeated typing.

To create a code snippet, select File => Preferences => User Snippets. Then select “python.json” because we will create code snippets for Python. A template JSON file will be created for you, with a large comment explaining how to create a code snippet. Normally we don’t need to create complex code snippets with tab stops and placeholders. If you do, you may want to read more on the official page.

Let’s create two code snippets for inserting breakpoints for the pdb and rpdb libraries, respectively. Add the following code in “python.json”:

  • prefix — defines one or more trigger words that display the snippet in VS Code.
  • body — one or more lines of content, which will be joined as multiple lines upon insertion.
  • description — an optional description of the snippet displayed in VS Code.

Save “python.json”. Then you will be able to create code snippets for inserting debugging breakpoints:

Some tips to make your VS Code more efficient (15)
Some tips to make your VS Code more efficient (16)
Some tips to make your VS Code more efficient (17)

Tip 11 — Press Ctrl K, Ctrl I to show the hover of warnings and errors in VS code.

Sometimes, the warnings and errors in VS code cannot be shown when you place your mouse over it, especially when you have multiple linters installed. In this case, you can press Ctrl K, Ctrl I (first press Ctrl +K, then Ctrl +I) to show the warning or error.

Some tips to make your VS Code more efficient (18)

Tip 12 — Use black, pylint, and mypy in VS Code.

VS Code supports various formatters and linters for Python, which can be used to format and fix your Python code at the very early development stage. It’s recommended to follow this article to set up the different formatters and linters in VS Code, which can make your code more professional.

Tip 13 — Debug API code in VS Code.

You can debug your API code in VS Code conveniently. Your API server can run locally, or remotely. Please check this article if you want to debug your API code in VS Code.

We have introduced several tips for using VS Code to write Python code. This article is not intended to be exhaustive, but only to cover the most common tricks gained from practical experience that may also boost your efficiency prominently. It also serves as a reference for the author himself, especially when the VS Code is reinstalled from scratch. More tips may be added in the future.

Top Articles
Latest Posts
Article information

Author: Msgr. Benton Quitzon

Last Updated: 10/25/2022

Views: 6040

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Msgr. Benton Quitzon

Birthday: 2001-08-13

Address: 96487 Kris Cliff, Teresiafurt, WI 95201

Phone: +9418513585781

Job: Senior Designer

Hobby: Calligraphy, Rowing, Vacation, Geocaching, Web surfing, Electronics, Electronics

Introduction: My name is Msgr. Benton Quitzon, I am a comfortable, charming, thankful, happy, adventurous, handsome, precious person who loves writing and wants to share my knowledge and understanding with you.