首页 > 代码库 > IntelliJ Idea设置默认换行符

IntelliJ Idea设置默认换行符

在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:

  • Setting up line separators for newly created files
  • Viewing line ending style for the current file
  • Changing line separator for a file, currently opened in the editor
  • Changing line separator for a selection in the Project view
  • Tips and tricks

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: 
    技术分享
  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, 技术分享.

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:
      技术分享
    • 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.

技术分享

Tips and tricks

    • Use multiple selection in the Project view.
    • Changing line separator is reflected in the Local history of a file.
    • Run the inspection ‘Inconsistent line separators‘ to find out, which files use line separator different from project‘s default.
 

IntelliJ Idea设置默认换行符