Autoindent on Sublime Text
Asked Answered
E

6

49

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.

Epley answered 28/6, 2017 at 13:13 Comment(0)
C
88

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.

Cadmar answered 30/6, 2017 at 20:46 Comment(6)
Is there a faster way to do this? As opposed to having to write 'reindent' every time?Brake
You can always add custom shortcuts to these commands. Check it out here sublimetext.com/docs/key-bindingsRato
it doesn't indent comments :(Swamp
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.Phyliciaphylis
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..Tanhya
I installed it on Linux (build 4143), and the shortcut comes by default with the key combination Alt+Shift+F (As suggested in other answers here). I confirmed this by going to the "Preferences" > "Key Bindings" menu, a screen showed the settings "{"keys": ["alt+shift+f"], "command": "reindent", "args": { "single_line": false}}"Tankard
D
75

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

Dumps answered 21/11, 2018 at 8:35 Comment(2)
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!Ashurbanipal
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!Nefarious
K
3

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

Kyl answered 28/6, 2017 at 13:15 Comment(2)
Thanks for the tip!Epley
It works great. Also fix the 2 space to 4 space indents issue...Tanhya
T
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 [

Trimming answered 30/5, 2020 at 18:0 Comment(1)
I'm on ST3 and this doesn't work. I had to add the key binding as described by chebaby.Seeger
H
0

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

Haplography answered 6/7, 2021 at 15:57 Comment(0)
B
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.

Braunstein answered 28/6, 2023 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.