415

How do you auto-indent your code in the Atom editor? In other editors you can usually select some code and auto-indent it.

Is there a keyboard shortcut as well?

Flip
  • 6,233
  • 7
  • 46
  • 75
Anders
  • 9,988
  • 7
  • 30
  • 36

13 Answers13

688

I found the option in the menu, under Edit > Lines > Auto Indent. It doesn't seem to have a default keymap bound.

You could try to add a key mapping (Atom > Open Your Keymap [on Windows: File > Settings > Keybindings > "your keymap file"]) like this one:

'atom-text-editor':
  'cmd-alt-l': 'editor:auto-indent'

It worked for me :)


For Windows:

'atom-text-editor':
  'ctrl-alt-l': 'editor:auto-indent'
kchetan
  • 4,987
  • 2
  • 18
  • 17
Nacho L.
  • 9,582
  • 2
  • 25
  • 25
  • 1
    Thank you! Would be nice if it did a core:select-all right before, but no clue on how to script that. – doobdargent May 14 '14 at 08:38
  • 3
    @doobdargent I created a question with an answer on how to compose two commands into a new command here: http://stackoverflow.com/questions/24456995/how-do-i-write-a-custom-command-in-atom/24456996 – Lee Jun 27 '14 at 16:50
  • I think me keymap stopped working after some upgrade. Had to change `.atom-text-editor` to `atom-text-editor` (remove class dot) – Mattias Wadman Jul 29 '15 at 13:48
  • 18
    You can also `cmd + shift + p` and search for `Editor: Auto Indent` – codingninja Oct 01 '15 at 20:20
  • 1
    I figured out a way to bind this approach with "Select All" first (and then restore the original selection after), so you can do it all in one key press -- see my answer below. http://stackoverflow.com/a/33927654/398630 – BrainSlugs83 Nov 25 '15 at 22:39
  • Works well thanks! `Cmd + a` then `Cmd + shift + a` (auto reindent) is so awesome! – Maxime Lafarie Dec 15 '15 at 16:46
  • 4
    For ubuntu, its [Edit > Open Your Keymap]. And as `ctrl-alt-l` is ubuntu's default shortcut for lock screen, I'm mapping with `ctrl-shift-]` for now. – vusan May 02 '16 at 11:51
  • I had to save the file with extension to make it work. Don't know why! – Subinoy Jun 15 '17 at 07:53
  • Wow - I searched for it - found it and solved it. That was fast ;) Thanks. – Slowwie Feb 09 '20 at 08:12
  • for ubuntu 18.04 with atom 1.26.1 -> Go to Edit -> Keymaps -> `add 'atom-text-editor': 'ctrl-alt-]': 'editor:auto-indent'`. After this select all your unindented texts and apply `ctrl+alt+]` and you are done. – Kevin Patel Mar 04 '20 at 12:22
  • Hi, where can I find a list of editor keymap options? – Souleste Aug 19 '21 at 18:19
101

The accepted answer works, but you have to do a "Select All" first -- every time -- and I'm way too lazy for that.

And it turns out, it's not super trivial -- I figured I'd post this here in an attempt to save like-minded individuals the 30 minutes it takes to track all this down. -- Also note: this approach restores the original selection when it's done (and it happens so fast, you don't even notice the selection was ever changed).

1.) First, add a custom command to your init script (File->Open Your Init Script, then paste this at the bottom):

atom.commands.add 'atom-text-editor', 'custom:reformat', ->
    editor = atom.workspace.getActiveTextEditor();
    oldRanges = editor.getSelectedBufferRanges();
    editor.selectAll();
    atom.commands.dispatch(atom.views.getView(editor), 'editor:auto-indent')
    editor.setSelectedBufferRanges(oldRanges);

2.) Bind "custom:reformat" to a key (File->Open Your Keymap, then paste this at the bottom):

'atom-text-editor':
    'ctrl-alt-d': 'custom:reformat'

3.) Restart Atom (the init.coffee script only runs when atom is first launched).

BrainSlugs83
  • 6,214
  • 7
  • 50
  • 56
  • Interesting. I'm not sure I'd want it to automatically auto indent everything though. The built-in allows for indenting the selection. You could argue, that proper indentation is always something, that should be had. – Zelphir Kaltstahl Dec 05 '15 at 23:28
  • 2
    @Zelphir I usually have one keybinding for indent selection, and another keybinding for indent all. -- I don't use indent all as often, but it's nice to have it when you need it. – BrainSlugs83 Dec 22 '15 at 07:51
  • @BrainSlugs83 Maybe a little bit offtopic: I've noticed that there is a problem when trying to auto indent languages, which don't use braces to mark blocks of code and allow to define functions or classes within others. The editor can't know what is on which level in those cases. – Zelphir Kaltstahl Dec 22 '15 at 16:47
  • 2
    `if oldRanges != null && oldRanges[0].start.column == oldRanges[0].end.column && oldRanges[0].start.row == oldRanges[0].end.row` adding this condition to `editor.selectAll();` will still allow indenting only the selection. If there is no selection, it will indent the entire file. – Manish Singh May 25 '16 at 11:28
  • Worked like a charm! Tho i got frustrated over cmd+alt+l not reformating til i discovered the shortcut was set to ctrl and not cmd :) – Deko Aug 25 '18 at 09:51
  • 2
    If you want to have a single undo step for the whole command, add this after defining editor: `checkpoint = editor.createCheckpoint();` and this at the end: `editor.groupChangesSinceCheckpoint(checkpoint);` – Dan Oct 21 '18 at 09:53
  • For my case, I only needed this done for at most a few files (extracted from a third party lib only so I could read it clearly...), so select all with auto indent is enough. There was too much nesting. – wlwl2 Nov 18 '20 at 13:07
