說我有一個只是一個角色的「一條線,讓我們去看看\n
的作用下,不同的情況: n和不同情況下 r的不同的行爲在Vim中
: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()
:echo substitute(" ", " ", "a\nb", "")
a
b
:echo substitute(" ", " ", "a\\nb", "")
a
b
:echo substitute(" ", " ", "a\\\nb", "")
a
b
:echo substitute(" ", " ", "a\\\\nb", "")
a\nb
這一次的情況下,\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
的效果就像在:echo "a\nb"
\n
,和反斜槓的規則是相同的。
: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()
時不同數量的反斜槓效果? - Vim的
\n
和\r
有何區別?
補充1:
,如果我:let s = "a\\\nb"
然後<C-r>=s
看到它在緩衝區中,我看到
a\
b
如何解釋呢?
太棒了!我幾乎得到它。考慮一下':echo substitute(「」,「」,「a \\ rb」,「」)',爲什麼在一次解釋之後''a \\\ rb「'會被解釋爲'a \ rb'?這是由於'substitute()'的解釋嗎?一次解釋後,它不應該是'a^^ Mb'嗎? – fujianjin6471
@ fujianjin6471是的。但是'\^M'的含義大多是未定義的。從':h/\\'反斜槓後跟一個字符,沒有特別的含義,爲未來的擴展保留。目前'\^M'被解釋爲回車符。如果您將'^ M'放入緩衝區(''),然後搜索'/ \^M',您可以看到這一點。看到'^ M'匹配。 (不要依賴於這種行爲) –
FDinoff
謝謝,但如果我':讓s =「a \\\ nb」'然後' = s'在緩衝區中看到它,我看到一些真正無法解釋的東西。我在問題中顯示它。 –
fujianjin6471