Peng Zhang
Peng Zhang

Reputation: 3585

Setting IndentWidth doesn't work in clang-format

I have .clang-format in my home directory and set indentwidth 4 as following.

BasedOnStyle:  LLVM
Standard:        Cpp11
IndentWidth:     4   
TabWidth:        4   
UseTab:          Never 

But when I use clang-format -style='~/.clang-format' a.cpp to format my code, the indent width becomes 2. like:

// See this indent width 2. The original file has width 4, 
// but clang-format changes it to width 2.
int main(int argc, char const *argv[]) {
  A a;  
  a.bar();

The output of clang-format --version is

LLVM (http://llvm.org/):
  LLVM version 3.4.2
  Optimized build.
  Default target: x86_64-unknown-linux-gnu
  Host CPU: core-avx-i

How can I let clang-format format my code (.h,.c,..) with indent width 4?

Upvotes: 11

Views: 7910

Answers (1)

bames53
bames53

Reputation: 88155

http://clang.llvm.org/docs/ClangFormat.html

The -style option does not take a file path. It takes the string file to indicate the use of a .clang-format file, and it looks for that file in the parent directories of the file being processed, or the working directory and its parent directories when transforming stdin.

You can also give it a string that directly sets the options you want:

clang-format -style="{IndentWidth: 4,TabWidth: 4}"

You can also use the -dump-config option to check the config.


-style='~/.clang-format'

Using ~ to refer to your home directory generally relies on shell globbing. The shell won't do that for you inside an argument like this. So even if -style did take a path, this wouldn't produce the right path.

Upvotes: 14

Related Questions