vim-clang-format icon indicating copy to clipboard operation
vim-clang-format copied to clipboard

how to set a global .clang-format

Open Unknown-Chinese-User opened this issue 5 years ago • 3 comments
trafficstars

I am learning C/C++ so I have many folders to store my codes. Thus I wander if I can use one .clang-format file to format all my codes to avoid copying all my .clang-format into every folder.

Unknown-Chinese-User avatar Mar 13 '20 18:03 Unknown-Chinese-User

Maybe you already have reached a solution to this.

If you are using g:clang_format#detect_style_file, one way is to define your .clang-format file in the parent directory.

Another way is to have no specific .clang-format, but using g:clang_format#style_options, but depending on how many you have, it might not be feasible.

What I am trying to describe is: https://github.com/rhysd/vim-clang-format/issues/6#issuecomment-32357977

Kypert avatar Apr 19 '20 21:04 Kypert

Another way is to use python to convert the .clang-format yaml into a single line

let g:clang_format#detect_style_file = 1
let g:clang_format#enable_fallback_style = 0

func s:load_style()
	let ret = {}
	py3 << END
import json
import os
import re

import vim
import yaml

style_file = os.path.join(os.environ['HOME'], '.clang-format')
if os.path.isfile(style_file):
	ret = vim.bindeval('ret')
	with open(style_file, encoding='utf-8') as f:
		styles = yaml.load(f.read(), Loader=yaml.CLoader)
		ret['style'] = re.sub(r'\s{2,}', ' ', yaml.dump(styles, Dumper=yaml.CDumper, default_flow_style=True).replace('"', '""').replace('\n', ''))
END
	let g:clang_format#extra_args = '-style="' . ret['style'] . '"'
endfunc
call s:load_style()

This is for Windows. For Linux the quotes string should be quoted using shlex.quote.

char101 avatar Dec 24 '20 17:12 char101