How to add blank lines between definitions?

I successfully managed to make clang-format format my code like iIwant. However, there is one thing that bugs me:

I want a blank line between definitions of structs/classes/functions and between declarations of functions. Currently, when formatting, clang-format removes blank lines, which makes everything condensed.

Here is my file:

---
AlignAfterOpenBracket: DontAlign
AlignTrailingComments: "true"
AllowAllArgumentsOnNextLine: "false"
AllowAllConstructorInitializersOnNextLine: "true"
AllowAllParametersOfDeclarationOnNextLine: "false"
AllowShortBlocksOnASingleLine: "false"
AllowShortCaseLabelsOnASingleLine: "false"
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: None
AllowShortLoopsOnASingleLine: "false"
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: "false"
AlwaysBreakTemplateDeclarations: "Yes"
BinPackArguments: "true"
BinPackParameters: "true"
BreakBeforeTernaryOperators: "false"
BreakConstructorInitializers: AfterColon
BreakInheritanceList: AfterColon
ColumnLimit: "170"
CompactNamespaces: "false"
ConstructorInitializerAllOnOneLineOrOnePerLine: "true"
IncludeBlocks: Merge
IndentCaseLabels: "true"
IndentPPDirectives: BeforeHash
IndentWidth: "4"
IndentWrappedFunctionNames: "false"
KeepEmptyLinesAtTheStartOfBlocks: "false"
Language: Cpp
MaxEmptyLinesToKeep: "0"
NamespaceIndentation: All
PointerAlignment: Left
SortIncludes: "true"
SortUsingDeclarations: "true"
SpaceAfterCStyleCast: "false"
SpaceAfterLogicalNot: "false"
SpaceAfterTemplateKeyword: "false"
SpaceBeforeAssignmentOperators: "true"
SpaceBeforeCpp11BracedList: "false"
SpaceBeforeCtorInitializerColon: "false"
SpaceBeforeInheritanceColon: "false"
SpaceBeforeParens: Never
SpaceBeforeRangeBasedForLoopColon: "false"
SpaceInEmptyParentheses: "false"
SpacesBeforeTrailingComments: "3"
SpacesInAngles: "false"
SpacesInCStyleCastParentheses: "false"
SpacesInContainerLiterals: "false"
SpacesInParentheses: "false"
SpacesInSquareBrackets: "false"
Standard: Auto
UseTab: Always
TabWidth: "4"

Here is how it looks:

enter image description here

I want a blank line between the two structs.


You have

MaxEmptyLinesToKeep: "0"

which needs to be set to 1 instead, to ensure that multiple empty lines between definitions are not removed.

Importantly, the value needs to be an unsigned type, not a string.

So the correct setting for what you want should be

MaxEmptyLinesToKeep: 1

Note that this won't work if you don't already have at least one blank line between definitions, but from clang-format 14, you can use

SeparateDefinitionBlocks : Always

which will add an empty line between every definition. The other options are Leave which doesn't change anything, and Never, which will remove blank lines, if any.

I don't think there's an option that lets you do this in previous versions.

Source: Clang format style options.