Is there a utility to indent C++ programs [closed]

2019-02-05 20:43发布

问题:

I am trying to use "indent" program to indent C++ programs. But it does not seem to work fine. It is messing up the indentation much more.

It is a Class file. Can you please suggest the right options for it or another program that works?

Thanks

回答1:

Try Artistic Style:

Artistic Style is a source code indenter, formatter, and beautifier for the C, C++, C# and Java programming languages.



回答2:

in visual studio, CTRL+a, CTRL+k, CTRL+f will auto-indent the entire file you're working in.



回答3:

Another tool for automated formatting of C++ code is clang-format.



回答4:

Many editors like Vim and Emacs have a feature that reindents code according to its built-in indentation standard.

In Vim, you can type = with any motion. (Like =% at any block delimiter or S-v to select a region and then = to reformat.) The gq command can be used to reformat comments and other text.

In Emacs, you can type M-x indent-region after selecting a region. (Such as with M-< and M->.) You can also reformat comments with M-x fill-paragraph.



回答5:

http://www.faqs.org/docs/Linux-HOWTO/C-C++Beautifier-HOWTO.html



回答6:

This is in reply to the OP and other posters: I work under Windows and was looking for an indent as well. This is what I learned:

I tried bcpp. It seems to be quite old (more or less from 1996) and you need to find a windows exe. I found one here: http://www.zeusedit.com/zforum/viewtopic.php?t=1067. Still I think it's not really worth the bother, since it can only do space/tab, indentation level and brace conversion.

Then there is Artistic Style, which is newer and on sourceforge. It seems to be quite capable, but it can not, for example, reformat spaces around function names and parameters (like, turn if ( a ( b ) ) into if (a(b)).

I like Cygwin indent ( http://www.cygwin.com/ ) best, but it works only well on newline separated files, so you have to use it like "dos2unix < infile | indent ...". But indent will not reformat boxed comments... :)

Maybe a combi of Artistic Style and Cygwin indent works best. Now I use this command line: astyle --style=kr -s3 --break-closing-brackets < infile.cpp | dos2unix | indent --no-tabs --k-and-r-style --indent-level 3 --dont-cuddle-else | unix2dos. I bit long, I agree, but apart from that it produces a very good result. :)



回答7:

bcpp, available in any linux distro (and probably for mingw/cygwin, but I have not checked it) is a C++ indenter and beautifier. You can parametrize how it works with the code.



回答8:

Another short key in Visual Studio 2008:

Ctrl+A, Alt+F8



回答9:

In netbeans, you can simply do

Alt + Shift + F

Please make sure that the plugins are installed for corresponding language. For example, for PHP code indentation, you have to choose netbeans PHP or install PHP plugins from repository.