如何在 VI 中整理 HTML 文件的缩进?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/815548/
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
How do I tidy up an HTML file's indentation in VI?
提问by mmcdole
How do I fix the indentation of his huge html files which was all messed up?
我如何修复他巨大的 html 文件的缩进,这些文件都搞砸了?
I tried the usual "gg=G
command, which is what I use to fix the indentation of code files. However, it didn't seem to work right on HTML files. It simply removed all the formatting.
我尝试了通常的"gg=G
命令,这是我用来修复代码文件缩进的。但是,它似乎不适用于 HTML 文件。它只是删除了所有格式。
I also tried setting :filetype = xml
, to see if tricking it into thinking this was an XML file would help but it still didn't do it.
我也尝试设置:filetype = xml
,看看是否欺骗它认为这是一个 XML 文件会有所帮助,但它仍然没有做到。
采纳答案by moinudin
With filetype indent on
inside my .vimrc
, Vim indents HTML files quite nicely.
随着filetype indent on
里面我的.vimrc
,Vim的缩进HTML文件相当不错。
Simple example with a shiftwidth
of 2:
ashiftwidth
为 2 的简单示例:
<html>
<body>
<p>
text
</p>
</body>
</html>
回答by tylerl
There's several things that all need to be in place. Just to summarize them all in one location:
有几件事都需要到位。只是在一个位置总结它们:
Set the following option:
设置以下选项:
:filetype indent on
:set filetype=html # abbrev - :set ft=html
:set smartindent # abbrev - :set si
Then either move the cursor to the top of the file and indent to the end: gg
=G
Or select the desired text to indent and hit =to indent it.
然后将光标移动到文件顶部并缩进到末尾:gg
=G
或者选择所需的文本进行缩进并点击=以缩进。
回答by oscaroscar
The main problem using the smart indentation is that if the XML (or HTML) sits on one line as it may end up coming back from a curl request then gg=G
won't do the trick. Instead I have just experienced a good indentation using tidy directly called from VI:
使用智能缩进的主要问题是,如果 XML(或 HTML)位于一行上,因为它可能最终从 curl 请求中返回,那么gg=G
将无法解决问题。相反,我刚刚使用从 VI 直接调用的 tidy 体验了良好的缩进:
:!tidy -mi -xml -wrap 0 %
This basically tells VI to call tidy to cleanup an XML file not wrapping the lines to make them fit on the default 68 characters wide lines. I processed a large 29MB XML file and it took 5 or 6 seconds. I guess for an HTML file the command should therefore be:
这基本上是告诉 VI 调用 tidy 来清理不包装行的 XML 文件,以使它们适合默认的 68 个字符宽的行。我处理了一个 29MB 的大 XML 文件,耗时 5 或 6 秒。我猜对于 HTML 文件,命令应该是:
:!tidy -mi -html -wrap 0 %
As mentioned in comments, tidy
is a basic tool which you could find on many base Linux / MacOS systems. Here is the projet's page in case you wish you had it but don't: HTML Tidy.
正如评论中提到的,tidy
是一个基本工具,您可以在许多基本 Linux / MacOS 系统上找到它。这是项目的页面,以防您希望拥有它但没有:HTML Tidy。
回答by Cory Klein
As tylerl explains above, set the following:
正如上面 tylerl 所解释的,设置以下内容:
:filetype indent on
:set filetype=html
:set smartindent
However, note that in vim 7.4 the HTML tags html
, head
, body
, and some others are notindented by default. This makes sense, as nearly all content in an HTML file falls under those tags. If you really want to, you can get those tags to be indented like so:
但是请注意,在vim 7.4的HTML标签html
,head
,body
,和其他一些人都不会默认缩进。这是有道理的,因为 HTML 文件中的几乎所有内容都属于这些标签。如果你真的想要,你可以像这样缩进这些标签:
:let g:html_indent_inctags = "html,body,head,tbody"
See "HTML indenting not working in compiled Vim 7.4, any ideas?" and "alternative html indent script" for more information.
有关更多信息,请参阅“ HTML 缩进在已编译的 Vim 7.4 中不起作用,有什么想法吗?”和“替代 html 缩进脚本”。
回答by adam_0
This is my solution that works nicely for opening "ugly" HTML in a nicely spaced way:
这是我的解决方案,非常适合以良好的间隔方式打开“丑陋”的 HTML:
vim fileIn.html -c "set sw=2 | %s/>/>\r/ | execute 'normal gg=G' | set nohlsearch | g/^\s*$/d"
- The
sw
command is because my default is 4, which is too high for HTML. - The next part adds a newline (Vim thinks it's a carriage return, sigh) after each element (
>
). - Then re-indent the entire file with
=
. - Then unhighlight
>
(since I haveset hlsearch
in my vimrc). - Then remove all empty/whitespace-only lines (see "Vim delete blank lines" for more, also this is double-escaped because it's in the shell).
- 该
sw
命令是因为我的默认值是4,这是太高了HTML。 - 下一部分在每个元素 (
>
)之后添加一个换行符(Vim 认为这是一个回车,叹气)。 - 然后重新缩进整个文件
=
。 - 然后取消突出显示
>
(因为我set hlsearch
在 vimrc 中有)。 - 然后删除所有空行/仅空白行(更多信息请参见“ Vim 删除空白行”,这也是双重转义,因为它在 shell 中)。
You can even add | wq! fileOut.html
to the end if you don't want to enter Vim at all, but just clean up the file.
| wq! fileOut.html
如果您根本不想进入 Vim,您甚至可以添加到最后,只需清理文件即可。
回答by SimonW
I use this script: https://github.com/maksimr/vim-jsbeautify
我使用这个脚本:https: //github.com/maksimr/vim-jsbeautify
In the above link you have all the info:
在上面的链接中,您拥有所有信息:
- Install
- Configure (copy from the first example)
- Run
:call HtmlBeautify()
- 安装
- 配置(从第一个示例复制)
- 跑
:call HtmlBeautify()
Does the job beautifully!
干得漂亮!
回答by JaredPar
Have you tried using the HTML indentation script on the Vim site?
您是否尝试过在 Vim 站点上使用 HTML 缩进脚本?
回答by Lemur
Here's a heavy-weight solution that gets you indenting, plus all the HTML pretty-printing you don't necessarily want to care about while you're editing.
这是一个重量级的解决方案,可以让您缩进,以及您在编辑时不一定要关心的所有 HTML 漂亮打印。
First, download Tidy. Make sure you add the binary to your path, so you can call it from any location.
首先,下载Tidy。确保将二进制文件添加到您的路径中,以便您可以从任何位置调用它。
Next, create a config file describing your favorite HTML flavor. Documentation is not great for Tidy, but here's an overview, and a list of all the options. Here's my config file:
接下来,创建一个描述您最喜欢的 HTML 风格的配置文件。文档对 Tidy 来说不是很好,但这里有一个概述和所有选项的列表。这是我的配置文件:
bare: yes
break-before-br: no
clean: yes
drop-proprietary-attributes: yes
fix-uri: yes
indent-spaces: 4
indent: yes
logical-emphasis: yes
markup: yes
output-xhtml: yes
quiet: yes
quote-marks: yes
replace-color: yes
tab-size: 4
uppercase-tags: no
vertical-space: yes
word-2000: yes
wrap: 0
Save this as tidyrc_html.txt
in your ftplugin
folder (under vimfiles).
将其另存为tidyrc_html.txt
您的ftplugin
文件夹中(在 vimfiles 下)。
One more file: add the following line to (or create) html.vim
, also in ftplugin
:
另一个文件:html.vim
将以下行添加到(或创建),也在ftplugin
:
map <leader>tidy :%! tidy -config ~/vimfiles/ftplugin/tidyrc_html.txt <CR>
To use it, just open an HTML file, and type /tidy
. (That /
is the <leader>
key.)
要使用它,只需打开一个 HTML 文件,然后输入/tidy
. (这/
是<leader>
关键。)
There you go! Not a quick solution, by any means, but now you're a bit better equipped for editing those huge, messed-up HTML files.
你去吧!无论如何,这不是一个快速的解决方案,但现在您可以更好地编辑那些巨大的、混乱的 HTML 文件。
回答by Chiel ten Brinke
You can integrate both tidyand html-beautifyautomatically by installing the plugin vim-autoformat. After that, you can execute whichever formatter is installed with a single keystroke.
您可以通过安装插件vim-autoformat自动集成tidy和html-beautify。之后,您可以通过一次按键执行安装的任何格式化程序。
回答by wisbucky
I tried the usual "gg=G" command, which is what I use to fix the indentation of code files. However, it didn't seem to work right on HTML files. It simply removed all the formatting.
我尝试了常用的“gg=G”命令,这是我用来修复代码文件缩进的。但是,它似乎不适用于 HTML 文件。它只是删除了所有格式。
If vim's autoformat/indent gg=G
seems to be "broken" (such as left indenting every line), most likely the indent plugin is not enabled/loaded. It should really give an error message instead of just doing bad indenting, otherwise users just think the autoformat/indenting feature is awful, when it actually is pretty good.
如果 vim 的自动格式化/缩进gg=G
似乎“损坏”(例如每行左缩进),则很可能未启用/加载缩进插件。它真的应该给出错误消息,而不是仅仅做不好的缩进,否则用户只会认为自动格式化/缩进功能很糟糕,而实际上它非常好。
To check if the indent plugin is enabled/loaded, run :scriptnames
. See if .../indent/html.vim
is in the list. If not, then that means the plugin is not loaded. In that case, add this line to ~/.vimrc
:
要检查缩进插件是否已启用/加载,请运行:scriptnames
. 看看是否.../indent/html.vim
在列表中。如果没有,则表示未加载插件。在这种情况下,将此行添加到~/.vimrc
:
filetype plugin indent on
Now if you open the file and run :scriptnames
, you should see .../indent/html.vim
. Then run gg=G
, which should do the correct autoformat/indent now. (Although it won't add newlines, so if all the html code is on a single line, it won't be indented).
现在,如果您打开文件并运行:scriptnames
,您应该看到.../indent/html.vim
. 然后运行gg=G
,它现在应该执行正确的自动格式化/缩进。(虽然它不会添加换行符,所以如果所有的 html 代码都在一行上,它就不会缩进)。
Note: if you are running :filetype plugin indent on
on the vim command line instead of ~/.vimrc
, you must re-open the file :e
.
注意:如果您:filetype plugin indent on
在 vim 命令行而不是 上运行,则~/.vimrc
必须重新打开文件:e
.
Also, you don't need to worry about autoindent
and smartindent
settings, they are not relevant for this.
此外,您无需担心autoindent
和smartindent
设置,它们与此无关。