本文翻译自:Intellij reformat on file save
I remember seeing in either IntelliJ or Eclipse the setting to reformat (cleanup) files whenever they are saved. 我记得在IntelliJ或Eclipse中看到了保存时重新格式化(清理)文件的设置。 How do I find it (didn't find it in the settings) 我如何找到它(没有在设置中找到它)
#1楼
参考:https://stackoom.com/question/3yM5/Intellij重新格式化文件保存
#2楼
Ctrl + Alt + L is format file (includes the two below) Ctrl + Alt + L是格式文件(包括下面两个)
Ctrl + Alt + O is optimize imports Ctrl + Alt + O可以优化导入
Ctrl + Alt + I will fix indentation on a particular line Ctrl + Alt + 我将修复特定行的缩进
I usually run Ctrl + Alt + L a few times before committing my work. 我通常在执行工作之前运行Ctrl + Alt + L几次。 I'd rather it do the cleanup/reformatting at my command instead of automatically. 我宁愿它在我的命令而不是自动执行清理/重新格式化。
#3楼
I wound up rebinding the Reformat code...
action to Ctrl - S , replacing the default binding for Save All
. 我将重新格式Reformat code...
重新绑定Reformat code...
操作到Ctrl - S ,替换Save All
的默认绑定。
It may sound crazy at first, but IntelliJ seems to save on virtually every action: running tests, building the project, even when closing an editor tab. 起初听起来可能很疯狂,但IntelliJ似乎几乎可以节省每一个动作:运行测试,构建项目,甚至关闭编辑器选项卡。 I have a habit of hitting Ctrl - S pretty often, so this actually works quite well for me. 我有一个经常按Ctrl - S的习惯,所以这对我来说确实很有用。 It's certainly easier to type than the default bind for reformatting. 输入肯定比重新格式化的默认绑定更容易。
#4楼
Below is Neil's answer updated. 以下是Neil的答案更新。
IntelliJ 13 Steps: IntelliJ 13步骤:
- Code -> Reformat Code 代码 - >重新格式化代码
- Edit -> Macros -> Start Macro Recording 编辑 - >宏 - >开始宏录制
- Code -> Reformat Code 代码 - >重新格式化代码
- File -> Save all 文件 - >全部保存
- Edit -> Macros -> Stop Macro Recording 编辑 - >宏 - >停止宏录制
- Name the macro (something like "formatted save") 命名宏(类似“格式化保存”)
- File -> Settings -> Keymap 文件 - >设置 - >键盘映射
- Right click on the macro. 右键单击宏。 Add Keyboard Shortcut. 添加键盘快捷键。 Set the keyboard shortcut to Control + S. 将键盘快捷键设置为Control + S.
- IntelliJ will inform you of a hotkey conflict. IntelliJ将通知您热键冲突。 Select "remove" to remove other assignments. 选择“删除”以删除其他分配。
#5楼
I suggest the save actions plugin . 我建议保存动作插件 。 It also supports optimize imports and rearrange code. 它还支持优化导入和重新排列代码。
Works well in combination with the eclipse formatter plugin . 与eclipse格式化程序插件结合使用效果很好。
Search and activate the plugin: 搜索并激活插件:
Configure it: 配置它:
Edit: it seems like it the recent version of Intellij the save action plugin is triggered by the automatic Intellij save. 编辑:似乎最近版本的Intellij保存动作插件是由自动Intellij保存触发的。 This can be quite annoying when it hits while still editing. 当它仍然在编辑时命中时,这可能非常烦人。
This github issue of the plugin gives a hint to some possible solutions: 这个插件的github问题提供了一些可能的解决方案的提示:
https://github.com/dubreuia/intellij-plugin-save-actions/issues/63 https://github.com/dubreuia/intellij-plugin-save-actions/issues/63
I actually tried to assign reformat to Ctrl + S and it worked fine - saving is done automatically now. 我实际上尝试将重新格式化为Ctrl + S并且工作正常 - 现在自动完成保存。
#6楼
IntellIJ 14 && 15: When you are checking in code in Commit changes dialog, tick the Reformat code checkbox, then IntelliJ will reformatting all the code that you are checking in. IntellIJ 14 && 15:当您在“提交更改”对话框中签入代码时,勾选“重新格式代码”复选框,然后IntelliJ将重新格式化您正在检入的所有代码。
Source: www.udemy.com/intellij-idea-secrets-double-your-coding-speed-in-2-hours 资料来源: www.udemy.com/intellij-idea-secrets-double-your-coding-speed-in-2-hours