I'd like to format my code using clang-format in CLion. I've gone through the settings and haven't found any setting that would allow me to run an external command to transform code. How are such things normally done in CLion?
-
1Related CLion issue https://youtrack.jetbrains.com/issue/CPP-4805 – KindDragon Dec 04 '18 at 10:55
4 Answers
You can use External Tools in CLion.
Go to File->Settings->Tools->External Tools
and click on the plus sign.
A window should pop up. Use a name of your choice.
For the Tool settings
tab I'm using this configuration:
Program:
clang-format-3.7
(you should use the name of your executable here)Parameters:
-i $FileName$
Working directory:
$FileDir$
Now, with your file open, you can go to Tools->External tools
and run the config above. It basically calls clang-format and does inplace formatting.
You can also set a custom keymap to it, just search the name of your external tool in "Keymap" of the Settings menu.

- 1,461
- 12
- 17

- 988
- 10
- 12
-
3
-
2Is it possible to aply it to all files? Is there any clang-format to Clion formating options guide? – DuckQueen Jun 21 '16 at 01:23
-
@DuckQueen: clang-format uses the LLVM style by default, but it can also use others. CLion supports most, if not all, of those styles, which you can set like this: https://twitter.com/clion_ide/status/758203475704635392 – izym Aug 03 '16 at 18:19
-
1I was struggling to get clang-format to run whenever you save a file. These instructions were helpful: https://marcesher.com/2014/03/30/intellij-idea-run-goimports-on-file-save/ – davidvandebunte Apr 19 '17 at 23:03
The lates version of CLion 2019.1 has native support for ClangFormat.
For previous version go to File->Preferences->Plugins
and search for ClangFormatIJ
. Install this plugin.
It installs the context menu to invoke locally installed clang-format
for a file or for a selection. It also set up the key shortcut for 'Reformat current Statement with clang-format' action.
This option works quite well for me in CLion 218.3 however, it might be included in an early version as well.

- 789
- 5
- 14
-
ClangFormatIJ is super slow... I mean when I press shortcut for code format it takes sometimes 1-3 secs to update code... If you save file during formating it will break your code (cut some parts or other odd things). It also hangs clion when you press it multiple times... Some kind deadlock I think. [Check for issues](https://github.com/mprobst/ClangFormatIJ/issues) – Gelldur Jan 16 '19 at 14:56
-
1Probably it depends. I never run in that problem. Good news is that everybody can looking forward for native support of clang format in next release. Here is a already planned task [CPP-4809](https://youtrack.jetbrains.com/issue/CPP-4809) – frido Jan 17 '19 at 13:26
The previous answers work well, but do not allow executing clang-format on save without the use of workarounds which don't work with the Vim emulation plugin I am using.
Here is a solution which executes clang-format on save and works well in every situation that I have encountered.
- First you need to install the
File Watchers
plugin. - From there you need to go to
File->Settings->Tools->File Watchers
And create a custom template I use the following settings to execute
clang-format
when one of the currently open files is saved. You can tweak these settings to match your needs- For example, if you wish to execute
clang-format
automatically whenever the file changes (without needing to save) you can enableAdvanced Options->Auto-save edited files to trigger the watcher
- For example, if you wish to execute
Documentation for the File Watchers plugin: https://www.jetbrains.com/help/clion/using-file-watchers.html

- 4,124
- 4
- 30
- 42
Since CLion 191 EAP ClangFormat is supported from the box.

- 1,451
- 10
- 12
-
-
For clion 2023.1 or nearly, please refers to https://www.jetbrains.com/help/clion/clangformat-as-alternative-formatter.html – YuTang May 02 '23 at 16:17