How can I fold every line with the same indentation as the current line (the line the cursor is on) using manual fold in vim?

Basically, sometimes I need Vim to fold things just as Vim would do when foldmethod=indent, but I prefer work with foldmethod=manual and won't change it.

For example:

function f(x)

  while (x > a)
    foo = bar
    someStuff() // suppose cursor is here
    x = foo + a

  if (x < b)
    x = b // this line should not be indented

In this case, it should fold lines foo = bar to x = foo + a.


You could use :-1,+1norm zF to specify a relative range of lines to fold. (In the case of 1 line above and one below you could omit the 1s in that command and just use :-,+norm zF).

If you're open to installing a plugin, there is vim-indent-object which gives 'indent objects' like ii for inside indent and ai for around indent (among others). Then you could do something like zfii.

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.