Sunday, August 28, 2016

cpp formatting tool - use clang-format in gvim on osx 10.11

fixing code format is trivial... why not use some automation tool?

$ brew update
$ brew install clang-format

$ wget https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/clang-format.py
# move this py script to some folder in your path, say, here:  '/usr/share/scripts'

# follow this page, add two lines to .vimrc:   http://clang.llvm.org/docs/ClangFormat.html

map  :pyf ~/scripts/clang-format.py
imap  :pyf ~/scripts/clang-format.pyi
# put the following into ~/.clang-format

$ cat ~/.clang-format 
BasedOnStyle: Google

AlignTrailingComments: true
AllowShortIfStatementsOnASingleLine: false
BreakBeforeBraces: Allman
ColumnLimit: 0
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: true
Language: Cpp
MaxEmptyLinesToKeep: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: false
PointerBindsToType: false
SpacesBeforeTrailingComments: 1
TabWidth: 4
UseTab: Never

# how to use?
in gvim, open a cpp file, highlight some lines, eg.
5gg
5v
(now ctrl i)


Labels: , , , ,