0

When I'm opening a fold, I would say 100% of the time the behaviour I want is zv - open all folds covering the current line, so that I can actually see it.

But zv won't close a fold.

How can I use the line's current state of being folded or not folded in a normal mode mapping so that I can have a za (toggle fold) but with my desired zv behaviour on opening?

3

Try overriding the default za functionality with this mapping:

:nnoremap <expr> za foldclosed(".") == -1 ? 'zc' : 'zv'

It uses the foldclosed() function within an expression mapping to check if the cursor is on a closed fold, mapping to zc if it's not, and zv if it is.

  • Thanks - though :verbose nnoremap <space> returns n <Space> * za foldclosed(".") == -1 ? 'zc' : 'zv', but its behaviour on a fold (i.e. :echo foldclosed(".") prints not -1) matches zo; not zv. (i.e. it only opens the first fold.) Any idea what I've missed? – OJFord Sep 8 at 20:43
  • @OJFord where did <space> come from??? – filbranden Sep 8 at 22:03
  • 1
    @OJFord I interpreted your question as wanting the mapping to be triggered by za. If you want it to be <Space> instead of za, you need to replace the za in my mapping with <Space>: nnoremap <expr> <Space> foldclosed(".") == -1 ? 'zc' : 'zv' I’m not sure exactly what you typed, but the output you’ve pasted in your comment looks wrong. – Rich Sep 8 at 22:26
  • Oh, what an idiot I am :) - it was late... is that any exuse? Many thanks, <expr> is new to me so I expect that'll come in handy again. – OJFord Sep 9 at 11:05
  • @OJFord I should've explained the mapping more when I first wrote it! I've clarified my answer a little, now. – Rich Sep 9 at 11:31

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.