3

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 Answers 4

6

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

2
  • I wound up using somini's fork of vim-textobj-fold, which has some updates that haven't made it upstream yet. Thanks!
    – cxw
    Commented Aug 17, 2018 at 12:18
  • That plugin (or the forked one suggested by @cxw) does not work for me. I get an error message on the loading of the plugin.
    – Tropilio
    Commented Apr 10, 2020 at 15:45
6

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
0

If you are on a closed fold, any regular action will apply to the whole fold.

To delete closed fold under cursor then, you need only type D or d$. To yank it to the clipboard, Y or y$.

You can similarly, for example, delete three folds from the current fold, inclusive, and place them in the system clipboard, by typing "+d3j.

0

I have defined a simple macro. Anywhere in a fold, delete the fold content:

map dz ?{{{<CR>:.+1,/}}}/-1d<CR>
1
  • 1
    Note that this relies on foldmethod=marker foldmarker={{{,}}}
    – D. Ben Knoble
    Commented Aug 25, 2022 at 16:10

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.