505

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?

ragnarswanson
  • 258
  • 3
  • 9
Kimpo
  • 5,775
  • 4
  • 22
  • 29

20 Answers20

849

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}}
]
YakovL
  • 5,213
  • 10
  • 46
  • 71
Bernd Elkemann
  • 22,657
  • 4
  • 36
  • 58
  • I think reindent should have a default key binding. In emacs you use the TAB key (no modifier) for this. I propose the alt+tab key binding as the tab key is already related to indentation commands. – gdelfino Jun 14 '12 at 14:36
  • 29
    alt+tab? that's used for switching windows! – Bernd Elkemann Jun 14 '12 at 16:02
  • 1
    Oops! It shows that I am on a Mac. Maybe the windows_key + tab? – gdelfino Jun 14 '12 at 17:08
  • no problem, in fact all those keys are already used: win+tab selects a window from the taskbar, ctrl+tab switches tabs, shift+tab unindents – Bernd Elkemann Jun 14 '12 at 17:52
  • This doesn't work for me. The short cut doesn't get set (in the menu it doesn't update the shortcut), nor does it work. – Nuno Gonçalves Dec 13 '12 at 08:29
  • 3
    @NunoGonçalves the menu does not show the new shortcut but it should work. make sure your Preferences - Keybindings-User contains exactly those lines and **save that file**. Also make sure ST knows which kind of filetype/syntax it should use (is your syntax highlighting correct?). If it still does not work try a different key (e.g. "f8" seems unused) then if that does not work either check whether user defined keybindings are disabled on your installation for some reason. – Bernd Elkemann Dec 13 '12 at 14:03
  • 2
    Having done nothing, it suddenly started working. :) I guess the problem was between the keyboard and the chair all along :) Thank you anyway. :) – Nuno Gonçalves Dec 13 '12 at 14:27
  • Mac users do use "super+h" or other binding that is not taken ... super = command button – Shehaaz Sep 18 '13 at 17:00
  • @gdelfino please don't bother proposing any shortcut again.. Win+tab is also used for switching between windows *win 7 user – Kamran Ahmed Dec 02 '13 at 09:57
  • 1
    { "keys": ["f12"], "command": "reindent", "args": { "single_line": false } } will reindent all document without need to select what you want – Alexis Apr 22 '15 at 11:43
  • And how to do that on a folder? – EpokK May 05 '15 at 06:09
  • 1
    `"f12"` is used for `"goto_definition"` in Sublime Text 3. I would recommend `"ctrl+\\"`, as it is right next to the indent `[` `]` keys. The extra "\" is for escaping. – Shah Abaz Khan Jun 02 '16 at 04:16
  • 1
    I can't understand why they don't make the indentation associated to a specific shortcut by default!! This is a very basic thing !!!! – hzitoun Jun 03 '16 at 15:47
  • Windows: `{"keys": ["alt+shift+r"], "command": "reindent", "args": {"single_line": false}}` Mac: `{ "keys": ["command+shift+r"], "command": "reindent", "args": {"single_line": false} } ` – Nik Kov Mar 18 '17 at 08:35
278

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.

Volker E.
  • 5,443
  • 11
  • 43
  • 62
Nicolas Zanotti
  • 2,941
  • 1
  • 12
  • 6
32

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

star18bit
  • 11,158
  • 2
  • 44
  • 44
  • please don't use Comma (,) at the end of the statement. It gave me an error. Type it without the comma (,) – Gihanmu Apr 19 '14 at 06:17
  • 1
    if you're not a Mac user use ctrl instead of command { "keys": ["ctrl+shift+i"], "command": "reindent"} – Juraj Petrik Feb 19 '15 at 17:58
30

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)
Community
  • 1
  • 1
TrinitronX
  • 4,518
  • 1
  • 35
  • 65
19

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

Volker E.
  • 5,443
  • 11
  • 43
  • 62
JeffBaumgardt
  • 838
  • 1
  • 11
  • 21
  • 4
    Not sure if anyone still looking at this but to respond to @jeff. Use , "args": {"single_line": false} after adding the command and then you dont need to select lines to format it. It format the entire document for you. (Nick has put in the fool command) – Mr H Mar 16 '13 at 12:53
  • NetBeans shortcut is Alt+Shift+F (by default; Ctrl+Shift+F opens Project Search), and is limited and not necessarily sensible (CSS doesn't work, and it may keep new code lines on the same line). Eclipse's shortcut is Ctrl+Shift+F, and it's better in some ways, and works on CSS, but still has an issue with new code lines. Both of them have some issues with new lines for inner-line attributes (like in HTML), though you can configure somewhat (more in Eclipse). – Davicus Oct 18 '14 at 17:57
  • cmd+a on mac or ctrl+a on windows will automatically select all the text for you in the current window. so do that, and then whatever key binding you chose for auto indenting to reformat the whole file. – user137717 Apr 29 '16 at 18:08
16

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

Volker E.
  • 5,443
  • 11
  • 43
  • 62
Sergio López
  • 953
  • 9
  • 15
7

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

Ondrej Janacek
  • 11,896
  • 14
  • 52
  • 87
Junan Chakma
  • 621
  • 6
  • 12
6

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

Community
  • 1
  • 1
Adrian Enriquez
  • 7,236
  • 7
  • 39
  • 62
6

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.

Nazim Kerimbekov
  • 3,965
  • 6
  • 23
  • 48
MindBrain
  • 6,100
  • 8
  • 45
  • 69
4

code formatter.

simple to use.


1.Install


2.press ctrl + alt + f (default)


Thats it.

Heinrich Cloete
  • 664
  • 5
  • 10
HIRA THAKUR
  • 15,044
  • 14
  • 47
  • 82
  • doesn't work for me, it just disables indentation across all lines. –  Nov 05 '15 at 07:24
  • I am using it since quite some time.Its woking fine .whats the problem. Was the installation succesful. – HIRA THAKUR Nov 05 '15 at 07:40
  • 1
    Of course yes, I am using it against HTML snippets... all the lines got their indents removed. Looking like a poem... :( –  Nov 05 '15 at 07:51
2

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.

Jon
  • 1,166
  • 11
  • 25
2

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

Nazim Kerimbekov
  • 3,965
  • 6
  • 23
  • 48
Ordiel
  • 2,151
  • 3
  • 31
  • 47
1

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.

Tshilidzi Mudau
  • 5,472
  • 6
  • 32
  • 44
Eduardo
  • 21
  • 2
1

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"}
]
Yuchen
  • 24,092
  • 18
  • 133
  • 193
1

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

{ "keys": ["f12"], "command": "reindent", "args": {"single_line": false} }  
Matthijs
  • 2,216
  • 4
  • 20
  • 33
Love Kumar
  • 722
  • 7
  • 7
0

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

evanjw
  • 49
  • 1
  • 4
0

This is my configuration for sublime-keymap:

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

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

Nazim Kerimbekov
  • 3,965
  • 6
  • 23
  • 48
Alan Dong
  • 3,443
  • 32
  • 32
0

First open the sublime text.
than open preferences.
than open Key Bindings -User.
than put the below code

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

I use CtrlShiftC and you also use other short cut key.

Misha Akopov
  • 9,542
  • 26
  • 54
  • 73
Er CEO Vora Mayur
  • 894
  • 1
  • 14
  • 24
0

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

Nazim Kerimbekov
  • 3,965
  • 6
  • 23
  • 48
bbandf
  • 1
  • 1
0
{ "keys": ["f12"], "command": "reindent", "args": {"single_line": false} } 

You can get the reindent option by using the above code

Abhishek
  • 1
  • 3