Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text 2: Auto fix indentation for javascript?

Here's some sample code I have, currently I'm set to only indent using 4 spaces at a time. Is there a way to highlight a block of javascript and press a single button or menu option to format it nicely like so:

Before:

app.get('/csvtest', function (req, res) {   MyModel.find(function (err, mymodel) {     if (!err) {       var csv = [];       _.each(mymodel, function(obj) {        csv.push(obj['mymodel']);       });       res.send(csv.join());     } else {       console.log(err);     }   }); }); 

After:

app.get('/csvtest', function (req, res) {     MyModel.find(function (err, mymodel) {         if (!err) {             var csv = [];             _.each(mymodel, function(obj) {                 csv.push(obj['mymodel']);             });             res.send(csv.join());         } else {             console.log(err);         }     }); }); 
like image 792
Robeezy Avatar asked Oct 12 '12 20:10

Robeezy


People also ask

How do I indent automatically 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.

How do I fix sublime indentations?

by pressing ctrl+f12, it will reindent your file to a tab size of 4. if you want a different tab size, you just change the "value" number.

How do you replace tabs with spaces in sublime?

Sublime Text 2 allows you to convert tabs to spaces in existing files manually (View -> Indentation -> Convert indentation to spaces). However, this can be done automatically when saving the file.

How do you justify text in Sublime Text?

Command Palette: Ctrl+Shift+P → Wrap Paragraph Justified at Ruler. Shortcut: Alt+Shift+Q.


2 Answers

Here is a tool for this. Found it on the sublime forums.

  • Install Package control
  • Run Package Control: Install Package from the command palette. Type Ctrl + Shift + P (Windows) or Command + Shift + P to open the command palette
  • Search for jsFormat and hit enter
  • Ctrl + Alt + f to format
like image 165
Robert Karl Avatar answered Oct 05 '22 10:10

Robert Karl


You could give JsFormat a go. ctrl+alt+f formats the selected text.

like image 30
Bubbles Avatar answered Oct 05 '22 11:10

Bubbles