Formatting code in Notepad++
Asked Answered
A

15

211

Is there a keyboard shortcut to format code in Notepad++ ?
I'm mainly working with HTML, CSS and Python code.

For example:

<title>{% block title %}    {% endblock %}</title>      
 <link rel="stylesheet" href="/media/style.css" type="text/css" media="screen" />
</head>

To:

<head>
  <title>
    {% block title %}
    {% endblock %}
  </title>
  <link rel="stylesheet" href="/media/style.css" type="text/css" media="screen" />
</head>


I remember Visual Studio doing it with Ctrl+K+D and NetBeans having the feature too but can't find it within Notepad++, if it can even do it.

Anisaanise answered 3/9, 2010 at 11:58 Comment(3)
I've been using Dreamweaver "Apply Source Formatting" or CRTL + K + D in Visual Studio and format my pages. I've also used VIM but I just never got it right with Notepadd++ because it doesn't like my django tags.Anisaanise
This needs a modern answer. None of these answers work anymore.Cotsen
You need to add related plugin which is accessible through Plugin manger, the point is as of Notepad++ version 7.5, plugin manager is no longer shipped with Notepad++Firdausi
P
131

TextFX -> HTML Tidy -> Tidy: Reindent XML

Remember to have the HTML code selected before you do this.

Pole answered 3/9, 2010 at 12:17 Comment(11)
Hey there, I used that and it works nicely with only HTML. With django tags however seems to just erase them or not format them properly. It's the closest so far. Ta for the tip.Anisaanise
been trying out your suggested method for a while now and it's close but still not quite what I need. I guess the Django tags are what really seem to through the formatter out.Anisaanise
@mongoose_za: Hm, strange that Notepad++ handles those tags differently. Oh well.Pole
NO TEXTFX coming in latest notepad++ and unable to install plugins as well pls helpIdeal
For those who download the new Notepad++. TextFX is not bundled anymore. You'll need to go to Plugins > Plugin Manager > Show Plugin Manager and download TextFX from there. On top of that you'll probably find that TextFX can't find libTidy.dll. To fix that you can do this #6986137 After all of that you should be able to TextFX -> HTML Tidy -> Tidy: Reindent XML as @Pole described.Anisaanise
Please note that you can no longer get the TextFX plugin with the Unicode executable of Notepad++. You have to download the ZIP, and use the ANSI version to get TextFX.Incipient
Seems Notepad++ want to make life difficult. EVEN if you have version 6 or higher you still need to download the zip version 5.9 here. Then copy the tidy folder from unicode\plugins\Config and paste it into your C:\Program Files\Notepad++\plugins\Config folder.Anisaanise
In my case there were no plugins listed under the 'Available' tab of the 'Plugin Manager'. To fix it I had to amend firewall rules for Notepad++ and the 'Plugin Manager' then displayed the full list of downloadable plugins as expected.Eponymous
The Tidy2 plugin will do the job, Plugins > Plugin Manager > Show Plugin Manager then add Tidy2.Arkhangelsk
@George How is it possible to use Tidy2 for Notepad++ v6 64bit for windows?Ervinervine
I downloaded JSTool plugin for JSON lint. Good.Odontoid
E
79

there is such a plugin as UniversalIndentGUI, it can be installed right from the plugin manager and has possibilities to reindent the most used programming languages.

Elementary answered 17/4, 2012 at 8:53 Comment(7)
+1 This works great! Install the UniversalIndentGUI plugin and press Ctrl+Alt+Shift+J to format.Tuberose
Thanks this works great. Is there a way to automatically format the code when you save? Right now, when I check enable autoupdate text, it doesn't update when I save or exit the file.Harkness
How could I prevent it to make to tag in upper case ?Oui
It seems worth noting that "the plugin manager" is itself a plug-in, yes? Being unfamiliar with Notepad++, it took a fair amount of failed menu searching followed by Googling to understand this. Its installation process needs to be factored into the "simplicity" of this solution if one is starting from scratch.Closer
though a plugin, it comes preinstalled in default Notepad++ installersElementary
It did not come preinstalled in my version, and I see that in Win7, one must do manual tweaks to get it to run: brotherstone.co.uk/npp/pm/#install I even had to create the plugins/ and updater/ directories as administrator, and it took quite a while to learn this.Closer
this doesn't work... Where is the format/auto-indent command? Nothing works. Nothing will auto-indent either PHP or JS on this day 2017-05-29. Can anyone explain what's going wrong and why the simple business of automatically indenting PHP and JS code is proving such a b*tching pain in the ***?Masturbate
A
23

