How to auto-indent code in the Atom editor?

后端 未结 11 1506
野的像风
野的像风 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: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).

提交回复
热议问题