How to auto-indent code in the Atom editor?

后端 未结 11 1484
野的像风
野的像风 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:35

    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.

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

    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.

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

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

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

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

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

    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

    0 讨论(0)
提交回复
热议问题