vim 配置 clang-format

在ubuntu14.04下完成配置

第一步:安装clang-format

sudo apt-get install clang-format-3.x

第二步:二进制文件拷贝

拷贝一个不带版本号的二进制,实际上就是重命名

sudo cp /usr/bin/clang-format-3.x /usr/bin/clang-format

截止目前,已经可以在shell使用clang-format
例如:

clang-format main.cpp -style=LLVM

第三步:配置vim

有很多方式,我才用了官网的方式
http://clang.llvm.org/docs/ClangFormat.html

具体流程:

1)下载clang-format.py
2)在vimrc中添加
map <C-K> :pyf <path-to-this-file>/clang-format.py<cr>

imap <C-K> <c-o>:pyf <path-to-this-file>/clang-format.py<cr>

至此完成的功能有:
normal模式下,ctrl+k将格式化一行代码
visual模式下,ctrl+k将格式化选中代码
insert模式下,ctrl+k将格式化一行代码

打开一个文件,发现提示:没有.clang-format文件,默认将使用llvm风格,我们可以才当前目录下创建一个.clang-format:
这里我抄了一个腾讯的文件

---
BasedOnStyle: LLVM
IndentWidth: 4
TabWidth: 4
AlwaysBreakTemplateDeclarations: true
AllowShortFunctionsOnASingleLine: Inline
BreakAfterJavaFieldAnnotations: true
BreakBeforeBraces: Linux
SpaceAfterCStyleCast: true
IndentCaseLabels: true
AccessModifierOffset: -4
BreakBeforeBraces: Custom
BraceWrapping:
    AfterNamespace: false
    AfterClass: false
    AfterFunction: true

BreakConstructorInitializersBeforeComma: true
ConstructorInitializerAllOnOneLineOrOnePerLine: true
BinPackParameters: false
ReflowComments: false
---
Language: Cpp
ColumnLimit: 80
---