Sublime Text 2: Automatically indent javascript?

Here is an example of the code that I have, currently I am only set to indent using 4 spaces at a time. Is there a way to select a javascript block and press a single button or menu item to format it like this:

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); } }); }); 
+59
javascript sublimetext2
Oct 12
source share
7 answers

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

  • Install package management
  • Start package management: install a package from the command palette. Type Ctrl + Shift + P (Windows) or Command + Shift + P to open the command palette
  • Find jsFormat and press enter
+121
Oct 12 '12 at 21:00
source share

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

+35
Oct 12 '12 at 21:00
source share

You can select all your code ( ctrl + A ) and use the functionality in the Reindent application (Edit → Line → Reindent) . It will format your code by looking at the Sublime / Intent tab.

Alternatively: You can use the JsFormat formatting plugin for Sublime Text 2 if you would like to have more custom settings on how to format your code to add default Sublime Text to the tab / indent tab.

https://github.com/jdc0589/JsFormat

More on how to install JsFormat in your Sublime IDE:. You can easily install JsFormat using package management (Preferences → Package Control). Open package management, then type install, press enter. Then type "js format" and press enter , you're done. (The package controller will show the installation status with success and errors in the lower left pane of Sublime)

Abbreviation settings: Add the following lines to your key bindings (Preferences → Keyboard Binding User)

 { "keys": ["ctrl+alt+2"], "command": "js_format"} 

I use ctrl + alt + 2 , you can change this short cut key as you want.

My opinion: JsFormat - good, definitely worth a try!

+29
Jun 24 '13 at 23:41
source share

If you specifically want to go from 2 to 4 spaces, click on the tab menu in the lower right corner. Click "convert indentation to tabs", change the width to 4, then "convert the indentation to spaces".

+6
Oct 12
source share

The fastest way to do this, in general, is with regex:

  • Press CTRL + H
  • Turn on the Regular Expression button in the lower left corner (or press ALT + R )
  • Type ^(\s+) in Find What
  • Enter \1\1 in the "Replace with
  • Press Replace All Right

This will double the number of prefix spaces (with 2 spaces to 4). The replacement window can be left open to easily apply it to multiple files.

+2
Feb 04 '15 at 14:12
source share

It seems that Sublime Text 2 already has what you want (maybe they added this feature more recently).

If you want to change the number of spaces or want to convert spaces to tabs, you can use this path: View> Indent

In this drop-down menu, you can select Convert indentation to tabs , Convert indentation to spaces, or select the number of spaces Tab width (<1-8) .

Hope this helps!

+1
Feb 18 '16 at 20:11
source share

Install jsFormat using PackageControl by selecting jsFormat from the Install Package menu.

Then do this to automatically format your code:

Ctrl + Alt + F

It is also useful to review jsLint's guidelines for formatting. You can install the jsLint package and run the check with the formatting options turned on.

Ctrl + L

0
May 22 '15 at 19:24
source share



All Articles