1

I like to use vim to program c and c++, but I am disappointed to use the folding scheme, because I have a lot of definition in C.

Such as

CONFIG_A_DEFINEDED
CONFIG_B_DEFINEDED
CONFIG_C_DEFINEDED

For example:

#if config_a_defineded
..
#endif

#if config_b_defineded
..
#endif

...

My question is: Are there ways for folding in vim to use sting predefined in files or something like that? Suggestions or any kind of help is highly appreciated.

BoJack Horseman
  • 4,406
  • 13
  • 38
  • 70
kevinpark
  • 19
  • 2

1 Answers1

0

Create a file ~/.vim/after/syntax/c.vim and put into it:

if !exists("c_no_ifdef_fold")
  syn region cPreIfdefRegion start="^\s*\(%:\|#\)\s*\(if\|ifdef\|ifndef\|elif\)\>" end="^\s*\(%:\|#\)\s*\(else\|endif\)\>" fold transparent
endif

When loading a c syntax file, set your foldmethod to syntax. Use the variable c_no_ifdef_fold to disable this behaviour.

Read the help at :h ft-c-syntax and also :h after-directory for details.

Christian Brabandt
  • 8,038
  • 1
  • 28
  • 32
  • Thanks for helping me out. But My aim is to fold out selectively in predefined file or some place. Unfortunately all `#ifdef..#endif` are folded. so sad.. – kevinpark Sep 03 '15 at 22:24
  • @kevinpark Then you need to write a custom foldexpr. It's not hard. Read `:h fold-expr` it has many examples – Christian Brabandt Sep 06 '15 at 10:22