There are multiple ways in which you can format code in Visual Studio Code IDE (also known as VS Code),
Option 1: Using Keyboard Shortcut
For Windows:Keyboard Shortcut: Shift + Alt + F
For Mac (macOS):Keyboard Shortcut: ⌥ Shift + Option + F
For Linux:Keyboard Shortcut: ⌥ Shift + Option + I
Note the first time you use the formatting shortcut you will get a VS Code Alert saying "Formatter autopep8 is not installed. Install?" do say YES or you can install it using pip command manually.
/opt/homebrew/bin/python3 -m pip install -U autopep8
Collecting autopep8
Downloading autopep8-1.7.0-py2.py3-none-any.whl (45 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 45.6/45.6 kB 1.1 MB/s eta 0:00:00
Collecting pycodestyle>=2.9.1
Downloading pycodestyle-2.9.1-py2.py3-none-any.whl (41 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 41.5/41.5 kB 2.3 MB/s eta 0:00:00
Collecting toml
Downloading toml-0.10.2-py2.py3-none-any.whl (16 kB)
Installing collected packages: toml, pycodestyle, autopep8
Successfully installed autopep8-1.7.0 pycodestyle-2.9.1 toml-0.10.2
Option 2: Using Mouse:
Be on your Python code file and do a right-click and select: Format Document
You can also select a section of your code and format it by selecting "Format Section"
Option 3: Using Palette Command:
Another way is to use the Palette Command,
Open Palette,
For Windows: Control + Shift + P
For Mac: Command + Shift + P
Now search for Format Document and select to format.
Do share in the comments if you know of any other ways or tricks to format Python code in VS Code!
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- How to convert int to ASCII in Python
- How to make use of SQLite Module in Python?
- Split a String into Sub-string and Parse in Python
- Python: Pandas Rename Columns with List Example
- How to run Python file from Mac Terminal
- How to Exit a Loop in Python Code
- Python: How to Plot a Histogram using Matplotlib and data as list
- MD5 Hashing in Python
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python]
- How to Read a binary File with Python
- How to add two float numbers in Python
- Python: How to install YAML Package
- Python: How to Save Image from URL
- What is Markdown in Jupyter Notebook with Examples
- How to change the Python Default version
- 33: Python Program to send an email vid GMail
- How to comment code in Python
- How to Fix AttributeError in Python
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode]
- How to comment out a block of code in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- Import Other Python Files Examples
- Python: How to add Progress Bar in Console with Examples
- 3 Ways to convert bytes to String in Python
- Display Seconds in Digital Clock on Mac Menu Bar [macOS] - MacOS
- Meaning of javascript:void(0) explained with example - JavaScript
- How to change the font in TextEdit on Mac - MacOS
- How to Disable Trackpad on MacBook - MacOS
- How to Enable Remote Management using Mac Terminal - MacOS
- Remove AirDrop Icon from macOS Menu Bar - MacOS
- Microsoft Office Mac Ventura: System Settings must be changed before Microsoft AutoUpdate can run - Microsoft
- Get Word Count in TextEdit - MacOS