c++vim

Auto formatting code in vim when saving .cpp file


I have read this How can I autoformat/indent C code in vim? about how to format C/C++ code, but I want to autoformat the code when I'm saving the file, like how vim-go calls gofmt when saving Golang code.

How can I configure my vimrc? I'm using clang-format, and have installed vim-clang-format with spf13vim using Vundle.


Solution

  • Found it.

    • g:clang_format#detect_style_file

    When this variable's value is 1, vim-clang-format automatically detects the style file like .clang-format or _clang-format and applies the style to formatting.

    So this code should be in the .vimrc:

    let g:clang_format#auto_format=1