Regular-Expression

Vim - 如何用字元串“ n”替換換行符

  • March 11, 2020

在 vim 中,我想用文字 string 替換換行符\n

例如,如果我打開一個包含此文本的文件:

This is line one
This is line two

我想替換換行符並具有以下內容:

This is line one\nThis is line two

我怎樣才能做到這一點?

你必須逃避你的替代品的替換部分\

:1,$-1s/\n/\\n

分解

:            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

看看這個:

:1,$-s/\n/\\n

這不會在文件末尾替換,因此:

This is line one\nThis is line two\nThis is line three

引用自:https://unix.stackexchange.com/questions/572207