Multi-language command-line source code formatter [closed]
Asked Answered
S

7

15

Is there a command-line Unix tool that will format/indent/prettify source code in different languages? I'm especially interested in Java, JavaScript, PHP, and XML, but ideally it would handle others.

(I'm not looking for something to generate syntax-highlighting markup; I already know of a few tools that do that.)

Strep answered 13/11, 2009 at 19:29 Comment(5)
I posted a similar question on askubuntu: How to reformat/reindent all the source files in a given directory?Delrosario
I haven't tried it myself but I think this is promising: github.com/google/google-java-formatVera
various other options to do this can be found hereHanahanae
and in the docs of this atom beautifier which integrates with, and lists TONS of beautifiers, with a very versatile tool looking like prettydiffHanahanae
and my final comment - i wrapped up a way to do this using Neovim, and posted the answer here on SuperUser... but if you want to use vim, you might be able to get away with this one liner, vim -s <(echo "gg=G\nZZ") YOUR_FILE_HEREHanahanae
S
5

Artistic Style. http://astyle.sourceforge.net/

Sexless answered 8/11, 2010 at 19:4 Comment(2)
which can be installed using homebrew on a mac, by running brew install astyleHanahanae
and various other options to do this can be found hereHanahanae
H
4

I've always found Vim's code formatter a great option. It is aware of many languages and can be reasonably customized.

You can pipe the relevant commands into vim like this:

vim MyClass.java <<< gg=G:wq

Explanation:

  • gg=G formats the file
  • :wq saves the file and returns to the command prompt
Hammad answered 13/11, 2009 at 20:21 Comment(1)
Unfortunately this doesn't work nicely when you have several java annotations before a method (e.g. @Override, @Deprecated)Vera
P
3

If you have set your auto-formatting options as project-specific settings in Eclipse, you can do something like:

/opt/local/eclipse/eclipse -nosplash
-application org.eclipse.jdt.core.JavaCodeFormatter
-verbose
-config .settings/org.eclipse.jdt.core.prefs
src/ tests/ doc/examples/

This means that you practically install and configure Eclipse for this purpose if only for using it's autoformatting features, regardless of what editor you use normally. :)

Source: http://blogs.operationaldynamics.com/andrew/software/java-gnome/eclipse-code-format-from-command-line

Additional Notes

On Mac OS X:

/Applications/eclipse/java-oxygen/Eclipse.app/Contents/MacOS/eclipse  -nosplash -application org.eclipse.jdt.core.JavaCodeFormatter -verbose -config ~/my-eclipse-workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs MyClass.java
Pneumonia answered 23/2, 2012 at 11:47 Comment(1)
Great tip. I hope you don't mind if I add the mac equivalent.Vera
M
1

Check out indent and enscript.

Myall answered 13/11, 2009 at 20:5 Comment(0)
A
0

Vim generally has automatic syntax highlighting and is available on most Unix-based systems when you install. For formatting and indentation in Vim I use the :set autoindent and :set tabstop=4 automatically when I start it. autoindent keeps the current indentation you are at when you start a new line, and tabstop sets how much your code is indented when you press tab (only for indentation, for tab in general use shiftwidth). To have these options configured whenever you start Vim put them in a ~/.vimrc file.

Amasa answered 8/11, 2010 at 19:15 Comment(0)
T
0

For XML and HTML I have used htb.

If you are an Eclipse user then JTidy is another option.

For Java there is Jalopy.

Thegn answered 1/2, 2011 at 14:24 Comment(0)
C
0

So, I bring to your attention Style Revisor, source code formatter with GUI and command-line interface. It will be support different languages, include JavaScript and PHP. If you're interested in command-line usage - you can define your own formatting style as addon. Of course, you can also use many predefined styles. Example:

./Style\ Revisor --lang=PHP --style=GNU --path=~/to-your-project-root-dir

Currently, Style Revisor supports two languages: C and Objective-C. Welcome: http://style-revisor.com/

Sincerely.

Chrissy answered 12/3, 2012 at 16:0 Comment(2)
Indeed, I have been looking for this a long time. When it's ready for use, I will add vim-support via my autoformat plugin github.com/Chiel92/vim-autoformat. Too bad it's not open source.Ballinger
Autoformat plugin for Vim? It's cool.Chrissy

© 2022 - 2024 — McMap. All rights reserved.