【问题标题】:How can I tell if my editor is adding or removing newlines at the end of a file from the diff?如何判断我的编辑器是否在差异文件末尾添加或删除换行符?
【发布时间】:2016-12-20 02:29:32
【问题描述】:

如果我有以下差异:

diff --git a/file.txt b/file.txt
index abcdef..ghijkl mnopqr
--- a/file.txt
+++ b/file.txt
@@ -2,4 +2,4 @@

-This is a line of code.
\ No newline at end of file
+This is a line of code

我了解I want the newline at the end of the file。但是,从这个差异来看,我的编辑器是在文件末尾添加还是删除换行符?

我不知道 \No newline at end of file 是指我的文本编辑器删除的内容(上图)还是添加的内容(下图)。

【问题讨论】:

    标签: git editor newline diff text-editor


    【解决方案1】:

    您的问题中引用的消息表明换行符已添加到正在比较的文件的较新版本中。

    如果在被比较的文件的较新版本中删除了换行符,您会看到:

    diff --git a/file.txt b/file.txt
    index abcdef..ghijkl mnopqr
    --- a/file.txt
    +++ b/file.txt
    @@ -2,4 +2,4 @@
    -This is a line of code.
    +This is a line of code.
    \ No newline at end of file
    

    也就是说,\ No newline at end of file 部分将是差异的最后一行。


    如果你想自己测试,这里有两个文件你可以运行diff -u

    • noeol.txt(文件末尾没有换行符)
    • eol.txt(文件末尾有换行符)

    diff -u noeol.txt eol.txt 会给你这个:

    --- noeol.txt   2016-08-13 12:55:16.000000000 +0900
    +++ eol.txt 2016-08-13 12:55:23.000000000 +0900
    @@ -1 +1 @@
    -This is a line of code.
    \ No newline at end of file
    +This is a line of code.
    

    diff -u eol.txt noeol.txt 会给你这个:

    --- eol.txt 2016-08-13 12:55:23.000000000 +0900
    +++ noeol.txt   2016-08-13 12:55:16.000000000 +0900
    @@ -1 +1 @@
    -This is a line of code.
    +This is a line of code.
    \ No newline at end of file
    

    【讨论】:

      猜你喜欢
      • 2013-04-28
      • 1970-01-01
      • 2012-01-27
      • 2012-04-12
      • 2014-05-28
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多