2012-02-23 38 views
1

自动格式化(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.htmlhtml.js也没有帮助

+0

它的缩进预期* *任何HTML,使其能够打开文件类型插件? (例如根本没有JavaScript) – 2012-02-23 22:41:05

+0

这似乎已经在这里得到解答: http://stackoverflow.com/questions/3276392/vim-gg-g-aligns-left-does-not-auto-indent – Chriseyre2000 2012-02-23 22:41:46

+0

@ Chriseyre2000我测试出来,看看是否该解决方案使得它更可靠 – puk 2012-02-23 22:57:56

回答

1

因此,vim针对不同文件类型具有不同的格式/语法高亮选项。你可以阅读关于它here。因此,对于你的常规C++文件缩进是非常标准的,因此通常得到它正确的,但你的HTML文件,你可能有那么不同perferences谁做的格式文件的pereson。您可以编辑,看看你的格式配置在linux下~/.vim/ftplugin和HTML文件将被称为html.vim

也像比尔说,你可能需要通过设置或者在你~/.vimrc或键入:filetype plugin indent on

+0

如果我没有设置':filetype plugin indent on',那么格式化不起作用。如果我确实设置了它,Vim会抱怨它无法调用JSLint(Syntastic)。我如何协调这一点? – puk 2012-02-24 07:13:14