This might be more of a vs code question but I’ve searched and cant figure out how to get this to work. I have attached a gif below
As you can see, if I paste the code block at the indent level it’s meant to be at, it doesn’t properly indent. If I paste it at the start of the line, it’s right but I have to manually tab the entire bloc of code that 1 indent.
This always annoys me (same in VSC), this is the advice Claude 3.5 gave me:
"Dealing with indentation when pasting code in Visual Studio Code (VSC) can be frustrating, especially when working with Python. Here are some tips and tricks to help manage this issue:
Auto-indent on paste:
Go to File > Preferences > Settings
Search for “editor.formatOnPaste”
Enable this setting
Use the “Paste and Indent” command:
Instead of using Ctrl+V, use Shift+Alt+F after pasting
This will format the pasted code according to the file’s indentation
Install the “Paste and Indent” extension:
This adds a command to paste and automatically indent the code
Use multi-cursor editing:
Paste your code
Use Alt+Click to add cursors at the start of each line
Press Tab to indent all lines at once
Use block selection:
Paste your code
Hold Alt and drag to select a vertical block at the start of the lines
Press Tab to indent the entire block
Use the “indent-rainbow” extension:
This visually highlights indentation levels, making it easier to spot and fix indentation issues
Configure Python-specific settings:
In settings, search for “python.formatting.provider”
Set it to “autopep8” or “black” for automatic Python formatting
Use the “Better Paste” extension:
This extension provides more intelligent pasting behavior, especially for indented code
Learn to use the “Reindent Lines” command:
After pasting, select all the pasted code
Use Ctrl+Shift+P to open the command palette
Search for and run “Reindent Lines”
Use keyboard shortcuts for indentation:
Select the pasted code
Use Ctrl+] to indent or Ctrl+[ to outdent
Remember, you can always undo (Ctrl+Z) if the paste doesn’t come out right, and then try a different method.
Lastly, if you’re frequently copying code from external sources, consider using a tool like “Prettier” or “Black” to automatically format your entire file, which can help maintain consistent indentation throughout your project."
Since I find Apply to take too long on many of my files I often apply code manually, but manually selecting a larger block of code where the top is off-screen is annoying. Not a big deal once, but over and over again? It gets old.
Anyway, this is what helped for me (I’m a VS Code noob, fwiw):
Install “Prettier” extension (I had to go with “SimonSiefke.prettier-vscode” since I’m working with C#. There seems to be a more popular “Prettier” extension with broad language support that just doesn’t, apparently, include C#). You can install extensions via File → Preferences → Extensions
Enable it in settings… File → Preferences → VS Code Settings → search for “editor.formatOnSave” and enable that and search for “editor.defaultFormatter” and set it to Prettier
Now, after pasting or making any other code changes, just hit Ctrl-S to save, and your entire file including anything you pasted in will be automatically formatted