1

When using foldmethod=marker, how do you select or reference exactly the contents of the current fold? I want to swap the order of two folds. If they were paragraphs, I would do dap { p. However, I can't find a corresponding daz. I think the answer is obvious, but I sure can't find it here or on Google. Possibly related to this question.

MCVE:

Fold 1 {{{

}}}
Fold 2 {{{

}}}
Fold 3 {{{
}}}

- vi: set foldmethod=marker: -

Put the cursor on Fold 2. Then dzj removes the contents of Fold 2, and also the Fold 3 line. Or, put the cursor on the }}} of Fold 2, and dzk kills the end of Fold 1 as well. Similar results from d[z and d]z.

zd removes the fold markers, but I want to actually grab the contents of the fold.

If I could do something like viz d, that would work great — but searches for "visual" and "folding" are giving me lots of hits about v<motion>zf, which is not what I want.

So far, the only options I have found are:

  • to delete, zc dd (close the fold, then delete the resulting single line).
  • to select in visual mode, zc V

Are those indeed the Right Things to use?

4

There is no text object for folds in vim, but there is a plugin which provides them, az/iz: https://github.com/kana/vim-textobj-fold

Here are vanilla alternatives:

  • vi{/va{. This may or may not select what you want, depending on whether your markers are in comments or not.

  • [zV]z. Alternatively you can replace V with any operator.

  • make your own: xnoremap az [zo]z, xnoremap iz [zjo]zk

3

To delete the current fold, you simply have zd for instance

Otherwise, you could also define an operator-pending mode mapping with for instance:

:onoremap iz :<c-u>normal! [zV]z<cr>

Which will permit stuff like diz, ciz, yiz, =iz and so on.

BTW, the related visual mode mapping would be:

:xnoremap iz [zo]z

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.