windows Vim:\n 与 \r

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/350661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:43:19  来源:igfitidea点击:

Vim: \n vs. \r

windowsunixvim

提问by Whaledawg

I haven't used vim in a Unix system in a while, but as I recall there was no \r, it was always \n.

我有一段时间没有在 Unix 系统中使用 vim,但我记得没有 \r,它总是 \n。

I'm using gVim under windows and when I searchfor new line characters I use \n. Searching for \r returns nothing. But when I replacethe characters I have to use \r's. \n's give me ^@

我在 Windows 下使用 gVim,当我搜索换行符时,我使用 \n。搜索 \r 不会返回任何内容。但是当我替换字符时,我必须使用\r。\n 给我 ^@

Can anyone explain what's going on here?

谁能解释一下这里发生了什么?

采纳答案by Jonathan Leffler

Lookup up:

查找:

:set fileformat=unix
:set fileformat=dos

This can be used on either platform to switch to the other encoding.

这可以在任一平台上使用以切换到其他编码。

回答by Brian Carper

Looks like you're asking two things. One issue is \rvs. \nwhich others have covered.

看起来你在问两件事。一个问题是\r\n其他人已经涵盖的问题。

The other issue is \non the right side of a substitution. If you look at :h s/\n, it says that \nin the replacement part of a substitution inserts a <NUL>/ <NL>, NOT a newline.

另一个问题是\n换人的右侧。如果您查看:h s/\n,它会说\n在替换的替换部分插入<NUL>/ <NL>,而不是换行符。

If you do a :%s/\n/\n/and save and open the file in a hex editor, all the ^@characters are ASCII 0's (NUL characters). Why the Vim devs use \non the left for end-of-line and \non the right for NUL is beyond me. But this particular behavior has nothing to do with Windows vs. Unix.

如果您执行 a:%s/\n/\n/并在十六进制编辑器中保存并打开文件,则所有^@字符都是 ASCII 0(NUL 字符)。为什么 Vim 开发人员\n在左侧使用行尾而\n在右侧使用 NUL,这超出了我的理解。但是这种特殊的行为与 Windows 与 Unix 无关。

回答by graywh

Behind the scenes, Vim uses \r (carriage returns) to save End-Of-Lines (regardless of the fileformat, which only matters when the file is being read or written). Vim uses \n to represent NULs. However, you search for EOL as \n, but in the replacement, \n stands for NUL. It's explained in :h sub-replace-sepcial. Searching for \r will find carriage returns that weren't part of the fileformat's EOL. There is a long explanation at :h file-formats.

在幕后,Vim 使用 \r(回车)来保存行尾(无论文件格式如何,这仅在文件被读取或写入时才重要)。Vim 使用 \n 来表示 NUL。但是,您将 EOL 搜索为 \n,但在替换中,\n 代表 NUL。它在 :h sub-replace-sepcial 中有解释。搜索 \r 将找到不属于文件格式 EOL 的回车符。在 :h 文件格式中有很长的解释。

回答by Paul Tomblin

vim does some messing around with carriage returns (\r) and newlines (\n). For instance, if you're in Unix and vi shows you lines ending in '^M' because they're Windows text files, an easy way to get rid of them is to enter the command

vim 对回车 (\r) 和换行符 (\n) 做了一些处理。例如,如果您在 Unix 中并且 vi 向您显示以“^M”结尾的行,因为它们是 Windows 文本文件,摆脱它们的一种简单方法是输入命令

:%s/^V^M/^V^M/g

It doesn't look like it should do anything, but it does.

它看起来不应该做任何事情,但确实如此。

回答by Paul Tomblin

:%s/^V^M/^V^M/g
:%s/^V^M/^V^M/g

Similarly, the following does the same thing (I think), and is easier to type out.

类似地,以下内容也做同样的事情(我认为),并且更容易输入。

:%s/\r/\r/g

回答by mat

Under unix, inside vim, ^V+ <enter>gives me a ^Mwhich is the \rcharacter.

在Unix下,Vim里面,^V+<enter>给了我^M这是\r性格。

Also, you can't find \rinside vim unless you tell it to edit the file in binary mode, that is, not using the default automatic mode where it autodetects the line ending. (it should print [dos]in the status.)

此外,\r除非您告诉它以二进制模式编辑文件,否则您无法在 vim 中找到,也就是说,不使用自动检测行尾的默认自动模式。(它应该[dos]在状态中打印。)

回答by Sean

I think the problem might lie in the way that Windows and Unix do newlines. The format for Unix is \n (line feed) but for Windows it is \r\n (carriage return, line feed).

我认为问题可能在于 Windows 和 Unix 换行的方式。Unix 的格式是 \n(换行),但 Windows 的格式是 \r\n(回车,换行)。

回答by Joel Coehoorn

In Windows, if you open up a file in text mode, \n is interpreted as both the newline and linefeed characters. This normally isn't the case the *nix systems.

在 Windows 中,如果您以文本模式打开文件,\n 将被解释为换行符和换行符。这通常不是 *nix 系统的情况。