1

I am in the process of customizing my vim setup, and since I write a lot in C, I have a whole function dedicated just for those files, which I call with autocmd filetype c,cpp,h,hpp call C_syntax().

The issue I'm having is that I want auto folding to occur on .c and .cpp files, but not .h and .hpp files. In order to do this, I have tried:

function C_syntax()
    autocmd filetype c,cpp setlocal foldmethod=syntax
    "---snip---
endfunction

which disables folding for all files (.c and .cpp included), as well as this:

autocmd filetype c,cpp,h,hpp call C_syntax()
autocmd filetype c,cpp setlocal foldmethod=syntax

which enables it for .h and .hpp files as well.

How would I go about enabling the folding for only .c and .cpp files?

Thanks for any help

2 Answers 2

2

In a c ftplugin, check (:h :if) for the extension of the current buffer (with : h expand ()). If you match a header file don't set the option. Otherwise do.

You can define the thing with autocommands, but it's easy to get it wrong, beside it doesn't scale well

1

You can define the autocommands using the file extension instead of the filetype as following:

autocmd BufNewFile,BufRead *.c,*.cpp,*.h,*.hpp call C_syntax()
autocmd BufNewFile,BufRead *.c,*.cpp setlocal foldmethod=syntax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.