Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format C/C++ code on save

The formatting options of Eclipse avoids me lots of conflicts with other users, and ensure that my code always looks "clean" (especially regarding my git commits).

I know about the "Format on save" option in Java editor, but I could not find one in C/C++.

Is this deliberate?

May I expect to see such option one day? Or maybe I haven't looked enough?

like image 424
Goulou Avatar asked Mar 21 '11 13:03

Goulou


People also ask

How do I format C code?

The C/C++ extension for Visual Studio Code supports source code formatting using clang-format which is included with the extension. You can format an entire file with Format Document (Ctrl+Shift+I) or just the current selection with Format Selection (Ctrl+K Ctrl+F) in right-click context menu.

How do I automatically format code in Visual Studio Code?

The code formatting is available in Visual Studio Code through the following shortcuts: On Windows Shift + Alt + F. On Mac Shift + Option + F. On Linux Ctrl + Shift + I.

Does prettier work with C?

Prettier wasn't intended to format C++ code. You should install the C/C++ for Visual Studio Code extension developed by Microsoft, it supports code formatting.

How do you format clion?

Go to Settings/Preferences | Editor | Code Style, select your programming language, and open the Wrapping and Braces tab. In the Keep when reformatting section, select the formatting rules which you want to ignore and deselect those which should be applied. Reformat your code ( Ctrl+Alt+L ).


2 Answers

I know about the "Format on save" option in Java editor, but I could not find one in C/C++ ... Or maybe I haven't looked enough?

I'm afraid that option is simply not available for the CDT C++ editor (Eclipse CDT Juno Service Release 1):

Eclipse Juno C++ CDT Text Editor Save Action Options

That's everything that's available at Eclipses IDE Level.

There are two solutions you might consider to solve your concerns:

  1. You might consider adopting some discipline as mentioned in the other answers
  2. Running an appropriate script to format your source files before committing them. The AStyle tool might be helpful for this purpose.

One could also think about an automatic pre-checkin/-commit interception running a script for the actually used Revision Control System. Many revision/version control systems offer such feature.

May I expect to see such option one day?

Did you ask for this feature on the Eclipse/CDT community forum, or placed a feature request in the Eclipse CDT bugzilla tracking system? If no one knows about such feature demand and whether it is demanded by a majority of users, chances that this will be implemented by 'accident' are low.

like image 151
πάντα ῥεῖ Avatar answered Oct 08 '22 15:10

πάντα ῥεῖ


You can use the keys "Ctrl+Shift+F" to format your edited c/c++ file.

like image 44
Cédric Julien Avatar answered Oct 08 '22 16:10

Cédric Julien