39

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;
    }
?>
3
  • Meta related
    – Michel
    Commented Feb 21, 2014 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
    Commented Feb 21, 2014 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? Commented Feb 21, 2014 at 15:55

2 Answers 2

30

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?

5
  • 3
    Amazing answer. Really complete. Thanks for the instruction as well, and the step by step (I needed that).
    – Michel
    Commented Mar 6, 2014 at 19:38
  • 1
    For those having trouble installing the "Indent By Fold" plugin, see here: deano.me/2013/07/…
    – zeke
    Commented Oct 21, 2014 at 22:56
  • 1
    Where exactly can I download it?
    – User
    Commented Feb 3, 2015 at 3:58
  • 3
    Just use the Plugin Manager. Also, you can download it from code.google.com/p/indentbyfold Commented Feb 18, 2015 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
    Commented Jan 19, 2016 at 7:20
3

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.)

1
  • Updates on year 2020: 1. JStool use Ctrl - Alt - M combination. - IndentByFold worked also, but was slightly worst for my case. - UniversalIndentGUI was not found. - Auto Detect Indentation - I didn't figure out how to get this to work. Commented Feb 10, 2020 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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