2012-02-23 39 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