2011-08-22 37 views
47

是否有一些插件可以在Vim中摺疊HTML標籤?
或者還有另一種方法來設置摺疊或展開html標籤的快捷方式?
我想摺疊/展開html標籤,就像我使用縮進摺疊一樣。如何使用Vim摺疊/展開HTML標籤

+0

'set foldmethod = syntax'適用於HTML –

回答

69

我發現zfat(或同樣地,zfit)適用於摺疊HTML文檔。 za將切換(打開或關閉)現有摺疊。 zR打開當前文檔中的所有摺疊,zM有效地重新啓用文檔中標記的所有摺疊。

如果你發現自己使用的褶皺廣泛,你可以爲自己一些方便的快捷鍵綁定在你的.vimrc.

+0

您的答案只適用於手動方式的fold方法,並且不能用於其他方法。 – soarinblue

14

如果縮進你的HTML下面應該工作:

set foldmethod=indent 

這樣做的問題,我發現,有太多褶皺。爲了解決這個問題,我分別使用zOzc來打開和關閉嵌套摺疊。

有關更多信息,請參見help fold-indent

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默認關閉第三級摺疊或更高; 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一部開拓創新的html:

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

JS-beautif y和VIM倍:

js-beautify and vim fold

2

添加由詹姆斯來回答。 最初我的foldmethod =語法,所以zfat不起作用。 解決方案是將foldemethod設置爲手動

:setlocal foldmethod=manual 

檢查其在使用中使用foldmethod,

:setlocal foldmethod? 
2

與使用foldmethod語法,這是更簡單的摺疊的HTML。

此答案根據HTML syntax folding in vim。作者是@Ingo Karcat。

  1. 設置要與語法的摺疊方法如下:

    vim的命令行:set foldmethod=syntax

    或將設定在~/.vim/after/ftplugin/html.vim

    setlocal foldmethod=syntax 
    
  2. 還要注意到目前爲止,默認語法腳本僅摺疊多行 標籤本身,而不是開始和結束標籤之間的文本。

    So, this gets folded: 
    
         <div 
          class="foo" 
          id="bar" 
         > 
    
        And this doesn't 
    
         <div> 
          <b>text between here</b> 
         </div> 
    
  3. 若要摺疊標籤之間,你需要擴展語法腳本,通過 以下,最好的地方爲~/.vim/after/syntax/html.vim

    幾乎是無效的HTML元素之間進行語法摺疊 (那些別「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

首先set foldmethod=syntax並嘗試zfit摺疊開始標記和zo展開標記,它在我的vim上運行良好。