In Eclipse, I can hit CTRL + SHIFT + F which not only corrects the indentation but also whitespace (for example: c=a +b ; is corrected to c = a + b;) and the line length. I am looking for a similar tool for Emacs. All I know is C-x h TAB that corrects the indentation, but nothing more. Do you know any automatic way how to completely format a (espacially C/C++) source file in Emacs?


1 Answer 1


You can use ClangFormat to achieve this. After installing the clang-format tool, you can use clang-format.el to perform the appropriate actions from emacs. clang-format.el is also available from MELPA. The emacs commands provided are clang-format-buffer and clang-format-region which you can bind as you need. Note that you can customize formatting options by using a .clang-format file located in an ancestor directory.

  • Ah, so the integration already exists. Note, however, that MELPA distributes a different (apparently newer) version than the one you linked to.
    – Dmitry
    Dec 7, 2014 at 5:20
  • Clang-Format is indeed a great tool, well integrated into Emacs. To have the same binding as in Eclipse you can use the following in a your C/C++ mode hook (define-key c-mode-base-map (kbd "C-S-f") 'clang-format-region). In recent versions clang-format supports also some other languages such as Javascript and Java, see LanguageKind in this page: clang.llvm.org/docs/ClangFormatStyleOptions.html Dec 7, 2014 at 16:13
  • Okay, this works nearly well, has only one weakness: spaces in closing angles in nested template arguments are removed: List<pair<int, int> > -> List<pair<int, int>> //Error! Dec 10, 2014 at 20:33
  • @user3000316 Check the options for the config value "Standard" here. You can set it to Cpp03 to fix this issue.
    – Pradhan
    Dec 10, 2014 at 21:02

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.