Indenting code in Sublime text 2?

Sublimetext2Sublimetext

Sublimetext2 Problem Overview


In Visual Studio I can press Ctrl+K+D to indent everything so the code is structured nicely and readable. Is there a shortcut in Sublime 2 to do the same?

Sublimetext2 Solutions


Solution 1 - Sublimetext2

You can find it in EditLineReindent, but it does not have a shortcut by default. You can add a shortcut by going to the menu PreferencesKeybindingsUser, then add there:

{ "keys": ["f12"], "command": "reindent", "args": {"single_line": false} }	

(example of using the F12 key for that functionality)

The config files use JSON-syntax, so these curly braces have to be placed comma-separated in the square-brackets that are there by default. If you don't have any other key-bindings already, then your whole KeybindingsUser file would look like this, of course:

[
    { "keys": ["f12"], "command": "reindent", "args": {"single_line": false}}
]

Solution 2 - Sublimetext2

The reindent command only works on the currently selected lines unless the "single_line" argument is set to false.

{ "keys": ["f12"], "command": "reindent", "args": {"single_line": false} }

Now, pressing f12 will reindent the entire document.

Solution 3 - Sublimetext2

No one seems to love mac re-indentation, So here How I do it:

[   { "keys": ["command+shift+i"], "command": "reindent"}
]

In Preferences > Key Binding - User

One more extra tip: add

{ "keys": ["command+0"], "command": "focus_side_bar" }

to have sidebar file tree view navigation using keyboard.

Note: Add , at the end of each {}, if you have more than one {} set of objects

Solution 4 - Sublimetext2

There is no default shortcut for reindenting a file. However you can create one by following eznme's answer above.

You can also use the Command Palette by pressing:

  1. Control+Shift+P (or +Shift+P on a Mac)
  2. Type the first few characters of Reindent e.g: rein
  3. Press Enter to run the command
    (The first command at the top should now show Indentation: Reindent Lines)

Solution 5 - Sublimetext2

For those interested it is easy to change but for a lover of Netbeans and the auto-format you can change the key binding from F12 to ctrl+shift+F to use your beloved key binding. Sad part is that you have to select all to format the entire file. Netbeans still has the upper hand on that. If anyone knows how to overcome that limitation I'm all ears. Otherwise happy reindenting (auto-formating).

Solution 6 - Sublimetext2

To indent with the same keys like Visual Studio Ctrl+K+D (I am a Visual Studio user so I am used to this combination) I suggest:

[
{ "keys": ["ctrl+k", "ctrl+d"], "command": "reindent", "args": {"single_line": false} }
]

Write this on Preferences>Key Bindings - User

Solution 7 - Sublimetext2

It is very simple. Just go to Edit=>Line=>Reindent

Solution 8 - Sublimetext2

Netbeans like Shortcut Key

Go to Preferences > Key Bindings > User and add the code below:

[
    { "keys": ["ctrl+shift+f"], "command": "reindent", "args": {"single_line": false} }
]

Usage

Ctrl + Shift + F

Solution 9 - Sublimetext2

Select all code that you intend to indent, then hit Ctrl + ] in Sublime text to indent.

For macOS users, use command + ] to indent, and command + [ to un-indent.

Solution 10 - Sublimetext2

code formatter.

simple to use.


1.Install


2.press ctrl + alt + f (default)


Thats it.

Solution 11 - Sublimetext2

Beside of the inbuilt 'reindent' function, you can also install other plugins, such as SublimeAStyleFormatter and CodeFormatter. These plugins are better for their specify language.

Solution 12 - Sublimetext2

Just in case this stop working for anyone like me, in OS X, the command key is identified as superso it should be able to do something like this:

[
	{
	"keys": ["super+i"], 
	"command": "reindent", 
	"args": {
		"single_line": 
		false}
	} 
]

in this case using command+i is going to indent your whole code (eclipse like :) )

Solution 13 - Sublimetext2

I used to use Alt + Shift + F in NetBeans, I checked and there isn't any collision in the default keymap array of sublime, so I added it to my sublime and I'm using it without any problem.

Solution 14 - Sublimetext2

For those who like the default key binding for IntelJ IDEA, select Preferences > Settings - User:

enter image description here

And paste in the following to have the command + shift + l shortcut for auto indent:

[   { "keys": ["command+shift+l"], "command": "reindent"}
]

Solution 15 - Sublimetext2

You can add a shortcut by going to the menu PreferencesKeybindingsUser, then add there:

{ "keys": ["f12"], "command": "reindent", "args": {"single_line": false} }  

Solution 16 - Sublimetext2

For Auto-Formatting in Sublime Text 2: Install Package: Tag from Command Palette, then go to Edit -> Tag -> Auto-Format Tags on Document

Solution 17 - Sublimetext2

This is my configuration for sublime-keymap:

[
  {
    "keys": [",+=+="],
    "command": "reindent",
    "args": {
      "single_line": false
    }
  }
]

For vim people, just use ,== to reindent the whole file.

Solution 18 - Sublimetext2

Steps:

  1. Open Sublime Text.
  2. Open Preferences.
  3. Open Key Bindings -User.
  4. Put below code:
[{"keys": ["ctrl+shift+c"], "command": "reindent"},]

I use CtrlShiftC and you also use other key shortcut.

Solution 19 - Sublimetext2

Select everything, or whatever you want to re-indent and do Alt+ E+L+R. This is really quick and painless.

Solution 20 - Sublimetext2

{ "keys": ["f12"], "command": "reindent", "args": {"single_line": false} } 
 

You can get the reindent option by using the above code

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKimpoView Question on Stackoverflow
Solution 1 - Sublimetext2Bernd ElkemannView Answer on Stackoverflow
Solution 2 - Sublimetext2Nicolas ZanottiView Answer on Stackoverflow
Solution 3 - Sublimetext2DPPView Answer on Stackoverflow
Solution 4 - Sublimetext2TrinitronXView Answer on Stackoverflow
Solution 5 - Sublimetext2JeffBaumgardtView Answer on Stackoverflow
Solution 6 - Sublimetext2Sergio LópezView Answer on Stackoverflow
Solution 7 - Sublimetext2Junan ChakmaView Answer on Stackoverflow
Solution 8 - Sublimetext2Adrian EnriquezView Answer on Stackoverflow
Solution 9 - Sublimetext2MindBrainView Answer on Stackoverflow
Solution 10 - Sublimetext2HIRA THAKURView Answer on Stackoverflow
Solution 11 - Sublimetext2JonView Answer on Stackoverflow
Solution 12 - Sublimetext2OrdielView Answer on Stackoverflow
Solution 13 - Sublimetext2EduardoView Answer on Stackoverflow
Solution 14 - Sublimetext2YuchenView Answer on Stackoverflow
Solution 15 - Sublimetext2Love KumarView Answer on Stackoverflow
Solution 16 - Sublimetext2evanjwView Answer on Stackoverflow
Solution 17 - Sublimetext2Alan DongView Answer on Stackoverflow
Solution 18 - Sublimetext2Er CEO Vora MayurView Answer on Stackoverflow
Solution 19 - Sublimetext2bbandfView Answer on Stackoverflow
Solution 20 - Sublimetext2AbhishekView Answer on Stackoverflow