Vitaly Isaev
Vitaly Isaev

Reputation: 5805

auto fold for C++ sources and headers

I have heard that Vim has a built-in support of folding for the files written in various programming languages. Particularly I'm interested in cpp, h, hpp files. I would like to achieve such a behavior of Vim when all the function definitions are folded by default in every newly opened source file. I don't want to create every fold manually with zF.

I added the following lines to the end of ~/.vimrc file (which was inherited from spf13-vim distribution):

set foldenable
autocmd FileType c,cpp,h,hpp setlocal foldmethod=syntax

Unfortunately that did not resulted to the desired behaviour. All the files look just the same, and my attempt to hide the function with zc combo leads to error:

E490: No fold found

I would be glad if someone could explain how to enable folding by default in Vim 7.4.

UPD: The code above works good, but there is still an issue with the recently opened files. Say we work on the project called 'sc-client-server' and we've just appended those two lines to ~/.vimrc and now ready to get back to the last edited file:

$ vim ~/.vimrc                                   //Modify config
$ reboot
$ vim ~/progs/sc-client-server/src/st.cpp        //No fold found. BUT: 
$ cp ~/progs/sc-client-server/src/st.cpp  /tmp
$ vim /tmp/st.cpp                                //Auto fold works fine. OR:
$ cd ~/progs/sc-client-server/src/ && mv src source
$ vim ~/progs/sc-client-server/source/st.cpp     //Auto fold works fine. 

Is there any kind of cache or memory inside of Vim that prevents the desired file representation?

Answer Remove all (or specific for you) files from ~/.vimviews folder.

To the off-topic voters https://meta.stackexchange.com/questions/25925/vim-questions-do-they-belong-to-stack-overflow-or-super-user/25930#25930?newreg=00a4918bba924cfa8b1d6a3e82061672

Upvotes: 3

Views: 1188

Answers (0)

Related Questions