How to Indent a Block of Python Code in Notepad++?

How to Indent a Block of Python Code in Notepad++?

While coding in Matlab, I had the option of smart indenting my code using the shortcut Ctrl+i. Is there a similar option for Python using Notepad++?

Specifically, I am looking for a solution to the following problem:
I want the block

line 1
line 2
line 3

to be converted to

    line 1
    line 2
    line 3

On a side-note, I found this to be surprisingly easy with SE editor (keyboard shortcut: Ctrl+k). I want this feature to be available for other languages as well (in my experience good indentation hasn't killed anyone, regardless of the language).

3

2 Answers

Just select the text you want indented and hit Tab. To un-indent, select the text and hit Shift+Tab.

If you're in "Normal text file" mode, this will give you tabs (\t characters). If, however, you're in Python mode (which you enter either by saving as a .py file or by selecting Python from the Language menu), it should default to using four spaces instead of tabs. To configure the way tabs work yourself, go to Settings → Preferences → Tab Settings (for Python, this should default to "Tab size: 4" with "Replace by space" checked).

2

Select all 3 lines and hit the Tab key. To undo this action you can hold down Shift while pressing the Tab key.

Pro Tip: As long as you have at least 2 lines partially selected, you can tab in the entire block.

Example:

If you want to change tab settings there are two locations in Settings -> Preferences

  1. Under Tab Settings: You can change the tab size and you can check off the Replace by space option if you want NPP to use spaces for tabs.
  2. Under MISC: you can check-off Auto-indent in case you have that option turned off.
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.