How do I unindent blocks of code in Python? [duplicate]
Asked Answered
S

2

13

I am using Python 2.7.10 and am having trouble trying to unindent a block of code. Surely there is some sort of shortcut, instead of having to backspace every individual line?

From the Python Shell, I opened a new file (.py) and that's where I am writing the code.

Shift + Tab does not work.

Susie answered 22/9, 2015 at 7:27 Comment(6)
What do you mean by the Python Shell? If I run python from the terminal I get a shell in which I can run individual commands but there is certainly no support for editing a file. It's not meant for that. If you want to edit a file nicely use an IDE such as PyCharm. If you want to edit in the terminal use your preferred text editor such as vim.Keeling
It's not clear what you mean by "in Python". If you mean IDLE, then by default Ctrl-[ dedents - if you go through the menus, they show the shortcuts.Medicate
From IDLE, if you go File>New File, it shows a blank document, looks exactly a .txt, but it's a .py file. I choose to use this over something like PyCharm as a form of discipline. I feel that if I use PyCharm I will get into the habit of relying too much on its built in features and autocompletes... Thanks for your help, it is Ctrl + [.Susie
So you do mean IDLE? Then mention that in the question. Or just search for it... https://mcmap.net/q/903478/-how-do-i-unindent-using-idle-python-gui/3001761. And IDLE's hardly asceticism, it does have autocomplete!Medicate
I don't think it's IDLE, because when I open IDLE off my desktop it opens the Python Shell. From there, I open a new file, and it's blank. Is this 'IDLE'?Susie
Yes, that's IDLE - note that if you go to About, it tells you as much. IDLE can have multiple windows open, one containing the "Python Shell" (interactive interpreter) and the others containing .py script files. Either way, the point remains that if you look at the options in the menu (Format -> Dedent Region), in common with virtually every other program, it lists the shortcuts for you there. Note also that I do not get rep for edits; that only applies to users with <2k, and that you need to add @username for others to get notifications of your comments.Medicate
E
14

It is Ctrl + [ in IDLE. You can change it to your favorite Shift + Tab in Options -> Configure IDLE - Keys. You need to restart the shell after that.

Elasmobranch answered 22/9, 2015 at 7:44 Comment(0)
D
22

You can unindent using following keys:

In Python IDLE/Spyder: Ctrl + [
In Eclipse: Shift + Tab
In Visual Studio: Shift+ Tab
In PyCharm: Shift + Tab
In Jupyter Notebook: Shift + Tab
Demimondaine answered 30/5, 2016 at 15:38 Comment(0)
E
14

It is Ctrl + [ in IDLE. You can change it to your favorite Shift + Tab in Options -> Configure IDLE - Keys. You need to restart the shell after that.

Elasmobranch answered 22/9, 2015 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.