在notepad++中是否有键盘快捷键来格式化代码? 我主要使用HTML, CSS和Python代码。

例如:

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

我记得Visual Studio用Ctrl+K+D做到了,NetBeans也有这个功能,但在notepad++中找不到它,如果它甚至可以做到的话。


当前回答

在我的notepad++中,似乎TextFX需要一个perl环境来格式化HTML文件。Tidy2不需要任何东西,所以我认为它更方便。

其他回答

如果你所需要的只是对齐,可以试试名为Code alignment的插件。

你可以从notepad++中的内置插件管理器中获取它。

另一个潜在的解决方案

好了,这个问题已经问了12年了。我认为我已经找到了另一个可接受的解决方案,这可能是更好的格式文件,使用pretty。

什么更漂亮?

beautitier被描述为:“一个固执己见的代码格式化器。它通过解析你的代码并使用自己的规则重新打印它来实现一致的风格,这些规则考虑到最大的行长,在必要时包装代码。”

pretty支持以下语言

超文本标记语言 CSS、Less和SCSS JavaScript 打印稿 JSX, Angular, Vue JSON, Markdown, YAML

还有其他一些。

如果你想使用其他语言,你可以使用:

社区为pretty制作的插件,允许支持其他格式,如Java、Rust、PHP、SQL和其他格式。 非漂亮格式器支持其他语言,如clangformat (npm版本)(C / c++ / Java / JavaScript / JSON / Objective-C / Protobuf / c#),或Black (Python)。

如何安装

在本教程中,你需要安装Node.js。

注意:可能会有其他的方法来使用后续的 提到的软件没有使用Node.js。但是为了保留这个教程 简单和Node.js已经广泛传播的事实 开发人员的熟悉程度,我将使用这个软件 没有解释如何安装。

在安装Node.js之后,在你的系统中全局安装更漂亮的程序,这样你就可以在命令行中运行更漂亮的程序。

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


NppExec + pretty的命令和截图。

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)\**"

安装NppExec Plugin


运行NppExec Plugin


在NppExec上执行命令

在插件>插件管理>搜索“XML工具”>安装。

该窗口将重新启动并在插件下安装XML Tools。

打开文件并单击Ctrl + Alt + Shift + B

在我的notepad++中,似乎TextFX需要一个perl环境来格式化HTML文件。Tidy2不需要任何东西,所以我认为它更方便。

这不是你想要的答案,但这是我有同样问题时想到的解决办法。

[更新-我现在使用vscode的一切。我认为它在很多情况下都更好。

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.