I have a folded section in a text document. Can I use the fold as a motion in vim, in the same way a word or WORD is a motion with w/W?

For example daw deletes around a word, so

This is a |t|est

with the cursor in ||, and in normal mode if I run the command daw will become

This is |a|

Can I do this with a folded section (using foldmethod=marker, and latex code)?

a = 10

after the fold motion would be deleted. Is this possible in other foldmethod modes?

For my question I know I can just fold the section then delete the "line", but I was wondering if the fold itself can be treated as a motion.

  • This would be a motion independent of whether the fold is folded or not – Simon Walker Apr 5 '13 at 12:06
  • Did you try daw on a closed fold? – romainl Apr 5 '13 at 13:33

I'm not aware of such a thing in Vim. But one can use [z and ]z to jump to the beginning and end of a fold.

This seems to do the trick:

xnoremap iz :<C-U>silent!normal![zV]z<CR>
onoremap iz :normal viz<CR>

You can use it like iw and friends:

| improve this answer | |

Take a look at the vim-textobj-fold plugin. It provides text objects for foldings, which I think is exactly what you are asking for.

| improve this answer | |

Linking this here since I found it useful:


Short answer is you can do this:

" Create text objects using [z and ]z
vnoremap if :<C-U>silent!normal![zjV]zk<CR>
onoremap if :normal Vif<CR>
vnoremap af :<C-U>silent!normal![zV]z<CR>
onoremap af :normal Vaf<CR>

This gives you if and af, which work like ip and ap.

| improve this answer | |

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.