2015-12-24 28 views
8

說我有一個只是一個角色的「一條線,讓我們去看看\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 

不過,我不明白反斜槓的行爲方式有多不同。

我的問題是:

  1. 爲什麼\n\r行爲不同下:substituteecho直接和substitute()
  2. 如何解釋使用substitute()時不同數量的反斜槓效果?
  3. Vim的\n\r有何區別?

補充1:

,如果我:let s = "a\\\nb"然後<C-r>=s看到它在緩衝區中,我看到

a\ 
     b 

如何解釋呢?

回答

2
  1. 對於這兩種:echosubstitute()\n是換行和\r是一個回車。他們應該做你期望的。 \n將光標移動到下一行(第1列),並且\r將光標移動到第1列的同一行。對於\r,打印的下一個字符將覆蓋先前打印的字符。 (我主要集中在substitute():echo,:substitute以下)

  2. 爲什麼echo和substitute()的行爲有所不同。你需要了解字符串有多少種解釋。對於回聲,只有一個發生替代()發生。

    :echo substitute(" ", " ", "a\\\rb", "") 
    b 
    :echo substitute(" ", " ", 'a\\\rb', "") 
    b\ 
    

第二是一樣的,你有什麼預期會爲回聲。 雙引號字符串中的內容根據:help expr-quote更改。這意味着,回聲和替代看看如果字符串只解釋一次

"a\\\rb"被第二後解釋爲a\rb一個解釋後,然後a<CR>b同樣的事情(這是使用單引號)。這導致只打印b

  • \r\n變化取決於使用它們的地方。唯一的區別是:substitute其中\r被用作替換中的換行符。

  • 這是其中:substitutesubstitute()行爲不同,

    :substitue如下

    <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 
    

    <NUL>相同^@情況是不一樣的一個新行一個)

    substitute()如下

    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). 
    ... 
    
    +0

    太棒了!我幾乎得到它。考慮一下':echo substitute(「」,「」,「a \\ rb」,「」)',爲什麼在一次解釋之後''a \\\ rb「'會被解釋爲'a \ rb'?這是由於'substitute()'的解釋嗎?一次解釋後,它不應該是'a^^ Mb'嗎? – fujianjin6471

    +0

    @ fujianjin6471是的。但是'\^M'的含義大多是未定義的。從':h/\\'反斜槓後跟一個字符,沒有特別的含義,爲未來的擴展保留。目前'\^M'被解釋爲回車符。如果您將'^ M'放入緩衝區(''),然後搜索'/ \^M',您可以看到這一點。看到'^ M'匹配。 (不要依賴於這種行爲) – FDinoff

    +0

    謝謝,但如果我':讓s =「a \\\ nb」'然後' = s'在緩衝區中看到它,我看到一些真正無法解釋的東西。我在問題中顯示它。 – fujianjin6471

    相關問題