0

If I have this react code:

<tag>
..
</tag>

and the cursor is on top of <tag>, tapping on % will make the cursor jump to the closing </tag>.

I want to create a vim key mapping which folds the contents of <tag></tag> when the cursor is on top of <tag>

2 Answers 2

2

If your foldmethod is default (foldmethod=manual) than you can:

zfit

  • :h zf -- create fold
  • :h it -- over "inner tag block" text object

asciicast

PS

And if you are into "automatic" folding, you can try built-in syntax based html folding as described in :h html-folding:

The HTML syntax file provides syntax |folding| (see |:syn-fold|) between start
and end tags.  This can be turned on by >

    :let g:html_syntax_folding = 1
    :set foldmethod=syntax

asciicast

4
  • i can't see the keystrokes from the actual recording, and typing :zfit when the cursor is ontop of a bracket doesn't do anything
    – abbood
    Commented Feb 8 at 9:22
  • zfit @abbood, have you checked help topics?
    – Maxim Kim
    Commented Feb 8 at 9:48
  • No I haven't, I'll take a look and try again. Thanks for your help Maxim :)
    – abbood
    Commented Feb 8 at 9:52
  • 1
    hey it worked perfectly! In fact it makes sense to just go through the commands (ie visual selection, then creating the fold. I also liked how we're creating an inner fold and an outer fold)
    – abbood
    Commented Feb 9 at 4:42
1

I would do:

nnoremap <leader>f v%zf

Under the hypothesis that foldmethod is manual:

set foldmethod=manual
6
  • it didn't work, i made sure that foldmethod is manual
    – abbood
    Commented Feb 8 at 9:30
  • When you select some lines and hit zf did it fold the lines you have selected? You can replace <leader>f by a key combination that is available for you e.g. <C-s>. Commented Feb 8 at 9:57
  • Could you tell us what is the filetype of your buffer (:set ft?)? Commented Feb 8 at 9:59
  • IIRC, using % to navigate HTML tags requires matchit to have been enabled.
    – Rich
    Commented Feb 8 at 12:31
  • when i type :set ft? I see recording @j on the status line
    – abbood
    Commented Feb 9 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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