自動格式化(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