ChatGPT解决这个技术问题 Extra ChatGPT

Indenting code in Sublime text 2?

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?

this is auto indenting not auto formatting

Y
YakovL

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}}
]

alt+tab? that's used for switching windows!
Oops! It shows that I am on a Mac. Maybe the windows_key + tab?
@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.
Having done nothing, it suddenly started working. :) I guess the problem was between the keyboard and the chair all along :) Thank you anyway. :)
{ "keys": ["f12"], "command": "reindent", "args": { "single_line": false } } will reindent all document without need to select what you want
V
Volker E.

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.


"current line" can be a selection with multiple lines in it.
This should be the accepted answer (or at least merged into the accepted one), since in Visual Studio Ctrl+K+D will also reformat the entire document.
swap out ["f12"] for ["ctrl+k", "ctrl+d"] for the two-press chord.
[ { "keys": ["ctrl+shift+f"], "command": "reindent", "args": {"single_line": false} } ] This is my customize, thanks so much!!!
Thank you for the "plus" ( "args": {"single_line": false} ) very usefull !
D
DPP

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


please don't use Comma (,) at the end of the statement. It gave me an error. Type it without the comma (,)
if you're not a Mac user use ctrl instead of command { "keys": ["ctrl+shift+i"], "command": "reindent"}
C
Community

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:

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


V
Volker E.

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


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)
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).
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.
V
Volker E.

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


O
Ondrej Janacek

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


C
Community

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


N
Nazim Kerimbekov

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.


H
Heinrich Cloete

code formatter.

simple to use.

1.Install

2.press ctrl + alt + f (default)

Thats it.


doesn't work for me, it just disables indentation across all lines.
I am using it since quite some time.Its woking fine .whats the problem. Was the installation succesful.
Of course yes, I am using it against HTML snippets... all the lines got their indents removed. Looking like a poem... :(
J
Jon

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.


N
Nazim Kerimbekov

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


T
Tshilidzi Mudau

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.


Y
Yuchen

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

https://i.stack.imgur.com/Q7y7O.png

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

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

M
Matthijs

You can add a shortcut by going to the menu Preferences → Keybindings → User, then add there:

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

e
evanjw

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


N
Nazim Kerimbekov

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


N
Nazim Kerimbekov

This is my configuration for sublime-keymap:

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

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


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

You can get the reindent option by using the above code


A
Audwin Oyong

Steps:

Open Sublime Text. Open Preferences. Open Key Bindings -User. Put below code:

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

I use CtrlShiftC and you also use other key shortcut.