本文翻译自:How to auto-indent code in the Atom editor?
How do you auto-indent your code in the Atom editor? 如何在Atom编辑器中自动缩进代码? In other editors you can usually select some code and auto-indent it. 在其他编辑器中,您通常可以选择一些代码并自动缩进。
Is there a keyboard shortcut as well? 还有键盘快捷键吗?
#1楼
参考:https://stackoom.com/question/1WsFd/如何在Atom编辑器中自动缩进代码
#2楼
I found the option in the menu, under Edit > Lines > Auto Indent. 我在菜单中的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>打开您的键盘映射[在Windows上:文件>设置>键绑定>“您的键映射文件”]),如下所示:
'atom-text-editor':
'cmd-alt-l': 'editor:auto-indent'
It worked for me :) 它对我有用:)
For Windows: 对于Windows:
'atom-text-editor':
'ctrl-alt-l': 'editor:auto-indent'
#3楼
Package auto-indent exists to apply auto-indent to entire file with this shortcuts : 存在包自动缩进以使用此快捷方式将自动缩进应用于整个文件 :
ctrl + shift + i ctrl + shift + i
or 要么
cmd + shift + i cmd + shift + i
Package url : https://atom.io/packages/auto-indent 包网址: https : //atom.io/packages/auto-indent
#4楼
If you have troubles with hotkeys, try to open Key Binding Resolver Window
with Cmd + . 如果您遇到热键问题,请尝试使用Cmd +打开Key Binding Resolver Window
。 . 。 It will show you keys you're pressing in the realtime. 它会显示您实时按下的按键。
For example, Cmd + Shift + ' is actually Cmd + " 例如, Cmd + Shift +'实际上是Cmd +“
#5楼
You can just quickly open up the command palette and do it there 您可以快速打开命令面板并在那里执行
Cmd + Shift + p and search for Editor: Auto Indent
: Cmd + Shift + p并搜索Editor: Auto Indent
:
#6楼
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. 事实证明,这并非超级微不足道 - 我想我会在这里发布这个以试图拯救志同道合的人30分钟来追踪这一切。 -- 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): 1.)首先,在init脚本中添加一个自定义命令(文件 - >打开你的初始化脚本,然后将其粘贴在底部):
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): 2.)将“custom:reformat”绑定到一个键(File-> Open Your Keymap,然后将其粘贴到底部):
'atom-text-editor':
'ctrl-alt-d': 'custom:reformat'
3.) Restart Atom (the init.coffee script only runs when atom is first launched). 3.)重新启动Atom(init.coffee脚本仅在首次启动atom时运行)。