Vim - 如何用字符串“\n”替换换行符

Mat*_*ewj 4 vim regular-expression

在vim中,我想用文字字符串替换换行符\n

例如,如果我打开一个包含以下文本的文件:

This is line one
This is line two
Run Code Online (Sandbox Code Playgroud)

我想替换换行符并具有以下内容:

This is line one\nThis is line two
Run Code Online (Sandbox Code Playgroud)

我怎样才能实现这个目标?

Lie*_*ers 5

你必须转义你的替换的替换部分\

:1,$-1s/\n/\\n
Run Code Online (Sandbox Code Playgroud)

分解

:            start an ex command
1,$-1        over a range from the first line till the last but one
s/           substitute
\n           all newlines
/            with
\\n          literal string \n
Run Code Online (Sandbox Code Playgroud)