Autofold #ifdef .. # endif in vim via .vimrc

I saw partial solutions on the Internet, but none of them satisfied me: what I need to put in my .vimrc (ideally, I do not want to use any syntax of the / *. Vim file), such that in general. c / cpp / h files that I open, I get automatic folding sections #ifdef ... #endif ?

+6
source share
2 answers

If you only need the folding type, this is the #ifdef section, the easiest way is to create a ~/.vim/after/ftplugin/c.vim (you may also need to do this in cpp.vim , I'm not sure) with the following content:

 set foldmarker=#ifdef,#endif set foldmethod=marker 

If you really want to put it in .vimrc instead of using the ~/.vim/after/ structure, you can do something like this:

 autocmd FileType *.[ch]{,pp} call FoldPreprocessor() function! FoldPreprocessor() set foldmarker=#ifdef,#endif set foldmethod=marker endfunction 

You may also consider using:

 set foldmarker=#if,#endif 

Since it will catch #if defined(...) , #ifdef , #ifndef , #if 0 etc., as well as #ifdef .

Doing this with syntax folding is more complicated since you have to change the syntax specification as it does not support this as a standard.

+6
source

Do you know that you can move conditional preprocessor blocks with the % key?

In addition, [# and ] # move up / down.

So, you can go to the beginning of the conditional block (possibly with / ^# Enter ), then

  zf% -- fold to next conditional directive v2]#zf -- fold to second next directive (eg #else... #endif) 

zd to fold folds.

Perhaps you can come up with a little script around this concept. I'm not very sure if there will be an (adverse) interaction with regular syntax folding, since I'm not used to it. I usually use padding for padding with manual manipulation of the palette.

+6
source

Source: https://habr.com/ru/post/891645/


All Articles