为什么gedit和vim会隐藏用户的最后一个换行符? [英] Why do gedit and vim hide the final newline from the user?

查看:323
本文介绍了为什么gedit和vim会隐藏用户的最后一个换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们假设我们有两个文本文档:

Let's assume we have two text documents:


  1. 我们的第一个文件包含 hi 作为文本。

  2. 我们的第二个文件包含 hi 作为文本。

  1. Our first file contains "hi" as text.
  2. Our second file contains "hi" as text.


    当我们在gedit,vi或vim中打开这两个文件时,这两个文件在各个方面都是视觉上相同的。

When we open these two files in gedit, vi, or vim, the two files are visually identical in every way.

但是,当我们在文件上运行 xxd 时,我们得到以下内容:

However, when we run xxd on the files, we get the following:


    <我们第一个文件的十六进制内容为: 6869
  1. 我们第二个文件的十六进制内容为: 6869 0a

  1. Hex content of our first file reads: 6869
  2. Hex content of our second file reads: 6869 0a

呵呵!有一个看不见的换行符。在vim中,如果我们足够关注状态栏,碰巧了解 [noeol] 意味着什么,那么我们可能会接受这个,但是在gedit中,两个文件打开完全一样!

Aha! There's an invisible newline. In vim, if we were paying close enough attention to the status bar and happen to understand what [noeol] means, then we might pick up on this, but in gedit, the two files open exactly the same!

在一个小的调查中,当我问人们使用gedit或vim来区分这两个文件时,他们失败了100%的时间。当我要求他们使用leafpad或emacs完成相同的任务时,他们在100%的时间内成功了。

In a small survey, when I asked people to distinguish between the two files using only gedit or vim, they failed 100% of the time. When I asked them to accomplish the same task using leafpad or emacs, they succeeded 100% of the time.

我明白vi和gedit想为每个他们创建的文件(我承认这可能有优势)。我不明白的是为什么gedit和vim觉得用户隐藏这个换行符是有益的吗?特别是当这种行为有潜在的破坏性...

I understand that vi and gedit want to add a newline to every file they create (and I acknowledge that there are probably advantages to this). What I don't understand is why gedit and vim think visually hiding this newline from their users is beneficial? Especially when this kind of behavior is potentially extremely destructive...


(例如,程序员看到这两个文件的内容在他们的vi / gedit文本编辑器中是一样的,然后假设他们看到的是他们得到的,继续将内容写入数组 char greeting [2]第一个编写第一个文件的程序员虽然用他的代码有点乱七八糟,但是第二个编写第二个文件的程序员死于悲惨的贫穷,困惑和困惑隐藏(和可预防)堆栈溢出。)

所以请告诉你,有什么好处的文字像vim和gedit这样的编辑器在他们创建的每个文档的末尾添加不可见的换行符,然后继续隐藏用户的这些换行符,以便这些文件的真实内容只能使用其他文本编辑器才可见?

So pray tell, what are the advantages of having text editors like vim and gedit add invisible newlines at the end of each document they create, and then proceed to hide those newlines from the user, so that the true contents of these files are only visibly detectable using other text editors?

推荐答案

您认为最后一行为视觉隐藏源于相反的观点。

That you perceive the last line as "visually hidden" stems from the opposite viewpoints.

特别是使用Unix背景)认为文本文件应该始终以换行符结尾(例如,请参阅为什么文本文件以换行符结尾),而允许创建没有它的文件的任何文本编辑器都被彻底打破。从这个角度来看,Vim中没有额外的空行只能与这个世界观相一致。该文件出现在编辑器中,就像在 cat 进入终端时一样。

Many people (especially with a Unix background) argue that text files should always end with a newline (e.g. see Why should text files end with a newline?), and that any text editor that allows to create files without it is fundamentally broken. From that perspective, that there is no additional empty line in Vim is only consistent with this world view. The file appears in the editor as it would when it is cated into the terminal.

从Vim的点查看,一个缺少换行符的文件是邪教和缺陷;这就是为什么它被加载的 [noeol] 消息指示(并且很难编辑文件,同时保留缺少的换行符;虽然我已经写了 PreserveNoEOL插件以帮助)。

From Vim's point of view, a file with a missing newline is heretical and defective; that's why it is indicated with the [noeol] message on load (and it is quite hard to edit the file while keeping the missing newline; though I've written the PreserveNoEOL plugin to aid with that).

这篇关于为什么gedit和vim会隐藏用户的最后一个换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