I installed the clang-format package from melpa.
I then added the following lines to my init file:

(require 'clang-format)

I then created a test directory, cpp-proj with one file, main.cpp.


#include <iostream>

using namespace std;

int main()
    cout << "ok\n";
    return 0;

I then created a .clang-format file in the directory using the command:

clang-format -style=webkit -dump-config > .clang-format


Language:        Cpp
# BasedOnStyle:  WebKit
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands:   false
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
  AfterClass:      false
  AfterControlStatement: false
  AfterEnum:       false
  AfterFunction:   true
  AfterNamespace:  false
  AfterObjCDeclaration: false
  AfterStruct:     false
  AfterUnion:      false
  BeforeCatch:     false
  BeforeElse:      false
  IndentBraces:    false
BreakBeforeBinaryOperators: All
BreakBeforeBraces: WebKit
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
ColumnLimit:     0
CommentPragmas:  '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat:   false
ExperimentalAutoDetectBinPacking: false
ForEachMacros:   [ foreach, Q_FOREACH, BOOST_FOREACH ]
  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
    Priority:        2
  - Regex:           '^(<|"(gtest|isl|json)/)'
    Priority:        3
  - Regex:           '.*'
    Priority:        1
IndentCaseLabels: false
IndentWidth:     4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: Inner
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments:  true
SortIncludes:    true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles:  false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard:        Cpp03
TabWidth:        8
UseTab:          Never

I then went back into my main.cpp file, with the whole buffer selected and issued the command Meta-x clang-format-region.
The result:


#include <iostream>

using namespace std;

int main() {
  cout << "ok\n" << endl;
  return 0;

This is not correct.

It should be:


#include <iostream>

using namespace std;

int main()
    cout << "ok\n"
         << endl;
    return 0;

No matter what style I choose it doesn't work within emacs.

Am I doing something wrong configuring clang-format?
It spits out the correct format when I issue the command
clang-format -style="<style>" main.cpp in a terminal, but never in Emacs!
What gives?

  • The easiest explanation for this is that the config file is not honored. Some more systematic debugging is necessary.
    – wasamasa
    Commented Jul 22, 2018 at 8:01

1 Answer 1


I found the same thing (not following my styles as defined in a .clang-format file, which happens to be in the parent directory in my case), and I think that the problem is in the way that the assume-file-name argument is defaulted if absent when calling the function clang-format-region from clang-format.el:

(unless assume-file-name (setq assume-file-name buffer-file-name))

Further down, this is passed to the -assume-filename command-line option of clang-format. This means that we ask clang-format to look in files with the same name as the current buffer for the style information, which didn't seem like a good default to me. I therefore experimentally altered the default to be .clang-format, thus:

(unless assume-file-name (setq assume-file-name '".clang-format"))

That way we look in the "usual place" for the style info. In my case this solved the problem and my style file is honoured when running clang-format via the emacs function.

It might be preferable to alter the arguments which are passed to the clang-format-region function instead of altering the default like this, but to me .clang-format felt like a more sensible default than the name of the current buffer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.