Dumb question: how do I correctly paste indented code?

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.

1 Like

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:

  1. Auto-indent on paste:

    • Go to File > Preferences > Settings
    • Search for “editor.formatOnPaste”
    • Enable this setting
  2. 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
  3. Install the “Paste and Indent” extension:

    • This adds a command to paste and automatically indent the code
  4. 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
  5. 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
  6. Use the “indent-rainbow” extension:

    • This visually highlights indentation levels, making it easier to spot and fix indentation issues
  7. Configure Python-specific settings:

    • In settings, search for “python.formatting.provider”
    • Set it to “autopep8” or “black” for automatic Python formatting
  8. Use the “Better Paste” extension:

    • This extension provides more intelligent pasting behavior, especially for indented code
  9. 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”
  10. 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."

1 Like

Try “apply” instead of copy + doing it manually. Works 95% with gpt4-turbo.