前提条件
安装扩展C/C++,扩展程序将自动安装clang-format。clang-format默认安装路径为"C:\Users\Users\.vscode\extensions\ms-vscode.cpptools-0.16.1\LLVM\bin\clang-format.exe"
首选项设置
打开首选项设置【ctrl + ,】
C_Cpp: Clang_format_style 决定格式化形式,若为file,则调用在workspace中的.clang-format
C_Cpp: Clang_format_fallback Style ,若上图变量为file,但无.clang-format文件则按照此处规则。
生成.clang-format
在clang-format.exe所在文件夹下打开cmd,输入:clang-format -style=llvm -dump-config > .clang-format
clang-format -style=llvm -dump-config > .clang-format
在文件夹下生成文件.clang-format,文件名可以取任何名字,一般取.clang-format或_clang-format,因为自定义的排版格式文件只有取这两种名字之一,才能被Clang-Format识别。(在Ubuntu下以点开头命名的文件是隐藏文件,所以导出为.clang-format后,在文件夹中看不到该文件,可以使用快捷键Ctrl+h显示隐藏文件。)直接将修改后的文件放在和代码文件相同的文件夹中,并且设置格式化选项-style=file,即可以使用自定义的排版格式。VS Code只要将该文件放在和代码文件相同的文件夹中即可,不需要额外的设置。将格式化文件放在代码文件的上一级文件夹中,也可以使用。注意,文件名必须为.clang-format或_clang-format。