-
安装vim-clang-format插件
项目地址:vim-clang-format
如果用Plug管理vim插件,直接在.vimrc中添加一行
Plug ‘rhysd/vim-clang-format’
运行:PlugInstall就可以自动安装插件 -
配置
支持的语言
C
C++
Objective-C
JavaScript
java
TypeScript
Protobuf
" map to <Leader>cf in C++ code
autocmd FileType h,cc,c,cpp,objc nnoremap <buffer><Leader>cf :<C-u>ClangFormat<CR>
autocmd FileType h,cc,c,cpp,objc vnoremap <buffer><Leader>cf :ClangFormat<CR>
" let g:clang_format#auto_format_on_insert_leave=1 "退出插入模式时自动格式化
- 安装clang-format:
npm install -g clang-format - 格式化方式
可以创建一个文件.clang-format放到根目录
可以参考: https://www.cnblogs.com/lepeCoder/p/8032178.html
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
# To disable for a line use `// clang-format off`
# IndentPPDirectives: BeforeHash
#WebKit:符合WebKit的样式指南的样式
AccessModifierOffset: -1
ConstructorInitializerIndentWidth: 4
AlignEscapedNewlinesLeft: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakTemplateDeclarations: true
AlwaysBreakBeforeMultilineStrings: true
BreakBeforeBinaryOperators: false
BreakBeforeTernaryOperators: true
BinPackParameters: true
ConstructorInitializerAllOnOneLineOrOnePerLine: true
DerivePointerBinding: true
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: true
NamespaceIndentation: None
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 60
PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerBindsToType: true
SpacesBeforeTrailingComments: 2
Cpp11BracedListStyle: true
Standard: Auto
BreakBeforeBraces: Attach
IndentFunctionDeclarationAfterType: true
SpacesInAngles: false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpaceAfterControlStatementKeyword: true
ContinuationIndentWidth: 4
#基于那个配置文件
BasedOnStyle: Google # https://google.github.io/styleguide/cppguide.html
#指针的*的挨着哪边
PointerAlignment: Right
#缩进宽度
IndentWidth: 2
# 连续的空行保留几行
MaxEmptyLinesToKeep: 1
#SYP # 在 @property 后面添加空格, \@property (readonly) 而不是 \@property(readonly).
#SYP ObjCSpaceAfterProperty: true
#SYP # OC block后面的缩进
#SYP ObjCBlockIndentWidth: 4
#SYP # 是否允许短方法单行
#SYP AllowShortFunctionsOnASingleLine: true
# 是否允许短if单行 If true, if (a) return; 可以放到同一行
AllowShortIfStatementsOnASingleLine: true
#注释对齐
AlignTrailingComments: true
#SYP # 换行的时候对齐操作符
#SYP #AlignOperands: true
# 小括号两边添加空格
SpacesInParentheses : false
#SYP #多行声明语句按照=对齐
#SYP AlignConsecutiveDeclarations: true
#连续的赋值语句以 = 为中心对齐
AlignConsecutiveAssignments: true
#等号两边的空格
SpaceBeforeAssignmentOperators: true
#SYP # 容器类的空格 例如 OC的字典
#SYP SpacesInContainerLiterals: true
#SYP #缩进
#SYP IndentWrappedFunctionNames: true
#SYP #在block从空行开始
#SYP KeepEmptyLinesAtTheStartOfBlocks: true
#在构造函数初始化时按逗号断行,并以冒号对齐
BreakConstructorInitializersBeforeComma: true
#函数参数换行
AllowAllParametersOfDeclarationOnNextLine: true
#SYP #括号后添加空格
#SYP SpaceAfterCStyleCast: true
#tab键盘的宽度
TabWidth: 4
UseTab: Never
ColumnLimit: 110
大功告成