A problem to which many threads exist, but I still could not figure out how to get it working.
When I paste code (e.g. a whole method I got returned from ChatGPT) and paste it into my code, the indentation must be corrected:
def message(self, message, filler="-", newline=True):
"""
Display a message in the console, formatted to fit within the width.
:param message: The message to display.
:param filler: The filler character to use for padding.
:param newline: Whether to print a newline after the message.
"""
max_messagelen = self.width-4
if len(message) > max_messagelen:
message_words = message.split()
In this example, I had dcostrings generated form ChatGPT and wanted to paste them into my function.
I would like my input code to be adjusted to the indentation of the line where I am pasting it.
So every following line of the pasted code should get the same amount of indentation, a pretty straightforward problem.
I would like to bring this to a custom shortcut, so I still have normal pasting and correct-indent-pasting.
2
Answers
You can right-click in the file then choose Format Document With…
In the list, choose Python will format your codes.
You can change your default Formatter to Python, or add the following codes to your
settings.json
:In this way, when you use shortcuts Ctrl+S, file will be formatted as you want.
If you have Visual Studio Code:
Go to the Extension bar and install Python Paste And Indent.
Also the only thing to do for one your showing is to delete an indent.