How to Auto-Format Unwanted Python Line Indentations in VS Code

ยท

6 min read


VS Code can remove unwanted line indentations in a Python file.. or can it?

TLDR: VS Code no longer supports the desired formatting behavior, but if you want, you can still install a version that supports it.

Update: You can install autopep8 in your virtual environment and run autopep8 -i -a -a your-file-name.py to get the desired formatting. Unfortunately, it won't apply in VS Code settings. You must run this in a terminal window. Thanks to Michael J. Sheets for the email!

The Beginning

I published a 9 Hour Python Course for Beginners in 2023.

In the second chapter of the course, I show how VS Code can auto-format your Python code when you save your file.

I did it again to create the screen recording below:

VS Code Python auto-formatting unwanted indentations

You can see that I tab twice and then choose "Format Document With" from the context menu. I then choose "Python" and the file auto-formats.

Then I tab twice again and save the file with Ctrl+S. The file once again formats because I have Format On Save selected in my settings.

The Problem

Shortly after publishing my course, VS Code changed how it handles auto-formatting.

This has led to some confusion and frustration.

I get it.

I am also frustrated when things change right after I publish a video.

In tech things change all the time - and that includes web development and programming tools.

My best advice for beginners is to get used to change. Change will keep happening throughout your career in tech.

That said, you can tackle a problem like this and find a solution.

I did, and I'm going to share it below.

First, let's address some fixes that won't work and a discussion that is not exactly accurate.

Fixes That Won't Work

After receiving many video comments about the Python formatting example I provided, I opened a Python file in VS Code and tried it again.

It didn't work.

It clearly used to work as I demonstrated in the video, but something had indeed changed.

At this point, I knew it was likely due to a VS Code update.

I searched the web for an answer. I found all sorts of random advice given with the best of intentions.

Some shared their VS Code settings.json preferences for Python because theirs worked.

Some blamed using a virtual environment.

Others claimed autopep8 was deprecated, and everyone should use black instead. (As of January 2024, autopep8 is available for use and is not deprecated.)

I believe anyone sharing the above solutions were just a VS Code update away from theirs not working as well.

A Not Exactly Accurate Discussion

I read the autopep8 logs of my current VS Code installation. There I noticed a warning stating Skipping non python code.

I Googled this phrase along with VS Code and found a GitHub discussion with a VS Code maintainer.

I know he was trying to help, but he suggested what I demonstrated in the video above was NOT possible with VS Code as I had it setup:

That's probably not being done by a formatter we support since that isn't valid Python code (you can't have an indent like that as it's not valid Python code). They may have some other extension installed, pressed something else to fix the indentation (e.g. ctrl+shift+tab to dedent the line), etc.

Regardless, there is zero expectation from us that any formatter we support would be able to handle fixing that code.

Again, I know he meant well, and I respect the VS Code (and all) maintainers. Nothing personal here, friend. Your work is vital.

Unfortunately, all of the assumptions above were incorrect.

  • I did not use another formatter.

  • I did not have another extension installed.

  • I did not press a key combination to dedent the line

I'm sure the maintainer would have directly addressed the issue if he was provided with steps to recreate the conditions for himself.

That's exactly what I'm going to do.

How to Remove Unwanted Line Indentations with VS Code Auto-Formatting

I had updated VS Code on my main workstation. The auto-formatting I demonstrated in the video was no longer working on it.

Fortunately, my old Windows laptop had not been updated.

I checked my installed versions of VS Code and the Python extension for VS Code:

VS Code v1.70.2

Python Extension v2022.16.1

I created a new Python file in VS Code and quickly recreated the auto-formatting scenario.

It worked!

VS Code Python auto-formatting unwanted indentations

This told me my hunch was correct. The desired formatting behavior had been removed in a VS Code update.

I wanted to confirm I could recreate this desired formatting behavior on another computer, too.

I got out my new Macbook. It has the latest version of Python installed. (3.12.1 as of this writing)

I downloaded VS Code v1.70.2 and installed it.

That's not enough though. I needed the VS Code Python extension to identify improper formatting.

I couldn't find a VSIX file for version 2022.16.1 of the extension, so I went with the closest version I could find to download: v2022.15.12731017

To install a VS Code extension from a VSIX file, open the Command Palette (Ctrl+Shift+P) and use the Install from VSIX... command.

With these older versions now installed on my new Macbook, I once again recreated the auto-formatting scenario.

It worked, too!

VS Code Python auto-formatting unwanted indentations

Note the following:

  • I do not have any other formatting extensions.

  • I do not have anything about Python in my settings.json file.

  • If I Ctrl+Click for the context menu and choose Format Document With, Python is the only formatting option listed.

Is It Worth It?

Probably not.

I wouldn't go back to an old version of VS Code just for this feature. This would also cause you to miss any new updates.

Recent editions of VS Code give you red squiggly lines to identify this formatting mistake. You should be able to easily see them and correct them on your own.

Note: I said formatting mistake.

If you read the full GitHub discussion I mentioned earlier, the maintainer says the line in question isn't being formatted because it isn't valid Python code.

Instead of considering this to be a formatting issue, it seems VS Code is now handling this as invalid code - an error. Their opinion is this is something you should identify and fix yourself instead of relying on a formatter.

That said, we all now know VS Code was once capable of auto-correcting unwanted line-indentations.

It sure would be cool if a current version of VS Code did this again.


Let's Connect!

Hi, I'm Dave. I work as a full-time developer, instructor and creator.

If you enjoyed this article, you might enjoy my other content, too.

My Stuff: Courses, Cheat Sheets, Roadmaps

My Blog: davegray.codes

YouTube: @davegrayteachescode

X: @yesdavidgray

GitHub: gitdagray

LinkedIn: /in/davidagray

Patreon: Join my Support Team!

Buy Me A Coffee: You will have my sincere gratitude

Thank you for joining me on this journey.

Dave