Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to set a global .clang-format #95

Open
Unknown-Chinese-User opened this issue Mar 13, 2020 · 3 comments
Open

how to set a global .clang-format #95

Unknown-Chinese-User opened this issue Mar 13, 2020 · 3 comments

Comments

@Unknown-Chinese-User
Copy link

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.

@Kypert
Copy link

Kypert commented Apr 19, 2020

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: #6 (comment)

@char101
Copy link

char101 commented Dec 24, 2020

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.

@sandrain
Copy link

sandrain commented Jul 17, 2024

This is an old topic, but this gives me error (clang-format has failed to format.):

let g:clang_format#extra_args = '-style=file:/home/myself/.clang-format'

The style file works on the command line:

clang-format -style=file:/home/myself/.clang-format test.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants