42

Are there any command/shortcut to auto-indent all my code on the page at once on Sublime Text 3?

e.g: On NetBeans I can do this with Alt+Shift+F.

NG_
  • 6,895
  • 7
  • 45
  • 67
Thiago Anderson
  • 587
  • 1
  • 6
  • 13

6 Answers6

81

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
    Is there a faster way to do this? As opposed to having to write 'reindent' every time? – Jack Sep 02 '18 at 19:35
  • You can always add custom shortcuts to these commands. Check it out here http://www.sublimetext.com/docs/key-bindings – Luan Gonçalves Barbosa Sep 04 '18 at 12:32
  • 2
    it doesn't indent comments :( – user1893354 Mar 10 '19 at 19:45
  • For me in ST3, it indents whether or not I've saved the file, but it won't indent if I haven't selected the syntax. – A N Aug 12 '19 at 20:49
  • but it does not work with files having 2 space indents...even after setting for 4 space indents...it does not indent all lines to 4 space indents. Maybe it is taking indents as spaces or I don't know but I still face this issue when I open files of github and it does not indent all lines which are not in 4 space indents.. – Himanshu Bansal Jan 21 '21 at 13:53
71

Auto-indenting on Sublime Text 3 - "Key Bindings"

Just add this binding to your "Key Bindings - User" file:

Preferences > Key Bindings - users

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

And adding this code between the square brackets.

Now you can use Alt + Shift + f as your auto-indent shortcut

You can also use the "reindent" feature through the command palette. Ctrl + Shift + P


Ressources:

Auto-indenting on Sublime Text 3 by Osvaldo Zonetti

Create a Keybinding by Joe Lloyd

chebaby
  • 7,362
  • 50
  • 46
  • 1
    That's sweet. I find that this works the best and is simple - at least for html. It's the only one I've found that is really effective for HTML, but I was missing the single line argument, and I couldn't figure out why it was so erratic. Thanks for that! – dgo Apr 23 '19 at 16:47
  • 1
    It is even work until now. This is the faster and best example for enhance the code interface configuration especially when you team up to build such a complicated project. I have not notice this exclusive feature from sublime. Very nice indeed! – Marfin. F Jun 12 '19 at 07:42
3

You can use one of many plugins which do the thing you need, for example, the one I am using:

matiit
  • 7,969
  • 5
  • 41
  • 65
2

This should work as at sublime text version 3 :

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

just copy and paste to your preferences->keybindings->User file. Now select the entire code and press alt shift f to see the results.

For intending purpose you can use Ctrl plus ] and to unintend use Ctrl plus [

GeniusGeek
  • 315
  • 6
  • 14
0

Select all and then choose Edit -> Indent Document ( Ctrl + Alt + I )

Marina Dunst
  • 859
  • 1
  • 6
  • 19
0

For C/C++/C#/Java code, you can try SublimeAStyleFormatter

For PHP/JavaScript/JSON/HTML/CSS/Python/Go Code, you can try CodeFormatter

Installation

Take SublimeAStyleFormatter as an example:

  1. Bring up the Command Palette (⌘+⇧+P on OS X. Ctrl+Shift+P on Windows and Linux.).
  2. Type “Install” and select “Package Control: Install Package”.
  3. Select “SublimeAStyleFormatter” from list.

The “Packages” directory is located at:

  • Windows: %APPDATA%\Sublime Text 3\Packages
  • Linux: ~/.config/sublime-text-3/Packages/
  • OS X: ~/Library/Application Support/Sublime Text 3/Packages/

Usage

Windows, Linux:

  • Ctrl+Alt+F: Format current file.
  • Ctrl+K, Ctrl+F: Format current selection.

OSX:

  • Ctrl+Alt+F: Format current file.
  • ⌘+K, ⌘+F: Format current selection.

For example, on macOS, install the SublimeAStyleFormatter plug-in, and then press the Ctrl+Alt+F shortcut key, and then you can Autoindent.

Tzu7
  • 73
  • 6