I'm looking for a plugin that can indent code (HTML, CSS, PHP, ASP, etc.) in Notepad++.

In Visual Studio (and a few other Microsoft editors), you can auto format a document with a simple Ctrl + K, Ctrl + D. This inserts line breaks and tabbing automatically.

Is there a similar feature in Notepad++?

Requeriments

  • Work with Notepad++
  • Windows 7+
  • Must implement shortcut for it
  • No too "heavy" (not more than Notepad++ itself)

Optional

  • May have other features
  • Able to define how to indent each language

I looked and couldn't find any "inbox" feature that covers it. I also gave a try using:

  • Code alignment for Notepad++. There are good tools in the Code Alignment, but not what I'm looking for. Below what I'm searching.
  • TextFX didn't the job as I expected, maybe I didn't use as I should have?

    Screenshof TextFX’s menu


// Some examples using PHP, but I want it to be used in other languages like ASP as well as CSS and HTML
<?php
    public function x()
    {
        $foo = 'test';
            $bar = 1;
return $foobar;
    }
?>

<?php
    public function x()
    {
                $foo = 'test';
                $bar = 1;
                return $foobar;
    }
?>

// What I'm looking for
<?php
    public function x()
    {
        $foo = 'test';
        $bar = 1;
        return $foobar;
    }
?>
  • Meta related – Michel Feb 21 '14 at 12:55
  • You might wish to see this answer, which mentions the appropriate plugins. Found by a search for "notepad++ indent" (the only hit, btw). – Izzy Feb 21 '14 at 14:00
  • You can highlight the lines and press tab to indent a big chunk at once. (And shift+tab to undo/remove) Is that not what you're looking for then? – 3ventic Feb 21 '14 at 15:55
up vote 26 down vote accepted

Why don't you try the Indent By Fold plugin?

Screenshot of the menu 'Plugins' → 'Indent By Fold'

Here's your PHP code indented by fold:

Screenshot of a PHP file before and after

(The image above is not assembled from two separate images! Notepad++ has the ability to clone its tabs in a new view.)

You can access a screencast demo for the 'Indent By Fold' plugin. There's no separate plugin for the 'auto complete' feature in the video; for most languages there are defined already XML files with keywords:

Screenshot of a XML file

Now, about the 'Code Alignment' plugin: just use it only if you are not satisfied with the 'Indent By Fold' results!


Here's how you can define a shortcut for the the indent operation:

Screenshot of the Shortcut Mapper

The Ctr + K and Ctrl + D shortcuts key are already "taken" (by 'comment code' and 'duplicate selection'), therefore I've choosed another combination. But everyone can re-map all the commands according to own needs.


If anytime you find a language with a "weak" 'code formatting / folding' you can take the lead, and proceed in defining your own folding and coloring rules for keywords, comments, numbers, operators and delimiters:

Screenshot of 'Folder & Default' menu

See how beautiful 'Indent By Fold' works when I press ALt + K?

The vbproc keyword is underlined because the 'DSpellChecker' plugin is active.

Here are a large amount of UDLs (User Defined Languages) for Notepad++. To better understand how to use this feature I recommend you to read the UDL 2.0 online documentation. It was a time when folding was possible only for single words like "BeginSub" and "EndSub". Now it is easier because folding can be done using expressions, as you can see in my My better ASP example. Even now, the UDL cannot address every imaginable situations (there is a work in progress called UDL 3). But can we blame the Notepad++ developers for not achieving perfection with this free and simple, yet wonderful utility?

  • 2
    Amazing answer. Really complete. Thanks for the instruction as well, and the step by step (I needed that). – Michel Mar 6 '14 at 19:38
  • 1
    For those having trouble installing the "Indent By Fold" plugin, see here: deano.me/2013/07/… – zeke Oct 21 '14 at 22:56
  • 1
    Where exactly can I download it? – User Feb 3 '15 at 3:58
  • 2
    Just use the Plugin Manager. Also, you can download it from code.google.com/p/indentbyfold – Sorin Călinică Feb 18 '15 at 12:59
  • Indent By Fold can work wonders in many cases, but it won't work at all in some cases, e.g.: <form><input><input><input><input><input></form> – thdoan Jan 19 '16 at 7:20

Here are most of the plugins, to format your code.

  • JStool (JSmin)

  • UniversalIndentGUI (Enable 'text auto update' in plugin manager → UniversalIndentGUI, Shortkey = CTRL+ALT+SHIFT+J)

  • TextFX (Shortkey = CTRL+ALT+SHIFT+B or TextFX > TextFX Html Tidy > Tidy: reindent XML; TextFX has the benefit of wrapping long lines, which XML Tools does not do, but doesn't indent those new lines correctly.)

  • XML Tools (customized plugin for XML; Shortkey = CTRL+ALT+SHIFT+B or XML Tools > Pretty print [Text indent] . XML Tools complements TextFX by indenting the newly wrapped lines nicely.)

protected by Community Oct 21 '16 at 12:51

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.