自动格式化(gg=G
)完全适用于代码像这样(例如,从here):Vim中的自动格式选项(=)不能正确缩进HTML + JS?
fun()
{
for(...)
{
for(...)
{
if(...)
{
}
}
}
}
变得
fun()
{
for(...)
{
for(...)
{
if(...)
{
}
}
}
}
但它不能用于更复杂的代码如下所示(从here复制)
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
</body>
</html>
变成:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
</body>
</html>
为什么,例如,<p>
标签不能在体内缩进?这是vim格式化程序的一个缺点,还是我使用不正确?
编辑:谢谢大家谁提到我应该把filetype plugin indent on
在我的.vimrc
文件。这使缩进更好。但是,它有时还是失败。观察(复制自here)
<!DOCTYPE html>
<html>
<body>
<div style="text-align:center">
<button onclick="playPause()">Play/Pause</button>
<button onclick="makeBig()">Big</button>
<button onclick="makeSmall()">Small</button>
<button onclick="makeNormal()">Normal</button>
<br />
<video id="video1">
<source src="mov_bbb.mp4" type="video/mp4" />
<source src="mov_bbb.ogg" type="video/ogg" />
Your browser does not support HTML5 video.
</video>
</div>
<script type="text/javascript">
var myVideo=document.getElementById("video1");
function playPause()
{
if (myVideo.paused)
myVideo.play();
else
myVideo.pause();
}
function makeBig()
{
myVideo.height=(myVideo.videoHeight*2);
}
function makeSmall()
{
myVideo.height=(myVideo.videoHeight/2);
}
function makeNormal()
{
myVideo.height=(myVideo.videoHeight);
}
</script>
<p>Video courtesy of <a href="http://www.bigbuckbunny.org/" target="_blank">Big Buck Bunny</a>.</p>
</body>
</html>
根本没有改变。它没有意识到这些函数嵌套在<script>
标签中。将文件类型设置为js.html
或html.js
也没有帮助
它的缩进预期* *任何HTML,使其能够打开文件类型插件? (例如根本没有JavaScript) – 2012-02-23 22:41:05
这似乎已经在这里得到解答: http://stackoverflow.com/questions/3276392/vim-gg-g-aligns-left-does-not-auto-indent – Chriseyre2000 2012-02-23 22:41:46
@ Chriseyre2000我测试出来,看看是否该解决方案使得它更可靠 – puk 2012-02-23 22:57:56