2011-08-22 9 views
47

VimでHTMLタグを折り畳むためのプラグインはありますか?
また、HTMLタグを折りたたんだり展開したりするためのショートカットを設定する別の方法がありますか?
インデントの折り畳みと同じように、HTMLタグを折りたたんだり展開したりしたいと思います。VimでHTMLタグを折りたたむ/展開する方法

+0

'set foldmethod = syntax'はHTMLのために働きます –

答えて

69

私はzfat(または同様にzfit)がHTML文書で折りたたみに適していることを発見しました。 zaは、既存の折り畳みを切り替える(開いたり閉じたりします)。 zRは、現在の文書のすべての折り目を開きます。zMは、文書にマークされている既存の折り目をすべて有効に戻します。あなた自身が広範囲に折り目を使用して見つけた場合、あなたはあなたのHTMLをインデントした場合、以下は動作するはずですあなたの.vimrc.

+0

あなたの答えは手動のfoldmethodでのみ動作し、他のfoldmethodsでは動作できません。 – soarinblue

14

の中で自分自身のためにいくつかの便利なキーバインディングを作ることができ

set foldmethod=indent 

これに伴う問題は、私は見つけて、があまりにも多いがあります。これを回避するために、ネストされた折り畳みを開閉するためにそれぞれzOzcを使用します。

は、より多くの情報のためhelp fold-indentを参照してください:

http://www.google.com.hk orignal

JS-beautif:

The folds are automatically defined by the indent of the lines. 

The foldlevel is computed from the indent of the line, divided by the 
'shiftwidth' (rounded down). A sequence of lines with the same or higher fold 
level form a fold, with the lines with a higher level forming a nested fold. 

The nesting of folds is limited with 'foldnestmax'. 

Some lines are ignored and get the fold level of the line above or below it, 
whichever is lower. These are empty or white lines and lines starting 
with a character in 'foldignore'. White space is skipped before checking for 
characters in 'foldignore'. For C use "#" to ignore preprocessor lines. 

When you want to ignore lines in another way, use the 'expr' method. The 
indent() function can be used in 'foldexpr' to get the indent of a line. 
+0

fdl = 3を設定すると、デフォルトで第3レベルの折り畳みを閉じてください。 zcはまだ開いている折り目を閉じます。 – JRG

3

js-beautifyコマンド(JavaScriptバージョン)

npm -g install js-beautify 
wget --no-check-certificate https://www.google.com.hk/ -O google.index.html 
js-beautify -f google.index.html -o google.index.bt.html 

http://www.google.com.hkのorignalのhtmlをインストールします。 Yとvimの倍:

js-beautify and vim fold

2

ジェームズ・ライで答えることに追加します。 最初は私のfoldmethod = syntaxなので、zfatは動作しません。 ソリューションは

:setlocal foldmethod? 
2

、使用中foldmethodどのチェックするために、手動

:setlocal foldmethod=manual 

にfoldemethodを設定することが簡単ですfoldmethod構文、と折りたたみhtmlです。

この回答はHTML syntax folding in vimに基づいています。著者は@Ingo Karcatです。

  1. は、次のと構文であるためにあなたの折り畳み方法を設定します。

    vimのコマンドライン:set foldmethod=syntax

    または~/.vim/after/ftplugin/html.vim

    setlocal foldmethod=syntax 
    
  2. に設定を入れても、これまでに注意してくださいデフォルトの構文スクリプトは、開始タグと終了タグの間のテキストではなく、複数行の タグ自体を折りたたむだけです。 経由~/.vim/after/syntax/html.vim

    に次の、最高の場所は、構文の折り畳みを除くすべてのボイドのHTML要素 の間で行われる

    So, this gets folded: 
    
         <div 
          class="foo" 
          id="bar" 
         > 
    
        And this doesn't 
    
         <div> 
          <b>text between here</b> 
         </div> 
    
  3. タグの間に折り畳まれ得るために、あなたは構文スクリプトを拡張する必要がある、(それらのドン「t)は<br>ように、まず

    syntax region htmlFold start="<\z(\<\(area\|base\|br\|col\|command\|embed\|hr\|img\|input\|keygen\|link\|meta\|para\|source\|track\|wbr\>\)\@![a-z-]\+\>\)\%(\_s*\_[^/]\?>\|\_s\_[^>]*\_[^>/]>\)" end="</\z1\_s*>" fold transparent keepend extend containedin=htmlHead,htmlH\d 
    
0

を閉鎖兄弟を有しますと開始タグとzoを折り畳むためにzfitを試してみると、それは私のvimでうまくいきます。