Command line tool to format C++ code on Ubuntu

Is there any command line tool which can be used to beautify C++ file on Ubuntu? If yes, can you please suggest one?


Solution 1:

Have you tried GNU indent?

Solution 2:

"astyle" is quite good - and in my experience does a much better job than "indent" for C++.

If you are familiar with Emacs you can also use that for automatic indention from the command line. A simple Emacs script would look like this:

#!/usr/bin/emacs --script

(require 'cc-mode)

(setq require-final-newline 'visit)
(setq c-default-style "gnu")

(defun indent-files (files)
  (dolist (file files)
    (find-file file)
    (indent-region (point-min) (point-max))
    (untabify (point-min) (point-max))
    (save-buffer)
    (kill-buffer)))

(indent-files command-line-args-left)

;; EOF ;;

Solution 3:

clang-format should do the trick:

clang-format -style=Google -i path/to/file.cc

There are several styles, including LLVM, Google, Chromium, Mozilla, and WebKit. For details see clang-format -help