Search code examples
pycharmindentationauto-indent

Add spaces to several lines PyCharm


Here is a my problem with a minimal reproducible example:

    def additional():
        df_additional_data = pd.read_excel("something.xlsx")
    
        for _, row in df_saga.iterrows():
            req = """
                  UPDATE table_name
                SET column1 = value1, column2 = value2, ...
                WHERE condition;
                  """

I want to add two spaces to both those lines (the one starting with SET and the one starting with WHERE). I cannot use TAB because it adds 4 spaces. I cannot use Ctrl Alt I because it's inside """ """ and it doesn't work inside those. I know you can do it by hand, but in my case I have over 200 lines in the """ """ string. And I don't want to hire an intern just to add spaces.

I'm using PyCharm 2021.3.3 (Community Edition)

Build #PC-213.7172.26, built on March 16, 2022
Runtime version: 11.0.14.1+1-b1751.46 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 11 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 2030M
Cores: 16
Non-Bundled Plugins:
    net.seesharpsoft.intellij.plugins.csv (2.18.2)
    com.chesterccw.excelreader (2021.3.2)
    ru.meanmail.plugin.requirements (2022.3-2021.3)

Solution

  • Since formatters generally try not modifying the contents inside strings too much, this would be tricky in an automated way without seeing the actual code, would be complicated, and would probably have hiccups (IMO).

    If the only reason using TAB doesn't solve your answer is because it uses 4 spaces, you can temporarily change it to using 2 spaces To do this you will need to do the following:

    1. Settings ->Editor -> Code Style -> Python -> indent -> 2.
    2. Settings ->Editor -> Code Style -> Uncheck Detect and use existing file indents for editing