windows下sublime text 3使用clang format格式化C++代码

update:

我发现windows系统直接在sublime text 3使用clang format插件有很大的bug,效果跟直接运行clang-format命令行结果不一样,经常不格式化或者部分格式化,可以考虑通过插件机制直接运行命令行对文件进行格式化:

sublime text自定义clang format插件格式化C++代码_Luchang-Li的博客-CSDN博客

ref: 

Windows下clang-format的安装_wanlong1215的博客-CSDN博客_clang-format windows

https://gist.github.com/danielTobon43/51764026f95240bbd03991089f0380a8

llvm下载:

https://github.com/llvm/llvm-project/releases

Download LLVM releases

安装后

在sublime text package control里面安装clang format插件

然后设置该插件:

Setting up clang-format

  • Go to Preferences->Package Control: Install Package
  • Install Clang Format
  • Go to Preferences->Package Settings->Clang Format->Setting-User, set as following:
{
    "binary": "C:/Program Files/LLVM/bin/clang-format.exe",
    "format_on_save": true,
    "style": "Custom",
}
  • Go to Preferences->Package Settings->Clang Format->Custom Style-User, add:
{
    "Language": "Cpp",
    "TabWidth": 4,
    "AlignTrailingComments": "true",
    "UseTab": "Never",
}

可以把default的拷过来改。 

  • Save and restart sublime text
  • Go to any script page, save it and see the difference

最后设置该插件的快捷键key bindings,使用该快捷键进行代码格式化(例如复制默认key bindings然后修改为ctrl+alt+r)

linux下代码格式化和保存配置文件

apt-get install clang-format-7 (其他系统版本可能apt-get install clang-format)

clang-format-7 -i xxx.cpp -style=Google

保存配置文件: 

clang-format -style=Google -dump-config > .clang-format

配置文件修改

tab size

SortIncludes: false

加载配置文件进行格式化(自动从当前向上层目录寻找.clang-format配置文件):

clang-format-7 -style=file -i xxx.cpp 

clang 14.0.0-RC1 or later now supports -style=file:<format_file_path> after this change "Add option to explicitly specify a config file" entered the main branch. It's documented here.

.\LLVM14.0.0RC1\bin\clang-format.exe test.cpp -style=file:my_clang_format.txt

c++ - How do I specify a clang-format file? - Stack Overflow

猜你喜欢

转载自blog.csdn.net/u013701860/article/details/124396897