IntelliJ Idea设置默认换行符

Posted 青春朝阳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IntelliJ Idea设置默认换行符相关的知识,希望对你有一定的参考价值。

IntelliJ换行CRLF, LF, CR的解释和默认设置

在window下开发有一个大坑,就是换行默认是CRLF,也就是回车换行,但是Linux下只有换行LF,这样代码提交后,会出现编译问题,所以最好的办法是在IntelliJ下设置默认为LF。首先我们先介绍CRLF,LF和CR这三种东西,CR是MAC老版本的做法,就是回车,但是后来的MAC系统统一换成LF了,LF是Linux下的做法,就是换行,这个做法比较自然,为什么要回车换行呢,是吧。微软采用的是CRLF,看上去好像是兼容了CR和LF,但是实际完全不是那么回事,就是回车并换行,好鸡肋啊,微软一直保持这种做法,开发人员大多在Linux下,所以对于开发人员来说还是比较坑的。下面介绍设置详解:

###第一步:File->Other Settings->Default Settings

 

###第二步:Editor->Code Style

可以看到,默认是System-Dependent,这个其实还是很牛叉的,根据系统自动配置,但是你是windows系统,默认是CRLF,服务器是Linux,你就得自己换了。

 

我们设置成下面这样,保存就好了

 

创建文件时,就能看到默认是LF了

参考网址是IntelliJ的使用,不熟悉的话,可以看看,这个文档还是写的不错的,就是全是英文,对于英文不好的人来说,略坑。 
参考网址: [https://www.jetbrains.com/idea/help/configuring-line-separators.html]


Configuring Line Separators

IntelliJ IDEA makes it possible to set up line separators (line endings) for the newly created files, and change line separator style for the existing files.

On this page:

To set up line separators for new files

  1. In Settings, click Code Style.
  2. From the Line separator (for new files) drop-down list, select the desired line separator style: 
    lineSeparatorForNewFiles
  3. Apply changes and close the dialog.

To view line ending style for the current file

  1. Open the desired file in the editor, as described in the section Opening and Reopening Files.
  2. View the Status bar: the current line endings style is denoted by the dedicated icon with the specified style, for example, uiStatusLineEnding.

To change line separator for a file, currently opened in the editor

  1. Open the desired file in the editor, as described in the section Opening and Reopening Files.
  2. Do one of the following:
    • Click the line separator spin box in the Status bar, and choose the desired line ending style from the pop-up menu:
      lineSeparatorPopup
    • Choose File | Line Separators on the main menu, and then choose the desired line ending style from the sub-menu.

To change line separator for a file or directory, selected in the Project view

Select a file or directory in the Project tool window.

Note that if a directory is selected, the line ending style applies to all nested files recursively.

Choose File | Line Separatorson the main menu, and then select the desired line ending style from the sub-menu.

lineSeparatorMenu

Tips and tricks

 

以上是关于IntelliJ Idea设置默认换行符的主要内容,如果未能解决你的问题,请参考以下文章

IntelliJ IDEA代码自动提示失效

IntelliJ IDEA设置代码括号对齐方式

IntelliJ IDEA 中的常用设置

IntellIJ Idea如何使用快捷键快速换行。

IntelliJ IDEA 常用设置

IntelliJ IDEA 15 设置默认浏览器