45

Package auto-indent exists to apply auto-indent to entire file with this shortcuts :

ctrl+shift+i

or

cmd+shift+i

Package url : https://atom.io/packages/auto-indent

sanjeevprasad
  • 804
  • 1
  • 6
  • 21
fhcoso
  • 585
  • 6
  • 12
26

I prefer using atom-beautify, CTRL+ALT+B (in linux, may be in windows also) handles better al kind of formats and it is also customizable per file format.

more details here: https://atom.io/packages/atom-beautify

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
ungalcrys
  • 5,242
  • 2
  • 39
  • 23
19

You can just quickly open up the command palette and do it there
Cmd + Shift + p and search for Editor: Auto Indent:

screenshot

Pablo Bianchi
  • 1,824
  • 1
  • 26
  • 30
codingninja
  • 1,360
  • 2
  • 13
  • 24
6

This works for me:

'atom-workspace atom-text-editor':
    'ctrl-alt-a': 'editor:auto-indent'

You have to select all with ctrl-a first.

dave doe
  • 71
  • 1
  • 2
5

This is the best help that I found:

https://atom.io/packages/atom-beautify

This package can be installed in Atom and then CTRL+ALT+B solve the problem.

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
Michel Fernandes
  • 1,187
  • 9
  • 8
3

On Linux

(tested in Ununtu KDE)

There is the option in the menu, under Edit > Lines > Auto Indent or press Cmd + Shift + p, search for Editor: Auto Indent by entering just "ai"

Note: In KDE ctrl-alt-l is already globally set for "lock screen" so better use ctrl-alt-i instead.

You can add a key mapping in Atom:

  • Cmd + Shift + p, search for "Settings View: Show Keybindings"
  • click on "your keymap file"
  • Add a section there like this one:

     'atom-text-editor':
        'ctrl-alt-i': 'editor:auto-indent'
    

If the indention is not working, it can be a reason, that the file-ending is not recognized by Atom. Add the support for your language then, for example for "Lua" install the package "language-lua".

If a File is not recognized for your language:

  • open the ~/.atom/config.cson file (by CTRL+SHIFT+p: type ``open config'')
  • add/edit a customFileTypes section under core for example like the following:

    core:
      customFileTypes:
        "source.lua": [
          "conf"
        ]
        "text.html.php": [
          "thtml"
        ]
    

(You find the languages scope names ("source.lua", "text.html.php"...) in the language package settings see here)

rubo77
  • 19,527
  • 31
  • 134
  • 226
2

If you have troubles with hotkeys, try to open Key Binding Resolver Window with Cmd + .. It will show you keys you're pressing in the realtime.

For example, Cmd + Shift + ' is actually Cmd + "

Dmytro
  • 5,443
  • 2
  • 52
  • 50
0

You could also try to add a key mapping witch auto select all the code in file and indent it:

'atom-text-editor':
  'ctrl-alt-l': 'auto-indent:apply'
Hristian Yordanov
  • 650
  • 1
  • 6
  • 25
0

I was working on some groovy code, which doesn't auto-format on save. What I did was right-click on the code pane, then chose ESLint Fix. That fixed my indents.

enter image description here

Jeff Lowery
  • 2,492
  • 2
  • 32
  • 40
0

If you are used to the Eclipse IDE or the Netbeans, you can use the package eclipse-keybindings (https://atom.io/packages/eclipse-keybindings):

This Atom package provides Eclipse IDE key mappings for Atom. Currently, the Eclipse shortcuts are directly mapped to existing Atom commands.

To format all lines from a file, just use: Ctrl+Shift+F.

Fabrício Pereira
  • 1,521
  • 1
  • 12
  • 20
0

Ctrl+Shift+i worked for me in PHP under Windows ... but some files did not react. Not being the brightest it took me a while to work out that it was the include files that were the problem. If you are using echo(' ... PHP ...') then the PHP does not get re-formatted. To get over this, create a temporary PHP file, say t.php, copy the PHP part into that, reindent it (Ctrl+Shift+i ... did I mention that?) and then copy the newly reformatted PHP back into the original file. Whilst this is a pain, it does give you correctly formatted PHP.

Dharman
  • 30,962
  • 25
  • 85
  • 135
andywicks
  • 13
  • 1
  • 3