0

I'm trying to fold my c++ code.

Google told me that I should set foldmethod=indent. But this is not what I need.

set foldmethod=indent makes all of folds happen automatically when I enter a c++ file. And zc and zo will fold/unfold all of functions. But what I need is that:

  1. Don't fold automatically when I enter a c++ file;
  2. I only want to fold the function in which the cursor is.

Is it possible to do so?

7
  • possible using a custom foldexpression. Commented Aug 24, 2020 at 7:27
  • @ChristianBrabandt any doc or link?
    – Yves
    Commented Aug 24, 2020 at 7:29
  • The first thing you need to explain is what's wrong with the defaults? I.e. foldmethod=syntax which is set by standard ftplugin?
    – Matt
    Commented Aug 24, 2020 at 7:53
  • @Matt I doubt it is set by default. At least in my vim default is manual
    – Maxim Kim
    Commented Aug 24, 2020 at 8:15
  • @MaximKim Ah, my bad. fdm=syntax is not set by default. That's my private setting.
    – Matt
    Commented Aug 24, 2020 at 8:29

1 Answer 1

2

If you are ok with set foldmethod=indent and don't need to use other methods (syntax, expr or manual) then add to your vimrc:

set foldlevelstart=99

With this option all your folds would be open for a file you edit(open).

'foldlevelstart' 'fdls' number (default: -1)
            global
            {not available when compiled without the |+folding|
            feature}
    Sets 'foldlevel' when starting to edit another buffer in a window.
    Useful to always start editing with all folds closed (value zero),
    some folds closed (one) or no folds closed (99).

For the second thing I am not sure what exactly you need, I would use zc and zo or za to close/open/toggle current fold.

PS, you might also like set foldmethod=syntax for c/cpp files:

augroup cpp | au!
    au FileType c,cpp setl fdm=syntax
augroup END

enter image description here

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.