Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-language command-line source code formatter [closed]

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.)

like image 901
JW. Avatar asked Nov 13 '09 19:11

JW.


People also ask

How do I format in Eclipse?

Open the required file. Go to Source | Format Document or press Ctrl+Shift+F.

How do I format code in Visual Studio?

The code formatting is available in Visual Studio Code (VSCode) through the following shortcuts or key combinations: On Windows Shift + Alt + F. On macOS Shift + Option + F. On Linux Ctrl + Shift + I.


4 Answers

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

like image 76
user240515 Avatar answered Feb 11 '23 05:02

user240515


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
like image 24
Motin Avatar answered Feb 11 '23 05:02

Motin


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
like image 45
jheddings Avatar answered Feb 11 '23 04:02

jheddings


Check out indent and enscript.

like image 20
Ewan Todd Avatar answered Feb 11 '23 04:02

Ewan Todd