16

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

4 Answers 4

12

Have you tried GNU indent?

7

"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 ;;
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

2

http://www.faqs.org/docs/Linux-HOWTO/C-C++Beautifier-HOWTO.html should cover most topics of your problem:

C++ : BCPP site is at "http://dickey.his.com/bcpp/bcpp.html" or at
      "http://www.clark.net/pub/dickey" . BCPP ftp site is at 
      "ftp://dickey.his.com/bcpp/bcpp.tar.gz"

C++ : "http://www.consultix-inc.com/c++b.html"

C : "http://www.chips.navy.mil/oasys/c/" and mirror at Oasys

C++ : "http://www.semdesigns.com/Products/DMS/DMSToolkit.html"

C++, C, Java and Oracle Pro-C Beautifier:
     "http://www.geocities.com/~starkville/main.html"

C++, C beautifier "http://users.erols.com/astronaut/vim/ccb-1.07.tar.gz" 
     and site at "http://users.erols.com/astronaut/vim/#vimlinks_src"

C++, C, Java, Perl beautifier CBP "http://www.prismtk.de/docs/cbp"

GC! GreatCode! is a powerful C/C++ source code beautifier Windows 
     95/98/NT/2000 "http://perso.club-internet.fr/cbeaudet"

CbVan for C, C++ and Java at "http://www.geocities.com/~starkville/main.html"

Artistic Style beautifier for C, C++, Java at 
     "http://sourceforge.net/projects/astyle" "http://astyle.sourceforge.net".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .