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

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

问题描述

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

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

:%s/,/
/g 

但它插入了一个看起来像 ^@ 的东西,而不是一个真正的换行符.该文件不是在 DOS 模式下或其他任何东西.

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

我该怎么办?

如果你和我一样好奇,请查看问题为什么Vim 的换行符? 也是.

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

推荐答案

使用 而不是 .

替换会在文本中插入一个空字符.要获得换行符,请使用 .但是,当搜索换行符时,您仍然会使用 .这种不对称是由于 做稍微不同的事情:

Use instead of .

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

匹配行尾(换行),而 匹配回车.另一方面,在替换中 插入一个空字符而 插入一个换行符(更准确地说,它被视为输入 CR).这是一个使用 Vim 命令行功能的小型非交互式示例来说明这一点(换句话说,您可以将以下内容复制并粘贴到终端中以运行它).xxd 显示结果文件的十六进制转储.

matches an end of line (newline), whereas matches a carriage return. On the other hand, in substitutions inserts a null character whereas 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/
/' '+s/a/
/' +wq
(echo 'After:'; xxd test) >> output.txt
more output.txt

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

换句话说, 已经将字节 0x00 插入到文本中; 已插入字节 0x0a.

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

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

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