如何在 Notepad++ 中自动格式化/缩进 XML/HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7117949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:09:38  来源:igfitidea点击:

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

htmlxmlnotepad++auto-indentautoformatting

提问by SBoss

Is there a way to re-indent a block of code? I'm looking for something similar to Ctrl+Shift+Fin Eclipse (Auto-Format/Indent).

有没有办法重新缩进一段代码?我在寻找类似的东西Ctrl+ Shift+F在Eclipse(自动格式/缩进)。

To be clear,

要清楚,

  • I already know how to format XML outsideof 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.
  • 我已经知道如何Notepad++之外格式化 XML (如前所述,Eclipse 工作正常)所以我不需要一堆指向其他 XML 格式化工具的链接。
  • 我专门使用 XML 和 HTML。
  • 理想情况下,有一个与 Eclipse 中一样方便的键绑定,所以我不必中断我的工作流程。

I already know about NppAutoIndent - it won't work, as I'm working with XML, HTML and CSS.

我已经知道 NppAutoIndent - 它不起作用,因为我正在使用 XML、HTML 和 CSS。

回答by remipod

Since I upgraded to 6.3.2, I use XML Tools.

由于我升级到 6.3.2,我使用XML Tools.

  • install XML Tools via the Plugin Admin.
  • use the shortcut Ctrl+Alt+Shift+B(ormenu → Plugins → XML Tools → Pretty Print)
  • 通过插件管理安装 XML 工具。
  • 使用快捷键Ctrl+ Alt+ Shift+ B菜单 → 插件 → XML 工具 → 漂亮打印)

In older versions: menu → TextFX → HTML Tidy → Tidy: Reindent XML.

在旧版本中:菜单 → TextFX → HTML Tidy → Tidy: Reindent XML。

回答by Dima

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

安装 Tidy2 插件。我有 Notepad++ v6.2.2,到目前为止 Tidy2 工作正常。

回答by Some Java Guy

For those who don't know, npp has a lot of support from plugins and other projects. You can download those plugins from SourceForge.

对于那些不知道的人,npp 有很多来自插件和其他项目的支持。您可以从SourceForge下载这些插件。

enter image description here

在此处输入图片说明

You need XML Toolsto format your text in n++

您需要XML Tools在 n++ 中格式化您的文本

After you have downloaded XML Tools..

下载后XML Tools..

Exit Notepad++

退出记事本++

Go To C:\Program File\Notepad++.... Your N++ installed folder.

转到 C:\Program File\Notepad++....您的 N++ 安装文件夹。

  1. Place below files from xml tools which you downloaded in the npp root folder by copy replace
  1. 将您从 xml 工具下载到 npp 根文件夹中的文件放在下面 copy replace

enter image description here

在此处输入图片说明

  1. Go To ..\Pluginssubfolder and place below downloaded file
  1. 转到..\Plugins子文件夹并放置在下载的文件下方

enter image description here

在此处输入图片说明

Restart and enjoy!!!

重新开始享受吧!!!

Ctrl+ Alt+ Shft+ Bto format.

Ctrl+ Alt+ Shft+B来格式化。

回答by Vladimir Grigorov

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.

这是我第三次安装 Windows 和 npp,一段时间后我发现 tidy 功能不再起作用。所以我用谷歌搜索了一个解决方案,来到这个线程,然后在更多线程的帮助下,我终于修复了它。我会一劳永逸地总结我所有的行动。

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

  2. Install libtidy.dll by pasting the Config folder from an old npp package: Follow instructions in this answer.

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

  4. Restart npp and verify TextFX -> TextFX HTML Tidy -> Tidy: Reindent XML works.

  1. 安装 TextFX 插件:插件 -> 插件管理器 -> 显示插件管理器。选择 TextFX Characters 并安装。重新启动 npp 后,应该可以看到菜单“TextFX”。(学分:@remipod)。

  2. 通过粘贴旧 npp 包中的 Config 文件夹来安装 libtidy.dll:按照此答案中的说明进行操作。

  3. 在您最新的 npp 安装目标(通常是 C:\Program Files (x86)\Notepad++\plugins)中有一个 Config 文件夹后,npp 需要对该文件夹的写访问权限。右键单击配置文件夹 -> 属性 -> 安全选项卡 -> 选择用户,单击编辑 -> 选中完全控制以允许读/写访问。请注意,您需要管理员权限才能执行此操作。

  4. 重新启动 npp 并验证 TextFX -> TextFX HTML Tidy -> Tidy: Reindent XML 有效。

回答by Daksh Agrawal

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.

Notepad ++ 默认不提供任何此类功能。但是您可以使用一些在线工具来自动格式化文本,例如https://www.freeformatter.com/xml-formatter.html

It helps. :)

它有助于。:)

回答by Oliver Meyer

I had to update the proxy settingsunder 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.

之后,安装“XML 工具”就很容易了,并完成了上述要求的工作。

回答by delliottg

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:

要直接回答 OP,请查看此人的站点: Thomas Hunter Notepad++ Tidy for XML。遵循简单的步骤,您就可以在 NPP 中获得非常好的 XML 格式。到目前为止,我发现的唯一异常是嵌套的自关闭元素 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.

可能有办法解决这个问题,但就目前而言,它设法将我的文档中的行数减少了 300k,并且可以解决这个特殊的异常。

回答by Bj?rn

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

我正在使用带有“插件管理”的记事本 7.6,但我找不到 XML 工具。
我必须像@some-java-guy 在他的回答中所做的那样手动安装它,只是我的插件文件夹位于此处:C:\Users\<my username>\AppData\Local\Notepad++\plugins
在该目录中,我创建了一个新目录(名为 XmlTools)并在那里复制了 XMLTools.dll。(我将所有依赖项复制到程序文件中的 Notepad++ 目录。)

回答by Daniel Nyamasyo

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

只需安装最新的记事本++并按折叠安装缩进。在菜单栏上选择 Plugins -> Plugins Admin 并选择 indent By fold 并安装。作品最好