\n和\r';在Vim中,在不同的环境下有不同的行为
假设我有一行只有一个字符“s”,让我们看看\n和\r';在Vim中,在不同的环境下有不同的行为,vim,Vim,假设我有一行只有一个字符“s”,让我们看看\n在不同情况下的效果: :s/s/a\nb a^@b :s/s/a\\nb a\nb :s/s/a\\\nb a\^@b :s/s/a\\\\nb a\\nb :echo "a\nb" a b :echo "a\\nb" a\nb :echo "a\\\nb" a\ b :echo "a\\\\nb" a\\nb 那么为什么\n的行为会有所不同呢?然后让我们使用substitute() 这次,\n仍然被解释为“换行符”,但是如何解释反斜杠呢 下一部
\n
在不同情况下的效果:
:s/s/a\nb
a^@b
:s/s/a\\nb
a\nb
:s/s/a\\\nb
a\^@b
:s/s/a\\\\nb
a\\nb
:echo "a\nb"
a
b
:echo "a\\nb"
a\nb
:echo "a\\\nb"
a\
b
:echo "a\\\\nb"
a\\nb
那么为什么\n
的行为会有所不同呢?然后让我们使用substitute()
这次,\n
仍然被解释为“换行符”,但是如何解释反斜杠呢
下一部分,假设我仍然有一行只有一个字符“s”,而不是\n
,\r
将被研究:
:s/s/a\rb
a
b
:s/s/a\\rb
a\rb
:s/s/a\\\rb
a\
b
:s/s/a\\\\rb
a\\rb
\r
的效果类似于中的\n
:echo“a\nb”
,反斜杠的规则是相同的
:echo "a\rb"
b
:echo "a\\rb"
a\rb
:echo "a\\\rb"
b\ "This is the most strange case!!!
:echo "a\\\\rb"
a\\rb
\r
在这种情况下做什么?第三种情况更为奇怪
:echo substitute(" ", " ", "a\rb", "")
b
:echo substitute(" ", " ", "a\\rb", "")
b
:echo substitute(" ", " ", "a\\\rb", "")
b
:echo substitute(" ", " ", "a\\\\rb", "")
a\rb
尽管如此,我还是无法理解不同数量的反斜杠是如何表现的
我的问题是:
\n
或\r
在:substitute
、echo
和substitute()下的行为不同
substitute()
时不同反斜杠数的效果\n
和\r
之间有什么区别:让s=“a\\\nb”
然后=s
在缓冲区中查看它,我会看到
a\
b
如何解释
:echo
和substitute()
,\n
是换行符,\r
是回车符。他们应该做你期望的事情\n
将光标移动到下一行(第1列),而\r
将光标移动到第1列的同一行。对于\r
,打印的下一个字符将覆盖以前打印的字符。(我主要关注substitute()
和:echo
,:substitute
如下):echo substitute(" ", " ", "a\\\rb", "")
b
:echo substitute(" ", " ", 'a\\\rb', "")
b\
:help expr quote
进行了更改。这意味着,如果字符串只解释一次(使用单引号),则echo和substitute可以看到相同的内容
“a\\\rb”
在一次解释后被解释为a\rb
,然后在第二次解释后被解释为ab
。这只会导致打印b
\r
和\n
根据使用位置的不同而变化。唯一的区别是:替换,其中\r
用作替换中的换行符
这是
:substitute
和substitute()
行为不同的情况之一
:取代基
如下
<CR> split line in two at this point
(Type the <CR> as CTRL-V <Enter>) s<CR>
\r idem s/\r
...
\n insert a <NL> (<NUL> in the file)
(does NOT break the line) s/\n
真棒的回答!我几乎明白了。考虑<代码>:回音代替品(“,”,“a \ rb”,“”“”/>代码,为什么在一个解释之后,<代码>“a \ rb”< /代码>被解释为<代码> a \ rb < /代码>?这种解释是由于
substitute()
引起的吗?经过一次解释后,它不应该是一个“^Mb”吗?@fujinjin6471是的。但是,^M
的含义大多没有定义。From:h/\`一个反斜杠后跟一个字符,没有特殊意义,保留用于将来的扩展。当前
^M`被解释为回车符。如果将^M
放入缓冲区(
),然后搜索/^M
,则可以看到这一点。然后查看^M
是否匹配。(不依赖于此行为)谢谢,但是如果我:让s=“a\\\nb”
然后=s
在缓冲区中查看它,我会看到一些真正无法解释的东西。我在问题中展示了它。@fujinjin6471我看到a\b
其中
是一个换行符(a\
在第一行,b
在第二行)。这正是人们所期望的。在:echo substitute(“,”,“a\\\rb”,“)
中看不到斜杠,因为substitute自己解释转义码(在翻译正常字符串转义码之后)。让我们不要这样做,表达式寄存器也不要这样做。@Fujinjin6471实际上,如果您启用了某种自动缩进功能,那么可能会发生这种情况,因为表达式寄存器添加的字符就像是手动键入的一样(我想)。看看您是否有文件类型的东西,它可能会附带一个indentexpr来实现这一点,或者您可能有其他一些设置。
<CR> split line in two at this point
(Type the <CR> as CTRL-V <Enter>) s<CR>
\r idem s/\r
...
\n insert a <NL> (<NUL> in the file)
(does NOT break the line) s/\n
The special meaning is also used inside the third argument {sub} of
the substitute() function with the following exceptions:
...
- <CR> and \r inserts a carriage-return (CTRL-M).
...