Formatting code in Notepad++ - html

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.

TextFX -> HTML Tidy -> Tidy: Reindent XML
Remember to have the HTML code selected before you do this.

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.

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.

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.

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

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.

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

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.

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

If all you need is alignment, try the plugin called Code Alignment.
You can get it from the built-in plugin manager in Notepad++.

We can use the following shortcut in the latest version of notepad++ for formatting the code
Alt + Ctrl + Shift + B

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

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

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:
Plugins made by the community to Prettier, which allow support for other formats such: Java, Rust, PHP, SQL, and others.
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
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.
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
On Notepad++, install a plugin called NppExec from Menu> Plugins Admin...> Available> NppExec.
You can now run NppExec by either pressing F6 or by clicking Menu > Plugins > NppExec > Execute NppExec Script...
Now you should have a window pop up, giving you the option of executing a command.
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.
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
Running NppExec Plugin
Executing Commands on NppExec

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.

Related

How to format pasted JSON in IntelliJ / Android Studio

I often need to use a text editor while writing code to paste random notes but especially JSON responses, where I format them using a plugin (for Sublime).
I recently heard about the 'scratch file' feature in IntelliJ / Android Studio which does exactly what I need it to - except I can't make it format JSON I paste in nicely.
How can I make Android Studio format JSON in a scratch buffer file?
I highlight the code and run Command Option L (a short-cut for the Code->Reformat Code menu).
On Windows use Ctrl Alt L.
Note that this only works if the code is well-formed JSON (clear any red squiggles).
You are asking about two seperate things: scratch files and scratch buffers.
When you create a scratch file in IntelliJ you can choose the type of the file (e.g. JSON) that you want to create. Based on file's type, IntelliJ provides code formatting (use Code->Reformat code).
However, scratch buffers are just simple .txt files and the only formatting that can be used is the one associated to .txt format. So, if you put JSON into scratch buffer it won't get formatted with JSON type formatter.
I would encourage you to use scratch files instead of scratch buffers if you want JSON formatting.
More information can be found at IntelliJ's official page https://www.jetbrains.com/help/idea/2016.2/scratches.html.
choose from the menu : code -> reformat code
hotkey On Windows -:
CTRL + Alt + L
Important note- if you have sensitive information in your JSON- NEVER use online tools to parse or beautify it. because this will compromise your organization's and customer's information
If you run the latest version I'd create a new Scratch File of type JSON.
It's really easy, e.g. hit double shift, then search for new scratch file, select JSON as the language, paste your snippet and then use the shortcut that you usually use for formatting any file in intelliJ.
There is a plugin Save Actions that does auto-formatting (also of JSON) every time when I save the file:
select Android Studio > Preferences > Plugins
check Martketplace tab and search for Save Actions (it has a red square-ish icon) and press Install next to it.
At this point you will need to restart your android studio, and then you can enable "Reformat on save" by:
select Android Studio > Preferences > Other Settings > Save actions, in there, select:
✅ General > activate save actions on save
✅ Formatting actions > Reformat File

VS Code, format HTML code with Twig or Swig tags

Is there a way to format correctly HTML in VSCode when we use Twig or Swig tags, like
{% if ... %} {%else%} {%endif%} {% for %} {%endfor%} {% include %}, etc...
For now code formatters remove all line breaks before and after those tags.
Was not able to find an suitable extension for that.. Neither a way to configure the internal code formatter.
I had the same problem and solved installing this extension:
Twig Language
Install it, restart the editor and ff you are on windows you can format with standard ALT + SHIFT + F.
Hope it helps.
I've been struggling SO MUCH with this, and finally found a fully working solution; so wanted to share it here. Follow the steps below and you should be good to go:
Download the Vs Code Extension Prettier Code Formatter. Pay close attention to grab this Prettier Package, as Visual Studio proposes several different Prettier Extension Packages.
In the Visual Studio Editor, go to Settings --> type "format" until the formatting settings pop up, and then select Prettier Code Formatter - esbenp.prettier-vscode as your default formatter. Do this for both the User as well as the Workspace tab, if not automatically done.
Next, you'll need to add the melody plugin used for the proper formatting of twig files using Prettier in Vs Code. This is actually a nodejs package, so you first need to install Node JS. It can occur that you will run into permission issues when installing node packages later on. To try to avoid this at max, install node using a nvm.
Now you can install the melody plugin. To do so, follow the instructions of the linked repository. Note that you previously need to install yarn if you don't have it already installed, via npm install --global yarn.
To hold project-specific formattings within your github repo / project, you can add a .vscode/settings.json file which holds the general prettier configs you're currently using in your project. That's pretty useful to share the prettier formatting settings across developers / repos.
Also add the .prettierrc file to your projects root, with the following content:
{
"printWidth": 80,
"tabWidth": 4,
"plugins": ["./node_modules/prettier-plugin-twig-melody"]
}
This defines the additional use of the above-mentioned plugin. You may adapt the configs according to your needs. You can either put your node_modules folder holding the package within it into your local project, or adapt the path as needed to wherever your melody plugin is located on your local machine.
FYI: The node_modules folder is normally not uploaded to project repos due to its size. Once you install node, you will automatically get that node_modules folder on your local machine. And when you then install the above-mentioned package, it should get installed into that node_modules folder.
Now add the Twig Syntax highlighting package from whatwedo. Now, VS Code should recognize Twig files when you open them. Verify this by opening a .twig file within VS Code and checking in the bottom right corner that the file is recognized as HTML (Twig). This will additionally highlight your swig tags.
To verify that everything's working properly, you can open the prettier terminal by clicking on Prettier at the bottom right in your VS Code. Now write some twig content into a .twig file and trigger the formatter. If no errors were reported in the console, and the console informs that the melody plugin is used for formatting; and of course the code has been properly formatted; you're good to go.
There is a built-in way nowadays; when you try to format a document you get forwarded to the extension page and a search for a formatter for a given file format. For Twig:
category:formatters twig
Twig Language 2 seems to be the go-to VS Code Twig extension with the best formatter as of now.
Something related to this, i did it by installing twig in vs code.
Open VS Code and
Ctrl+Shift+X
And get the twig extension and install it. Thanks.

