如何在Atom编辑器中自动缩进代码?

如何在Atom编辑器中自动缩进代码?在其他编辑器中,您通常可以选择一些代码并自动缩进。

还有快捷键吗?

424397 次浏览

我在菜单中找到了这个选项,在编辑>行>自动缩进下。它似乎没有默认的键映射绑定。

你可以尝试添加一个键映射(Atom > Open Your Keymap[在Windows: File > Settings > Keybindings > " Your Keymap File "),就像这样:

'atom-text-editor':
'cmd-alt-l': 'editor:auto-indent'

这对我很有用:)


Windows:

'atom-text-editor':
'ctrl-alt-l': 'editor:auto-indent'

包自动缩进的存在是为了通过这个快捷键整个文件应用自动缩进:

ctrl + 转变 +

cmd + 转变 +

包url: https://atom.io/packages/auto-indent

如果热键有问题,尝试用Cmd +。打开Key Binding Resolver Window。它会实时显示你正在按的键。

例如,Cmd + Shift + '实际上是Cmd + "

你可以快速打开命令面板并在那里执行 Cmd + Shift + p,搜索Editor: Auto Indent:

截图

公认的答案是可行的,但你必须先做一个“全选”——每次都是——我太懒了。

事实证明,这并不是非常微不足道的——我想我在这里发布这篇文章是为了节省志同道合的人花30分钟来追踪所有这些内容。-另外注意:这种方法在完成时恢复原始的选择(它发生得如此之快,你甚至没有注意到选择曾经被改变)。

1)。首先,添加一个自定义命令到你的init脚本(File->打开你的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)。绑定"custom:reformat"到一个键(File->打开你的Keymap,然后粘贴在底部):

'atom-text-editor':
'ctrl-alt-d': 'custom:reformat'

3)。重新启动Atom(初始化。Coffee脚本仅在atom第一次启动时运行)。

我更喜欢使用原子美化,CTRL+ALT+B(在linux中,也可能在windows中)处理更好的各种格式,它也是可定制的每个文件格式。

更多细节:https://atom.io/packages/atom-beautify

这对我来说很管用:

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

你必须先用ctrl - a选择所有选项。

这是我找到的最好的帮助:

< a href = " https://atom。Io /packages/ atomic -beautify" rel="nofollow noreferrer">https://atom.io/packages/atom-beautify

这个包可以安装在Atom中,然后CTRL+ALT+B解决问题。

你也可以尝试添加一个键映射来自动选择文件中的所有代码并缩进它:

'atom-text-editor':
'ctrl-alt-l': 'auto-indent:apply'

我在写一些绝妙的代码,保存时不会自动格式化。我所做的是右键单击代码窗格,然后选择ESLint Fix。这就固定了我的凹痕。

enter image description here

在Linux上

(在Ununtu KDE中测试)

菜单中有选项,在编辑>行>自动缩进下或按Cmd + Shift + p,通过输入“ai”搜索Editor: Auto Indent

注意:在KDE中,ctrl-alt-l已经全局设置为“锁屏”,所以最好使用ctrl-alt-i

你可以在Atom中添加一个键映射:

  • Cmd + Shift + p,搜索“Settings View: Show Keybindings”
  • 点击“你的keymap文件”
  • 在这里添加一个像这样的部分:

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

如果你使用Eclipse IDE或Netbeans,你可以使用包eclipse-keybindings (https://atom.io/packages/eclipse-keybindings):

这个Atom包为Atom提供了Eclipse IDE键映射。目前,Eclipse快捷方式直接映射到现有的Atom命令。

要格式化文件中的所有行,只需使用:Ctrl+Shift+F

Ctrl+Shift+我在Windows下用PHP为我工作…但一些文件没有反应。不是最亮的,我花了一段时间才弄清楚这是包含文件的问题。如果你使用的是回声('……PHP……”),那么PHP不会被重新格式化。为了解决这个问题,创建一个临时的PHP文件,比如t.p p,将PHP部分复制到其中,重新缩进(Ctrl+Shift+i…),然后将重新格式化的PHP复制回原始文件中。虽然这很麻烦,但它确实为您提供了正确格式化的PHP。