How to auto-indent code in the Atom editor?

后端 未结 11 1483
野的像风
野的像风 2020-12-07 07:04

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?

相关标签:
11条回答
  • 2020-12-07 07:20

    This works for me:

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

    You have to select all with ctrl-a first.

    0 讨论(0)
  • 2020-12-07 07:25

    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)

    0 讨论(0)
  • 2020-12-07 07:26

    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

    0 讨论(0)
  • 2020-12-07 07:27

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

    0 讨论(0)
  • 2020-12-07 07:28

    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'
    
    0 讨论(0)
  • 2020-12-07 07:29

    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'
    
    0 讨论(0)
提交回复
热议问题