Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text 3 Code autoformatting

I've searched high and low, but I can't seem to find a plugin that makes Sublime work similar to how Visual Studio formats my code as I type it.

For example, when I write a for loop, it looks something like this:

for(int i=0;i<value.length;i++) {
    //loop body
}

As soon as I complete the loop body, Visual Studio will format it to be much more readable:

for (int i = 0; i < value.length; i++)
{
    //loop body
}

Basically, it's just adding spaces around operators in this case, but it does much more. If I write horribly indented HTML/XML code, it corrects the indentation. Arrays and multiline conditionals become much more readable.

Are there any Sublime Text 3 plugins out there that do something similar to this? Everybody seems to highly recommend the "Reindent" command, which works for the HTML/XML formatting, but it doesn't space everything out in a consistent way. JsParen looks good, but it won't work for any other language that I use, namely PHP, and it's for ST2.

like image 548
Scott Avatar asked Jan 25 '14 02:01

Scott


People also ask

How do I beautify C++ code in Sublime Text 3?

Open Sublime Text editor. Press Ctrl + Shift + P. Now select SublimeAStyleFormatter.

How do I beautify the code in Sublime Text?

To beautify your code when saving the document, set the format_on_save setting to true in HTMLPrettify. sublime-settings : Ctrl+Shift+P or Cmd+Shift+P in Linux/Windows/OS X. type htmlprettify , select Set Plugin Options.

How do I align HTML code in Sublime Text 3?

One option is to type [command] + [shift] + [p] (or the equivalent) and then type 'indentation'. The top result should be 'Indendtation: Reindent Lines'. Press [enter] and it will format the document.

How do I fix indentations in Sublime Text?

That's quite simple in Sublime. Just Ctrl+Shift+P (or Command+Shift+P on MacOS) to open the tools pallet, type reindent , and pick Indentation: Reindent Lines . It should reindent all the file you are in, just remember to save before running the command, or it may not appear.


1 Answers

CodeFormatter is one possible option for PHP. It uses the PEAR PHP_Beautifier, which you'll need to install separately. There are a bunch of configuration options detailed in the README, so you should be able to find something that suits your needs.

For C/C++/C#/Java code, you can't go wrong with SublimeAStyleFormatter, a formatter that uses the popular AStyle rules. Again, there are many options available, check the .sublime-settings file for details.

HTML-CSS-JS Prettify is what I'm using currently for those languages. It requires node.js to work, so make sure you read through the instructions carefully.

Finally, you may think I'm being facetious, but I'm really not: pay attention to style when you're coding. I work a lot in Python, where the visual presentation of the code is actually part of the syntax. Code is meant to be read, by other developers as well as by machines, and it does no one any good to try and pound out poorly-formatted, unindented code while thinking "I'll just prettify it later." Maybe your formatter doesn't fix all your mistakes, or maybe you forget, or get lazy. If you focus on the look and structure of the code, you can more easily see how the different parts fit together, and perhaps catch some bugs before they can do any harm. Set a clear style guide for yourself, and stick with it. You'll be glad you did.

like image 52
MattDMo Avatar answered Oct 15 '22 19:10

MattDMo