I use the following method to comment out code using the keyboard. It works on single and multiple lines and you don’t need to select the whole line to comment it out.
I used the examples provided with Pythonista to create an extension script:
import editor, console
script = editor.get_text()
sel_start, sel_end = editor.get_line_selection()
selected_text = script[sel_start:sel_end].splitlines()
r = '\n'.join('#' + i for i in selected_text)
editor.replace_text(sel_start, sel_end, r)
From there, you can add the script as an IDE extension:
click the wrench icon at the top-right of the window. The bottom section of the popup menu that appears is reserved for extensions. Click “Edit” then the “+” icon and browse and add the script you created in the previous step.
You can comment out lines by selecting the wrench icon and clicking the newly added icon. You can also run the extension using your keyboard if it has the Apple “Command” and “Option” keys available. Hitting Command + Option + a number on the keyboard will trigger that script. So if the extension you added is the only extension script in your IDE, you would hit Command + Option + 1.
It should only take a few minutes to set up and then you’re good to go.