[code-formatting] How to auto-indent code in the Atom editor?

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?

This question is related to code-formatting atom-editor auto-indent

The answer is


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


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)


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


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

screenshot


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


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.


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 + "


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


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'

This works for me:

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

You have to select all with ctrl-a first.


Examples related to code-formatting

How do you format code on save in VS Code How do you format code in Visual Studio Code (VSCode) JavaScript Chart.js - Custom data formatting to display on tooltip Is there a command for formatting HTML in the Atom editor? How to auto-indent code in the Atom editor? Code formatting shortcuts in Android Studio for Operation Systems How to auto-format code in Eclipse? How to format code in Xcode? What is the proper way to format a multi-line dict in Python? How do I format XML in Notepad++?

Examples related to atom-editor

You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How to open the terminal in Atom? "CSV file does not exist" for a filename with embedded quotes Commenting out code blocks in Atom How can I jump to class/method definition in Atom text editor? How to run a program in Atom Editor? Atom menu is missing. How do I re-enable Running Python from Atom Is there a command for formatting HTML in the Atom editor? How to auto-indent code in the Atom editor?

Examples related to auto-indent

Brackets.io: Is there a way to auto indent / format <html> How to auto-indent code in the Atom editor? Sublime Text 3, convert spaces to tabs How to fix/convert space indentation in Sublime Text? How To Auto-Format / Indent XML/HTML in Notepad++ Difference between Ctrl+Shift+F and Ctrl+I in Eclipse Turning off auto indent when pasting text into vim Tab key == 4 spaces and auto-indent after curly braces in Vim