If my cursor is somewhere on an HTML tag, I’d like to invoke a custom command that folds until the closing tag if it finds it, else returns “0” or something.

Should I make a function in VimScript or something?

I imagine it would work something like this:

  1. Scan left until opening angle bracket is found. If none found, you are not on an HTML tag; return 0.
  2. Scan right until closing angle bracket tag is found. If second character is backslash, you are on a closing tag; in that case, seek opening tag in next step.
  3. Search forward (backward) for the matching closing (opening) tag.
  4. Now fold between the two lines containing those tags.

This also assumes that the HTML tags are spread out on newlines, which for now I am comfortable with.

The main question I have to get started is if I need to track the cursor with an index or if Vim functions are enough to do it. I could search backwards for “<“, enter “v”, search forward with “/“ for “>”, maybe use some copy function to save that string? Or just save it as a variable? There are many such questions that make it hard for me to see the details.

Thanks very much

1 Answer 1


I think you might want to try zfat or zfit, which create a fold around or inside the current tag. Then you can manipulate the fold as usual.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.