197

We always write code like this formal:

void main(){
  if(){
    if()
    }

Alt text

But when I use Notepad++, the display is:

void main(){
if(){
if()
}

Alt text

How do I use Notepad++ to auto indent?

Thanks to Jonathan, I have set it, but it does not take any effect. The snapshot is below:

Alt text

I am using Notepad++ version 5.1.3.

Community
  • 1
  • 1
Turion
  • 9
  • 2
  • 3
  • 5

16 Answers16

142

Notepad++ will only auto-insert subsequent indents if you manually indent the first line in a block; otherwise you can re-indent your code after the fact using TextFX > TextFX Edit > Reindent C++ code.

scronide
  • 12,012
  • 3
  • 28
  • 33
  • 1
    If your version is missing the DLL you can get it from the ZIP 5.9.0 version. Some of the later versions did not include this DLL. http://notepad-plus-plus.org/download/v5.9.0.html – Fostah Mar 09 '12 at 18:31
  • 2
    I actually found this while looking for a way to format javascript code. Turns out, this works for .js files as well! – sacredfaith Apr 19 '12 at 15:57
  • 7
    @Fostah: you can also install the "Text FX Character" via >Plugins>Plugin Manager>Show Plugin Manager – Adriano Jan 17 '14 at 14:37
  • @scronide, Is there a shortcut key for this? – Pacerier Aug 07 '16 at 17:08
  • Note that despite the name, it absolutely also correctly re-indented the javascript code I was trying to fix the indentation of. No reason I would expect it not to, just mentioning, since the name *is* rather specific. Dunno why the author of that library felt he had to give special preference to C++... :D – neminem Dec 04 '17 at 17:08
  • It doesn't work with User-Defined Languages, I'm trying to use it with GLSL which is mostly C-like. – Beyondo Jun 16 '18 at 18:06
  • 1
    So how does one get started going `TextFX >` ? I've checked all the menus, plugins, and help file and I don't see anything that looks like that. I can tell Notepadd++ is powerful, but I barely use it because it's so complicated it's almost a no-starter. – Travis Bemrose Dec 18 '20 at 18:01
105

If the TextFX menu does not exist, you need to download & install the plugin. Plugins->Plugin Manager->Show Plugin Manager and then check the plugin TextFX Characters. Click 'install,' restart Notepad++.

In version Notepad++ v6.1.3, I resolve with: Plugin Manager->Show Plugin Manager** and then check the plugin "Indent By Fold"

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
Moose5
  • 1
  • 1
  • 2
  • 2
  • 3
    Indent by fold is awesome. The fact that it works in arbitrary formatting (*XML, C/++/#, etc.*) is great. Only problem with it is the "undo" stack. When used, if you wish to undo to a point before when you indented, you have to undo through **every** line re-indentation, as it doesn't get added to the stack as a single command. Basically, be careful when working with *long* files. – Dan Lugg Jan 26 '13 at 14:21
  • Hi @Dan the undo problem seem to have been fixed in newer version of indent by fold... – TOPKAT Apr 09 '17 at 14:13
  • 3
    Yes, It doesn't exist and there's no Plugin Manager. – Beyondo Jun 16 '18 at 17:22
31

There seems to be an option for auto-format of XML at least. It can be found at TextFX -> TextFX HTML Tidy -> Tidy: reindent XML.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abhishek
  • 1
  • 2
  • 2
29

Most developers of text editing programs misuse this name (auto-indent). The correct name is "maintain indentation". Auto-indent is what you actually want, but it is not implemented.

I would also like to see this feature in Notepad++.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
22

In the latest version (at least), you can find it through:

  • Settings (menu)
  • Preferences...
  • MISC (tab)
  • lower-left checkbox list
  • "Auto-indent" is the 2nd option in this group

[EDIT] Though, I don't think it's had the best implementation of Auto-indent. So, check to make sure you have version 5.1 -- auto-indent got an overhaul recently, so it auto-corrects your indenting.


Do also note that you're missing the block for the 2nd if:

void main(){
  if(){
    if() { }  # here
  }
}
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • 8
    Note that this auto-indent setting in Notepad++ assumes you do the first indent in a block manually; it doesn't auto-format the code for you. – scronide Jan 05 '09 at 07:05
  • I got your meaning just now,please forgive my poor Eng... My mother language is Chinese... – Turion Jan 05 '09 at 12:49
16

You can use 'Indent by fold' plugin. Install it from the plugin manager. It works fine for me.

Aki143S
  • 809
  • 7
  • 5
  • 2
    Indent by fold is great, except that it only indents by fold ;) In situations where you've indented a list initialization, unless the language uses a folding character to delimit the initializer, it'll end up un-indenting. An example is a multi-line array initialization in PHP using `array( ... )`, where `()` are non-folding. – Dan Lugg Mar 21 '13 at 12:23
6

For those who using version 7.8.5, the Auto-indent settings is now located at "Settings" -> "Preferences..." -> "Auto-Completion".

enter image description here

ooi18
  • 130
  • 2
  • 5
5

In the 6.6.8 version I installed the NppAutoIndent plugin from Plugins > Plugin Manager > Show Plugin Manager. Then I selected the Smart Indent option in Plugin > NppAutoIndent. Hope this helps.

TheRS
  • 1
  • 1
  • 1
5

For those who use Notepad++ v6.8.1 and later, the auto-indent setting now is placed in menu SettingsPreferencesMISC.Auto Indent.

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Luke
  • 1,623
  • 3
  • 24
  • 32
3

A little update: You can skip the TextFX Plugin and just use Tidy2. Here you can configure your own formating-rules for different types of codes. Easy to install and remove within

Notepad++ > Plugins > Plugin Manager > Show Plugin Manager

and just search for Tidy2 and install it. Done.

Sven Ya
  • 552
  • 1
  • 5
  • 12
3

You can add "Text FX Character" plugin. Download it from https://sourceforge.net/projects/npp-plugins/files/TextFX/ and install it via

Plugins → Plugin Manager → Show Plugin Manager.

Then you will find a new "TextFX" menu:

Menu TextFX → TextFX Edit → Reindent C++ code

enter image description here

To add shortcut for Indentation.

Click on SettingsShortcut Mapper... set the shortcut key you prefer. I prefer Ctrl + =.

Enter image description here

jumpjack
  • 841
  • 1
  • 11
  • 17
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
2

Try the UniversalIndentGUI plugin for Notepad++. It re-indents code based on some parameters. It worked well for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
imriss
  • 1,815
  • 4
  • 31
  • 46
  • 3
    Note that the link is direct to the download. The website is: http://universalindent.sourceforge.net/. Also, this appears to be a GUI for configuring a reformating tool, rather than automatically formatting the text correctly to start with. – Martin Bonner supports Monica Oct 08 '13 at 08:26
2

This may seem silly, but in the original question, Turion was editing a plain text file. Make sure you choose the correct language from the Language menu

kurdtpage
  • 3,142
  • 1
  • 24
  • 24
0

Install Tidy2 plugin. I have Notepad++ v6.2.2, and Tidy2 works fine so far.

0

First download plugin manager this link then unzip the zip folder and copy this inside your program/ notepad++ folder . then restart your notepad++. then you see plugin manager inside plugin menu . then click plugin manager then click show plugin manager . It shows all your plugin list . from the list in bottom find XML tools , checked it and install it. then restart your notepad++. After open a document then plugins/xml tools/pretty plain(indent text) then enjoy.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
tapos ghosh
  • 2,114
  • 23
  • 37
-1

Try to save the file before, then it will indent.

Sh13
  • 13
  • 1
  • 6