In trying to get vim
to indent .html
files properly, I followed the examples set out here.
在尝试让vim正确缩进.html文件时,我按照这里列出的示例进行操作。
Given the following file index.html
:
给定以下文件index.html:
<html>
<body>
<p>
text
</p>
</body>
</html>
I tried opening it like so (ignoring my .vimrc
to make sure it isn't interfering negatively)
我尝试打开它(忽略我的.vimrc以确保它不会产生负面干扰)
vim -u NONE index.html
Then I set the options to enable automatic indenting:
然后我设置选项以启用自动缩进:
:filetype plugin indent on
:set filetype=html # abbrev - :set ft=html
:set smartindent # abbrev - :set si
And then I indented the entire file with gg=G
, and this is the result:
然后我用gg = G缩进整个文件,这就是结果:
<html>
<body>
<p>
text
</p>
</body>
</html>
I checked to make sure that the html.vim
file existed, and it's definitely there
我检查确保html.vim文件存在,它肯定存在
$ head -2 ~/.vim/after/ftplugin/html.vim
" Vim syntax file
" Language: HTML
$ head -2 ~/.vim/ftplugin/html.vim
" Vim syntax file
" Language: HTML
My version of vim is 7.4:
我的vim版本是7.4:
$ vim --version | head -1
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Sep 23 2013 16:12:20)
And it includes smart indent:
它包括智能缩进:
$ vim --version | grep smartindent
-ebcdic +mouse +smartindent +xim
I'm at a loss as to why the indentation isn't working! Any clues or ideas to research this problem further?
我不知道为什么压痕不起作用!有进一步研究这个问题的线索或想法吗?
FWIW, I'm running Ubuntu 13.04.
FWIW,我正在运行Ubuntu 13.04。
4 个解决方案
#1
25
As mentioned in Cory's answer, the currently distributed version is Vimscript 2075. If you go to that plugin page you can see documented all the tags that by default will increase indent.
正如Cory的回答所提到的,当前分发的版本是Vimscript 2075.如果你去那个插件页面,你可以看到记录的所有标签,默认情况下会增加缩进。
None of the tags you gave in your example are in this default list, but there are plenty of them.
您在示例中给出的标签都不在此默认列表中,但有很多标签。
Since indentation of HTML is very open to user preference, the plugin maintainer has included an option to add or remove tags to or from the list of tags that increases indent. See :help html-indent, where it suggests:
由于HTML的缩进对用户偏好非常开放,因此插件维护者已经包含一个选项,用于在增加缩进的标记列表中添加或删除标记。请参阅:help html-indent,它建议:
You can add further tags with:
:let g:html_indent_inctags = "html,body,head,tbody"
#2
14
Between versions 7.3 and 7.4, the bundled html.vim
file located in $VIMRUNTIME/indent
changed. The currently distributed version is actually Vimscript #2075, and it doesn't indent some html
tags by default.
在版本7.3和7.4之间,位于$ VIMRUNTIME / indent中的捆绑html.vim文件已更改。当前分发的版本实际上是Vimscript#2075,默认情况下它不会缩进某些html标签。
I recommend Ben's solution above, but alternatively you can revert to a previous version of the distributed html.vim
file.
我推荐上面的Ben的解决方案,但您也可以恢复到以前版本的分布式html.vim文件。
To do this, just replace the existing 7.4 html.vim
file with the one from 7.3.
要做到这一点,只需用7.3中的一个替换现有的7.4 html.vim文件。
wget ftp://ftp.vim.org/pub/vim/unix/vim-7.3.tar.bz2
tar jxf vim-7.3.tar.bz2
cp vim73/runtime/indent/html.vim ~/.vim/indent/
#3
7
As of 7.4.52
截至7.4.52
within vim:
在vim内:
:let g:html_indent_inctags = "html,body,head,tbody"
:call HtmlIndent_CheckUserSettings()
else in .vimrc:
.vimrc中的其他内容:
let g:html_indent_inctags = "html,body,head,tbody"
I wanted to just to add this to a comment on the top answer, to give back, after spending too much time not getting the answer to work, but apparently don't have enough reputation :(
我想只是将其添加到对最佳答案的评论中,在花费太多时间没有得到工作答案之后回馈,但显然没有足够的声誉:(
#4
0
The first troubleshooting step is to run :scriptnames
. If you don't see .../indent/html.vim
, then that means the plugin is not loaded correctly and indenting won't work properly. It will probably just left indent every line. (The problem is that vim doesn't give an error, so it seems like the indenting is doing an awful job.)
第一个故障排除步骤是运行:scriptnames。如果你没有看到... / indent / html.vim,那么这意味着插件没有正确加载,缩进将无法正常工作。它可能只是留下每行的缩进。 (问题是vim没有出错,所以看起来缩进是做得太糟糕了。)
The most reliable way to get it working is to put this line in your ~/.vimrc
.
让它运行的最可靠方法是将这一行放在〜/ .vimrc中。
filetype plugin indent on
Then open the file with vim again, and run :scriptnames
. You should see .../indent/html.vim
now. Then type gg=G
to autoformat the entire file.
然后再次使用vim打开文件,然后运行:scriptnames。你现在应该看到... / indent / html.vim。然后键入gg = G以自动格式化整个文件。
One important note that tripped me up for a while: If you don't put it in ~/.vimrc
and just type :filetype plugin indent on
after you've opened the file, you will have to re-open the file again with :e
. The indent plugin has to be loaded before you open the file. Run :scriptnames
to confirm.
一个重要的注意事项让我绊倒了一段时间:如果你没有把它放在〜/ .vimrc中,只需在你打开文件后输入:filetype plugin indent on,你就必须重新打开文件:即在打开文件之前必须加载缩进插件。运行:scriptnames确认。
Side note: you don't need to worry about smartindent
or autoindent
settings, those are for something else.
附注:您不需要担心smartindent或autoindent设置,这些都是针对其他事情的。
#1
25
As mentioned in Cory's answer, the currently distributed version is Vimscript 2075. If you go to that plugin page you can see documented all the tags that by default will increase indent.
正如Cory的回答所提到的,当前分发的版本是Vimscript 2075.如果你去那个插件页面,你可以看到记录的所有标签,默认情况下会增加缩进。
None of the tags you gave in your example are in this default list, but there are plenty of them.
您在示例中给出的标签都不在此默认列表中,但有很多标签。
Since indentation of HTML is very open to user preference, the plugin maintainer has included an option to add or remove tags to or from the list of tags that increases indent. See :help html-indent, where it suggests:
由于HTML的缩进对用户偏好非常开放,因此插件维护者已经包含一个选项,用于在增加缩进的标记列表中添加或删除标记。请参阅:help html-indent,它建议:
You can add further tags with:
:let g:html_indent_inctags = "html,body,head,tbody"
#2
14
Between versions 7.3 and 7.4, the bundled html.vim
file located in $VIMRUNTIME/indent
changed. The currently distributed version is actually Vimscript #2075, and it doesn't indent some html
tags by default.
在版本7.3和7.4之间,位于$ VIMRUNTIME / indent中的捆绑html.vim文件已更改。当前分发的版本实际上是Vimscript#2075,默认情况下它不会缩进某些html标签。
I recommend Ben's solution above, but alternatively you can revert to a previous version of the distributed html.vim
file.
我推荐上面的Ben的解决方案,但您也可以恢复到以前版本的分布式html.vim文件。
To do this, just replace the existing 7.4 html.vim
file with the one from 7.3.
要做到这一点,只需用7.3中的一个替换现有的7.4 html.vim文件。
wget ftp://ftp.vim.org/pub/vim/unix/vim-7.3.tar.bz2
tar jxf vim-7.3.tar.bz2
cp vim73/runtime/indent/html.vim ~/.vim/indent/
#3
7
As of 7.4.52
截至7.4.52
within vim:
在vim内:
:let g:html_indent_inctags = "html,body,head,tbody"
:call HtmlIndent_CheckUserSettings()
else in .vimrc:
.vimrc中的其他内容:
let g:html_indent_inctags = "html,body,head,tbody"
I wanted to just to add this to a comment on the top answer, to give back, after spending too much time not getting the answer to work, but apparently don't have enough reputation :(
我想只是将其添加到对最佳答案的评论中,在花费太多时间没有得到工作答案之后回馈,但显然没有足够的声誉:(
#4
0
The first troubleshooting step is to run :scriptnames
. If you don't see .../indent/html.vim
, then that means the plugin is not loaded correctly and indenting won't work properly. It will probably just left indent every line. (The problem is that vim doesn't give an error, so it seems like the indenting is doing an awful job.)
第一个故障排除步骤是运行:scriptnames。如果你没有看到... / indent / html.vim,那么这意味着插件没有正确加载,缩进将无法正常工作。它可能只是留下每行的缩进。 (问题是vim没有出错,所以看起来缩进是做得太糟糕了。)
The most reliable way to get it working is to put this line in your ~/.vimrc
.
让它运行的最可靠方法是将这一行放在〜/ .vimrc中。
filetype plugin indent on
Then open the file with vim again, and run :scriptnames
. You should see .../indent/html.vim
now. Then type gg=G
to autoformat the entire file.
然后再次使用vim打开文件,然后运行:scriptnames。你现在应该看到... / indent / html.vim。然后键入gg = G以自动格式化整个文件。
One important note that tripped me up for a while: If you don't put it in ~/.vimrc
and just type :filetype plugin indent on
after you've opened the file, you will have to re-open the file again with :e
. The indent plugin has to be loaded before you open the file. Run :scriptnames
to confirm.
一个重要的注意事项让我绊倒了一段时间:如果你没有把它放在〜/ .vimrc中,只需在你打开文件后输入:filetype plugin indent on,你就必须重新打开文件:即在打开文件之前必须加载缩进插件。运行:scriptnames确认。
Side note: you don't need to worry about smartindent
or autoindent
settings, those are for something else.
附注:您不需要担心smartindent或autoindent设置,这些都是针对其他事情的。