VS Code integrated terminal is messing up the clipboard python code when pasting

Viewed 715

In a clean installation of VS Code (1.59.1) the code pasted in the integrated terminal has a different indentation than the one available in the editor. I tried changing the parameter "editor.formatOnPaste": true and there was no improvement. This problem does not exist when I copy the same content and paste it into the powershell or command prompt. Any ideas on how to solve this?

error description

enter image description here

4 Answers

I just tried to reproduce your problem on Linux (bash, tmux, zsh, etc.) and Windows (PowerShell, Command Prompt, etc.) without much success.

I am willing to bet it is a problem with the 'ipython' you are running in PS.

Have you tried installing a different Python version just to see if that is the issue?

I think you have to import and initialize ipython or you have given an extra space.

I couldn't reproduce your problem myself, but here are some solutions I think they might work.

I believe the source of this problem may have something to do with Ipython's automatic indentation when working with the vscode version you are using. You could use Ipython's built-in magic command: %autoindent to turn off the automatic indentation.

Just run the magic command: %autoindent and you will see in your prompt the message:

Automatic indentation is: OFF (You can run it again to turn it back on if you want)

After that you should be able to copy and paste your code (keeping the original indentation) and Ipython won't add new indentation.

Another solution is using the magic command: %paste This command paste and execute the code from clipboard (here you can find more information about this command).

If you want to edit the code in the prompt before executing it, remember you can use: %cpaste instead.

Considering the fact that no one can reproduce the problem, I suspect you might have inconsistent space/tab (or other) characters with the file itself.

To check if this is the case, try making a new python file in VS Code and rewriting the original code in it (do not copy-paste). If it works, it was just an issue certain characters in the file: this answer may help for larger files where re-writing is not an option.

If this didn't work, try now making another new python file in a different text editor and rewriting the original code in it (again, do not copy-paste). Run it through the CMD or powershell. If this doesn't work, it's not VS Code's fault (may be an issue with your python/ipython configuration/installation).

If it still hasn't worked, you might want to try running the newest file (created in another text editor) through VS Code, or running the original file you have through the CMD/powershell (not copy-paste), and diagnosing from there.

Related