nvim 配置 clang-format

主要解决多个平台 clang-format 安装路径不同问题。

下面脚本实现了路径变了,只需改一个地方。

1
2
3
4
5
6
7
8
9
10
11
12
13
let g:clang_format_file_path="/opt/homebrew/Cellar/clang-format/18.1.8/share/clang/clang-format.py"

function! Formatonsave()
let l:formatdiff = 1
exe "py3f " .. g:clang_format_file_path
endfunction

autocmd BufWritePre *.h,*.cc,*.cpp,*.c call Formatonsave()

" 利用了 exe 来使用变量
map gcfa :exe "0,$py3file " .. g:clang_format_file_path<cr>

map gcfn :exe py3file .. g:clang_format_file_path<cr>