I'm trying to replicate a feature from PyCharm in Visual Studio Code and running into trouble.
In PyCharm, when I copy a block of Python code and paste it inside a function, class, or loop, the IDE automatically adjusts the indentation to match the surrounding context. This behavior—often referred to as context-aware indentation—makes it easy to maintain clean code structure without manually fixing tabs or spaces.
In VSCode, however, when I paste the same block of code into a similar context, the indentation remains exactly as it was copied. This often results in broken indentation that I have to fix manually.
Example:
Copied code:
for item in collection:
process(item)
Pasted inside a function:
def my_function():
do_something()
# cursor is here
Result in VSCode:
def my_function():
do_something()
for item in collection:
process(item)
Expected (like in PyCharm):
def my_function():
do_something()
for item in collection:
process(item)
What I’ve tried:
- Installing the Black Formatter extension
- Enabling
"editor.formatOnPaste": true - Enabling
"editor.formatOnSave": true - Enabling
"editor.autoIndentOnPaste": true - Using
"[python]": { "editor.defaultFormatter": "ms-python.black-formatter" } - Running
Format DocumentandFormat Selection - Installing the Python Indent extension
None of these worked. The pasted code still doesn’t align with the surrounding block structure.