If you go to TextFX menu and go to TextFX Edit, you will see a menu item Reindent C++ Code.

That will also format C# code.

Artistic answered 11/8, 2011 at 16:35 Comment(3)
It doesn't indent correctly in PHP. Sometimes it makes 2 or 3 tab spaces instead of one, and in some situations it makes none, when it should.Debose
I don't have a TextFX menu... what are you on about?Masturbate
You will need to install it through the plugins manager. It's called TextFX Characters.Artistic
W
20

ANSWER AS OF June 2019

Install the XML Tools plugin from the Plugin Admin (in Notepad++ 7.7 at least)

Then click Plugins -> XML Tools -> Pretty Print (XML Only with Line breaks)

That did it for me.

Whitnell answered 19/6, 2019 at 15:44 Comment(3)
This is the ONLY solution on this page that worked for me, and it works perfectly!Condyle
It worked for me as well. But This is the Right Step After installing the XML Tools From the Plugins>Plugin Admin: Plugins -> XML Tools -> Pretty Print After installing the XML Tools From the Plugins>Plugin Admin. Try: Ctrl + Alt + Shift + BEquable
It indents empty closed tags correctly, but not void tags, or closed tags with content. <closed></closed> 👍, <void> & <closed>text, CSS, or JavaScript</closed> 👎Jell
P
11

Here is a list of the available shortcuts in Notepad++.

In case your desired functionality is not available, you are able to define own macros and assign them to a custom shortcut (i am not used to use macros).

UPDATE: I will post the shortcuts here in case the link gets invalid:

Shortcut    Command

Ctrl-C  Copy
Ctrl-X  Cut
Ctrl-V  Paste
Ctrl-Z  Undo
Ctrl-Y  Redo
Ctrl-A  Select All
Ctrl-F  Launch Find Dialog
Ctrl-H  Launch Find / Replace Dialog
Ctrl-D  Duplicate Current Line
Ctrl-L  Delete Current Line
Ctrl-T  Switch the current line position with the previous line position
F3  Find Next
Shft-F3 Find Previous
Ctrl-Shft-F Find in Files
Ctrl-F3 Find (volatil) Next
Ctrl-Shft-F3    Find (volatil) Previous
Ctrl-Shft-I Incremental Search
Ctrl-S  Save File
Ctrl-Alt-S  Save As
Ctrl-Shft-S Save All
Ctrl-O  Open File
Ctrl-N  New File
Ctrl-F2 Toggle Bookmark
F2  Go To Next Bookmark 
Shft-F2 Go To Previous Bookmark
Ctrl-G  Launch GoToLine Dialog
Ctrl-W  Close Current Document
Alt-Shft-Arrow keys or Alt + Left mouse click   Column Mode Select
F5  Launch Run Dialog
Ctrl-Space  Launch CallTip ListBox
Alt-Space   Launch Word Completion ListBox
Tab (selection of several lines)    Insert Tabulation or Space (Indent)
Shft-Tab (selection of several lines)   Remove Tabulation or Space (outdent)
Ctrl-(Keypad-/Keypad+) or Ctrl + mouse wheel butto  Zoom in (+ or up) and Zoom out (- or down)
Ctrl-Keypad/    Restore the original size from zoom 
F11 Toggle Full Screen Mode
Ctrl-Tab    Next Document
Ctrl-Shft-Tab   Previous Document
Ctrl-Shft-Up    Move Current Line Up
Ctrl-Shft-Down  Move Current Line Down
Ctrl-Alt-F  Collapse the Current Level
Ctrl-Alt-Shft-F Uncollapse the Current Level
Alt-0   Fold All
Alt-(1~8)   Collapse the Level (1~8)
Alt-Shft-0  Unfold All
Alt-Shft-(1~8)  Uncollapse the Level (1~8)
Ctrl-BackSpace  Delete to start of word
Ctrl-Delete Delete to end of word
Ctrl-Shft-BackSpace Delete to start of line
Ctrl-Shft-Delete    Delete to end of line
Ctrl-U  Convert to lower case
Ctrl-Shft-U Convert to UPPER CASE
Ctrl-B  Go to matching brace
Ctrl-Shft-R Start to record /Stop recording the macro
Ctrl-Shft-P Play recorded macro
Ctrl-Q  Block comment/uncomment
Ctrl-Shft-Q Stream comment
Ctrl-Shft-T Copy current line to clipboard
Ctrl-P  Print
Alt-F4  Exit
Ctrl-I  Split Lines
Ctrl-J  Join Lines
Ctrl-Alt-R  Text Direction RTL
Ctrl-Alt-L  Text Direction LT
F1  About
Parlin answered 3/9, 2010 at 12:2 Comment(3)
I don't see a shortcut option there that formats code. As for a macro I have no idea how I'd go about that. Thanks anyway.Anisaanise
macros are described here: #362944Parlin
Haha all that and no shortcut for the question? Why post it? -1Candice
N
10