Notepad++ re indent code? [duplicate]

Is there a way to re-indent a block of code? I'm looking for something similar to Ctrl+Shift+F in Eclipse (Auto-Format/Indent).
To be clear,
I already know how to format XML outside of Notepad++ (Eclipse works fine, as mentioned) so I don't need a bunch of links to other XML-formatting tools.
I'm specifically working with XML and HTML.
Ideally, there's a keybinding as convenient as the one in Eclipse, so I don't have to break my workflow.
I already know about NppAutoIndent - it won't work, as I'm working with XML, HTML and CSS.
Since I upgraded to 6.3.2, I use XML Tools.
install XML Tools via the Plugin Admin (Plugins → Plugins Admin... Then search for "XML Tools", check its box and click the "Install" button).
use the shortcut Ctrl+Alt+Shift+B (or
menu → Plugins → XML Tools → Pretty Print)
In older versions:
menu → TextFX → HTML Tidy → Tidy: Reindent XML.
Install Tidy2 plugin. I have Notepad++ v6.2.2, and Tidy2 works fine so far.
For those who don't know, npp has a lot of support from plugins and other projects. You can download those plugins from SourceForge.
You need XML Tools to format your text in n++
After you have downloaded XML Tools ..
Exit Notepad++
Go To C:\Program File\Notepad++ .... Your N++ installed folder.
Place below files from xml tools which you downloaded in the npp root folder by copy replace
Go To ..\Plugins subfolder and place below downloaded file
Restart and enjoy!!!
Ctrl + Alt + Shft + B to format.
It's been the third time that I install Windows and npp and after some time I realize the tidy function no longer work. So I google for a solution, come to this thread, then with the help of few more so threads I finally fix it. I'll put a summary of all my actions once and for all.
Install TextFX plugin: Plugins -> Plugin Manager -> Show Plugin Manager. Select TextFX Characters and install. After a restart of npp, the menu 'TextFX' should be visible. (credits: #remipod).
Install libtidy.dll by pasting the Config folder from an old npp package: Follow instructions in this answer.
After having a Config folder in your latest npp installation destination (typically C:\Program Files (x86)\Notepad++\plugins), npp needs write access to that folder. Right click Config folder -> Properties -> Security tab -> select Users, click Edit -> check Full control to allow read/write access. Note that you need administrator privileges to do that.
Restart npp and verify TextFX -> TextFX HTML Tidy -> Tidy: Reindent XML works.
Not exactly a solution but a workaround.
Notepad ++ doesn't provide any such feature by default. But you can use some online tools to autoformat text like https://www.freeformatter.com/xml-formatter.html.
I had to update the proxy settings under Plugins -> Plugin Manager -> Show Plugin Manager -> Settings to see any PlugIns in the "Available" list.
After that, installing "XML Tools" was easy and did the requested job as described above.
Just install the latest notepad++ and install indent By fold. On the menu bar select Plugins -> Plugins Admin and selct indent By fold and the install. Works finest
To directly answer the OP, take a look at this guy's site: Thomas Hunter Notepad++ Tidy for XML. Simple steps to follow and you get very nice formatting of your XML right inside NPP. So far the only anomaly I've found is with nested self closing elements EG:
<OuterTag>Text for outer element<SelfClosingTag/></OuterTag>
Will be tidied up to:
<OuterTag>Text for outer element
<SelfClosingTag/></OuterTag>
There may be a way to fix this, but for the time being, it's managed to reduce the number of lines in my document by 300k and this particular anomaly can be worked around.
I'm using Notepad 7.6 with "Plugin Admin" and I could not find XML Tools.
I had to install it manually like #some-java-guy did in his answer except that my plugins folder was located here: C:\Users\<my username>\AppData\Local\Notepad++\plugins
In that directory I created a new directory (named XmlTools) and copied XMLTools.dll there. (And I copied all dependencies to the Notepad++ directory in Program files.)

Set syntax for a specific file name in Sublime Text 2/3

I have a program that uses a file called user.cfg to get its user defined configuration settings. The odd thing is that they chose the syntax for this file to be Tcl (it's not odd that it is Tcl, it's odd they chose the .cfg extension instead of .tcl). So, when I open this file in Sublime Text, it doesn't know what syntax highlighting scheme to choose.
What I would like to do is set the syntax highlighting for user.cfg to Tcl, but not all .cfg files to Tcl.
I have seen this question which is very similar to mine, except in that case the special file name had no extension so Sublime Text knew to assign Ruby highlighting to only that one file. Unfortunately, I have an extension so the solution given there will not work for me.
Is there any known way to get Sublime Text base a highlighting scheme on the full filename?
Take a look at the ApplySyntax plugin.
The previous answer is completely true; however, I thought it would be better to have it here all in one place rather than going on another webpage to find the list of procedure to apply it
Sublime text 3
This is found here
Ensure Package Control is installed. Instructions are found here.
In Sublime Text, press Ctrl+Shift+P (Win, Linux) or Cmd+Shift+P (macOS) to bring up the quick panel and start typing Package Control: Install Package.
Select the command and it will show a list of installable plugins.
Start typing ApplySyntax; when you see it, select it.
Restart to be sure everything is loaded proper.
Enjoy!

How To Auto-Format / Indent XML/HTML in Notepad++

Is there a way to re-indent a block of code? I'm looking for something similar to Ctrl+Shift+F in Eclipse (Auto-Format/Indent).
To be clear,
I already know how to format XML outside of Notepad++ (Eclipse works fine, as mentioned) so I don't need a bunch of links to other XML-formatting tools.
I'm specifically working with XML and HTML.
Ideally, there's a keybinding as convenient as the one in Eclipse, so I don't have to break my workflow.
I already know about NppAutoIndent - it won't work, as I'm working with XML, HTML and CSS.
Since I upgraded to 6.3.2, I use XML Tools.
install XML Tools via the Plugin Admin (Plugins → Plugins Admin... Then search for "XML Tools", check its box and click the "Install" button).
use the shortcut Ctrl+Alt+Shift+B (or
menu → Plugins → XML Tools → Pretty Print)
In older versions:
menu → TextFX → HTML Tidy → Tidy: Reindent XML.
Install Tidy2 plugin. I have Notepad++ v6.2.2, and Tidy2 works fine so far.
For those who don't know, npp has a lot of support from plugins and other projects. You can download those plugins from SourceForge.
You need XML Tools to format your text in n++
After you have downloaded XML Tools ..
Exit Notepad++
Go To C:\Program File\Notepad++ .... Your N++ installed folder.
Place below files from xml tools which you downloaded in the npp root folder by copy replace
Go To ..\Plugins subfolder and place below downloaded file
Restart and enjoy!!!
Ctrl + Alt + Shft + B to format.
It's been the third time that I install Windows and npp and after some time I realize the tidy function no longer work. So I google for a solution, come to this thread, then with the help of few more so threads I finally fix it. I'll put a summary of all my actions once and for all.
Install TextFX plugin: Plugins -> Plugin Manager -> Show Plugin Manager. Select TextFX Characters and install. After a restart of npp, the menu 'TextFX' should be visible. (credits: #remipod).
Install libtidy.dll by pasting the Config folder from an old npp package: Follow instructions in this answer.
After having a Config folder in your latest npp installation destination (typically C:\Program Files (x86)\Notepad++\plugins), npp needs write access to that folder. Right click Config folder -> Properties -> Security tab -> select Users, click Edit -> check Full control to allow read/write access. Note that you need administrator privileges to do that.
Restart npp and verify TextFX -> TextFX HTML Tidy -> Tidy: Reindent XML works.
Not exactly a solution but a workaround.
Notepad ++ doesn't provide any such feature by default. But you can use some online tools to autoformat text like https://www.freeformatter.com/xml-formatter.html.
I had to update the proxy settings under Plugins -> Plugin Manager -> Show Plugin Manager -> Settings to see any PlugIns in the "Available" list.
After that, installing "XML Tools" was easy and did the requested job as described above.
Just install the latest notepad++ and install indent By fold. On the menu bar select Plugins -> Plugins Admin and selct indent By fold and the install. Works finest
To directly answer the OP, take a look at this guy's site: Thomas Hunter Notepad++ Tidy for XML. Simple steps to follow and you get very nice formatting of your XML right inside NPP. So far the only anomaly I've found is with nested self closing elements EG:
<OuterTag>Text for outer element<SelfClosingTag/></OuterTag>
Will be tidied up to:
<OuterTag>Text for outer element
<SelfClosingTag/></OuterTag>
There may be a way to fix this, but for the time being, it's managed to reduce the number of lines in my document by 300k and this particular anomaly can be worked around.
I'm using Notepad 7.6 with "Plugin Admin" and I could not find XML Tools.
I had to install it manually like #some-java-guy did in his answer except that my plugins folder was located here: C:\Users\<my username>\AppData\Local\Notepad++\plugins
In that directory I created a new directory (named XmlTools) and copied XMLTools.dll there. (And I copied all dependencies to the Notepad++ directory in Program files.)