如何用Vim中的换行符替换一个字符? [英] How to replace a character by a newline in Vim?

查看:230
本文介绍了如何用Vim中的换行符替换一个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用新行替换当前文件中的每个

I'm trying to replace each , in the current file by a new line:

:%s/,/\n/g 

但是它插入看起来像一个 ^ @ 而不是实际的换行符。该文件不在DOS模式或任何东西。

But it inserts what looks like a ^@ instead of an actual newline. The file is not in DOS mode or anything.

我该怎么办?

编辑: 如果您好奇,像我一样,检查问题 为什么

If you are curious, like me, check the question Why is \r a newline for Vim? as well.

推荐答案

使用 \r 而不是 \\\



替换为 \\\
在文本中插入一个空字符。要获取换行符,请使用 \r 。当搜索
换行符时,您仍然可以使用 \\\
。这种不对称是由于 \\\
\r
做稍微不同的事情

Use \r instead of \n.

Substituting by \n inserts a null character into the text. To get a newline, use \r. When searching for a newline, you’d still use \n, however. This asymmetry is due to the fact that \n and \r do slightly different things:

\\\
匹配行尾(换行符),而 \r 匹配回车符。另一方面,在替换 \\\
中插入一个空字符,而 \r 插入一个换行符,它被视为输入< CR> )。这是一个很小的非互动的例子来说明这一点,使用Vim命令行功能(换句话说,您可以将以下内容复制并粘贴到终端中以运行它)。 xxd 显示生成的文件的十六进制。

\n matches an end of line (newline), whereas \r matches a carriage return. On the other hand, in substitutions \n inserts a null character whereas \r inserts a newline (more precisely, it’s treated as the input <CR>). Here’s a small, non-interactive example to illustrate this, using the Vim command line feature (in other words, you can copy and paste the following into a terminal to run it). xxd shows a hexdump of the resulting file.

echo bar > test
(echo 'Before:'; xxd test) > output.txt
vim test '+s/b/\n/' '+s/a/\r/' +wq
(echo 'After:'; xxd test) >> output.txt
more output.txt





Before:
0000000: 6261 720a                                bar.
After:
0000000: 000a 720a                                ..r.

换句话说, \\\
将字节0x00插入文本; \r 已插入字节0x0a。

In other words, \n has inserted the byte 0x00 into the text; \r has inserted the byte 0x0a.

这篇关于如何用Vim中的换行符替换一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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