Using clang-format in CLion

Klemen Forstnerič picture Klemen Forstnerič · Jan 7, 2016 · Viewed 23.8k times · Source

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?

Answer

Allan Hasegawa picture Allan Hasegawa · Jan 7, 2016

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.