linux vim(nvim)c++代码格式化工具clang-format

  1. 安装vim-clang-format插件
    项目地址:vim-clang-format
    如果用Plug管理vim插件,直接在.vimrc中添加一行
    Plug ‘rhysd/vim-clang-format’
    运行:PlugInstall就可以自动安装插件

  2. 配置
    支持的语言
    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	"退出插入模式时自动格式化
  1. 安装clang-format:
    npm install -g clang-format
  2. 格式化方式
    可以创建一个文件.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


大功告成

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值