Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim how to fold an html tag

Tags:

vim

folding

I see a few posts saying I should use zfat to fold html tags. So i tried it but did not get the results I expected. For example, I had this in my document

<div id="begin">
   <p>hello world</p>
      </div>

I moved my cursor in vim to the first line of <div id="begin">. Then I pressed escape to make sure I'm in command mode. Then I pressed the keys z, f, a and t. But nothing happened. I even tried typing z, f, a and {, which normally folds blocks of PHP code, but it has no effect on the html.

Can some one tell me the exact steps to fold the html div tags?

Thanks

like image 601
John Avatar asked Dec 12 '22 07:12

John


1 Answers

zf indeed is the command to create a manual fold. For that to work, you need to have

:setlocal foldmethod=manual

(There will be an E350 otherwise.) The at is a text object for a tag. You can check what it comprises by applying it to visual mode instead: vat.

Note that manual folding is tedious; I'd recommend to use the syntax folding that comes with the built-in HTML syntax plugin. Just put

:setlocal foldmethod=syntax

in ~/.vim/after/ftplugin/html.vim to enable it.

Also note that so far (as of Vim 7.4.1830), the default HTML syntax script only folds a multi-line tag itself, not the text between the opening and closing tag. To achieve that, you need a syntax extension, see here.

like image 100
Ingo Karkat Avatar answered Feb 03 '23 16:02

Ingo Karkat