vim插件--代码格式化工具

2021-09-14  本文已影响0人  zlcook

配置参考

Linux下vim配置clang-format的方法

vim中代码格式化工具

安装

前提

:echo has("python3") 
:echo has("python2").
python3 -m pip install pynvim

.vimrc中添加python3的执行文件路径

let g:python3_host_prog="/path/to/python/executable/"

通过Vundle安装

Plugin 'Chiel92/vim-autoformat'

使用

配置格式化程序

let g:formatterpath = ['/some/path/to/a/folder', '/home/superman/formatters']

使用

:Autoformat
# 指定文件类型格式化
:Autoformat json
# 只格式化当前行
:AutoformatLine
au BufWrite * :Autoformat

clang-format配置

# fedora
sudo yum install clang
clang-format -dump-config -style=Google > .clang-format

会生成一个.clang-format文件,这个文件在.vimrc中可以配置为使用的格式:

let g:clang_format#command = 'clang-format'
nmap <F4> :ClangFormat<cr>
autocmd FileType c ClangFormatAutoEnable
let g:clang_format#detect_style_file = 1

可以打开任一C/C++文件,直接按F4执行clang-format了。

批量格式化目录下的文件

经常需要对一个目录下所有指定后缀的文件做批量格式化处理,指令如下:

clang-format -style=file -i `find . -type f -regex ".*\.\(cpp\|h\)"`
上一篇 下一篇

猜你喜欢

热点阅读