Using Python with VS Code – 3 Tips & Tricks Help You to Write Code Faster

VS Code Python Tips Tricks

If you are using Visual studio code to write a Python application, you may think it does not offer many tools.  VS code is not built to create Python applications. But if you want the freedom to customize & a lightweight IDE, you can choose VS code.

Here I am listing some shortcut keys, and setups to write your code faster in Visual Studio code.

Tip1: Turn selected code to comment (VS Code Python – Single line comment)

Ctrl+/ – To comment & uncomment multiple lines

Write a multiline comment with a shortcut key

To comment on multiple lines in a Python file,

Select the code lines and press the Shift + Alt + A shortcut key.

That’s it you will get multi-line comments like this

""" excel = client.Dispatch("Excel.Application")

sheets = excel.Workbooks.Open(r'D:\Chromedriver\output.xlsx')

work_sheets = sheets.Worksheets[0] """

Tip 2: Change indentation from 2 spaces to 4 spaces for Python file only

When you write code like function, loop, if-else statement, etc. You have to use 4-space indentation.  But by default, Visual Studio Code applies 2 2-space indent when you press the Tab key.

To change it to 4 spaces, go to VS code Extensions.

Search and install the EditorConfig for the VS Code plugin in the VS code editor.

Now in VS Code workspace, select your Python project folder -> right click and choose “Generate .editorconfig”.

It will create .editorconfig file in that folder.

Now add the following line in .editorconfig file.

[*.py]
indent_style = space
indent_size = 4

Save the file.

Now in your python file, select a particular line of code you want to indent and press the Tab key on your keyboard. That’s it. It will apply 4 spaces for the indentation.

for conten in content:
    title = conten.get_text()
    content_title.append(title)

Tip 3: Set keyboard shortcut to run python file in VS code Terminal

In the VS Code editor, open the command palette by pressing the “F1” key.

Type and search “Python: Run Python file in Terminal”.

Click the settings icon in the right corner.

For this command, click the Keybinding and add the shortcut key.

Here I am using ctrl + 1 to run the Python file.

That’s it.

Extra Tip: Kite Plugin for VS Code 

Visual Studio code offers Code Autocomplete and IntelliSense. But Kite, 3rd party plugin for VS code gives a super-fast auto-complete experience.

First, in the VS code Extension, search and install “Kite AutoComplete AI Code”.

Then open your Python file. In the VS code terminal, it will show a message like “Kite requires the Kite Copilot desktop application’.

Click install. It will install a standalone kite application on your computer.

Then close open VS code. You will check if the Kite is ready for code auto-completion or not in VS Terminal.

That’s all.

Multi-line Editing

  • Hold the Alt key and insert the cursor on the multiple lines using your mouse to get the multi-line editing.
  • If you want to select and edit the same words in multi-line editing, select 1 word and press Ctrl+D. To insert the cursor in all matching locations. use shortcut key – Ctrl+ Shift+ L.
  • If you want to insert a cursor at the end of the selected lines, select some lines and press Shift+Alt+I.
  • If you want to insert a multi-line cursor above and below the current cursor,  press Ctrl+Alt+Up/Down.

If you are Creating Flask back WebApps

Remove all CSS classes at once

  1. Install Class Killer VS code extension.
  2. To use this extension, press ‘Ctrl +Shift +P”.
  3.  Search “Remove all classes – Class Killer”. Apply this option. It will remove all CSS classes.

Remove All comments at once

  1. Install Remove comments VS code extension. This extension supports multiple programming languages like Python, PHP, etc.
  2. To use this extension, press ‘Ctrl +Shift +P”.
  3. Search “Comments: Remove All Comments”. Apply this option. It will remove all comments at once.

That’s it. I hope this tutorial will help you to write Python code faster in Visual Studio code. If you know any other tips and tricks for Python VS code, do let us via the comment form.

About The Author

Scroll to Top