The latest plugin is tidy2, which can be installed through Plugins>Plugin Manager>Show Plugin Manager.

I suggest editing config 1 and setting quote-marks: no, especially if you have script that makes use of quotes.

Also, tidying more than once can result in inserting ampersands the first time and then replacing the ampersands the second time. You may want to play with the config to get it to where you need it.

Naze answered 13/4, 2013 at 5:2 Comment(0)
O
7

For JavaScript Formatting I use Notepad ++ JSMin Plugin.Quite Handy

Octant answered 7/6, 2012 at 7:43 Comment(1)
As of 2023 JSMin is the best answer for JS, its built into the plugins list, install any time. Select the region, click JS Format from the plugin menu.Reyesreykjavik
D
5

This isn't quite the answer you were looking for, but it's the solution I came to when I had the same question.

[update - I'm using vscode now for everything. I think it works better in a lot of cases.]

I'm a pretty serious Notepad++ user, so don't take this the wrong way. I have started using NetBeans 8 to develop websites in addition to Notepad++ because you can set it to autoformat on save for all your languages, and there are a ton of configuration options for how the formatting looks, down to the most minute detail. You might look into it and find it is a worthy tool to use in conjunction with notepad++. It's also open source, completely free, and has a bunch of plugins and other useful things like automatically compiling Sass if you use that too. It's definitely not as quick as NP++ so it's not great for small edits, but it can be nice for a long coding session.

Dorkus answered 23/7, 2014 at 2:40 Comment(3)
Definitely not an answer to this questionSlipway
Is it possible to auto format PHP inside Netbeans? Do I need some additional plugin to do that? Thanks.Hajji
Yes Netbeans autoformats PHP, but you probably need to make sure that you have the php version, or at least have the php plugin installed. On windows the key combo to autoformat is Alt+Shift+F.Dorkus
M
4

In Plugins > Plugin Admin > Search 'XML Tools' > Install.

The window will restart and install XML Tools under plugins.

Open your file and click Ctrl + Alt + Shift + B

Mollee answered 2/11, 2020 at 11:11 Comment(0)
H
3

We can use the following shortcut in the latest version of notepad++ for formatting the code

Alt + Ctrl + Shift + B
Hew answered 5/6, 2019 at 12:14 Comment(3)
I installed latest notepad++ but the short cut is not working for me. Can you provide another answer please.Comatose
In which version and on what content type is that shortcut supposed to work? Tried it in Notepad++ v7.7.1 with no effect.Coriolanus
This is a shortcut added by XML Tools.Pewter
B
2

In my notepad++, it seems TextFX needs a perl environment to format HTML files. Tidy2 demands nothing so I think it's more handy.

Boney answered 3/9, 2013 at 0:47 Comment(0)
P
2

If all you need is alignment, try the plugin called Code Alignment.

You can get it from the built-in plugin manager in Notepad++.

Pilcomayo answered 20/2, 2014 at 1:58 Comment(0)
C
1

No. Notepad++ can't format by itself. Formatting can easily be accomplished in many IDEs like Eclipse, NetBeans, Visual Studio [Code].

Cotsen answered 11/4, 2019 at 19:30 Comment(3)
Wrong answer, into the embedded plugin manager of Notepad++ can install and activate the functionality. IDE are not required.Draconic
"by itself". Have fun trying to get Notepad++ to format when you don't have access to install plugins.Cotsen
@Draconic , please provide the answer in detail.Comatose
B
-1

NPP+ v7.9.1 with the latest version of XMLTools can't format exported VBA code from Office 2016/2019 Word. It puts all the code on the same line since it strips the CRLF out. Moreover, when you enable "auto validation" it errors out on the first VBA line that is, Attribute VB_Name = "The VBA module name". So any of the xml tool validations apparently can't be used for VBA validation.

Beliabelial answered 17/11, 2020 at 13:47 Comment(0)
M
-2

Another Potential Solution

Okay, so it's been 12 years since this question was asked. I think that I've found a another acceptable solution, that is perhaps better for formatting files, using Prettier.

What is Prettier?

Prettier is described as: "an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary."

Prettier Supports the Following Languages

  • HTML
  • CSS, Less, and SCSS
  • JavaScript
  • TypeScript
  • JSX, Angular, Vue
  • JSON, Markdown, YAML

And a few others.

If You Want to Use Other Languages, You Can Either Use:

  1. Plugins made by the community to Prettier, which allow support for other formats such: Java, Rust, PHP, SQL, and others.
  2. Non-Prettier formatters supporting other languages such as clang-format (npm version) (C / C++ / Java / JavaScript / JSON / Objective-C / Protobuf / C#), or Black (Python).

How To Install

  1. You're going to need to install Node.js for this tutorial.

Note: There may be other work arounds for using the subsequent mentioned software without using Node.js. But to keep this tutorial simple and the fact that Node.js already carries widespread familiarity among developers, I'm going to be using this software without explaining how to install it.

  1. After installing Node.js, install prettier globally to your system, allowing yourself to run prettier anywhere from the command line.
# NPM is the package manager used by Node.js
# And --global allows you to use prettier anywhere on your local computer.

npm install --global prettier

# To install other plugins (example):
npm install --g prettier-plugin-rust prettier-plugin-java

# To Install clang-format using NPM
npm install -g clang-format

# Usage Examples
prettier --write jquery-min.js # Formatting a JavaScript file
clang-format -i Test.cpp # Formatting C++ file
  1. On Notepad++, install a plugin called NppExec from Menu> Plugins Admin...> Available> NppExec.
  2. You can now run NppExec by either pressing F6 or by clicking Menu > Plugins > NppExec > Execute NppExec Script...
  3. Now you should have a window pop up, giving you the option of executing a command.
  4. You can either use prettier on a (1) specific file or (2) all files in a folder or (3) all files in a directory recursively.
  5. You can always reload a file after it's better overwritten by Prettier using Ctrl+R, and enabling the following setting: Menu > MISC. > Update silently.

Commands and Screenshots of NppExec + Prettier.

REM Using Prettier on Current File (use cmd or powershell /c)
cmd /c prettier --write "$(FULL_CURRENT_PATH)"

REM Using Prettier on Current Folder (a single asterisk)
pwsh /c prettier --write "$(CURRENT_DIRECTORY)\*"

REM Using Prettier on Current Directory Recursively (double asterisk)
pwsh /c prettier --write "$(CURRENT_DIRECTORY)\**"

Install NppExec Plugin

Install NppExec


Running NppExec Plugin

Running NppExec Plugin


Executing Commands on NppExec

Executing Commands on NppExec

Mews answered 13/11, 2022 at 5:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.