\ n and \ r different behaviors under different conditions in Vim

Say I have a line with the only character, release the \n effect under different circumstances:

 :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 

So why does \n behave differently? Then consider the condition using 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 

This time, \n is still interpreted as a "new line", but how to explain the backslash?

In the next part, let's say I still have a line with only the character ', instead of \n , \r will be studied:

 :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 effect is similar to \n in :echo "a\nb" , and the backslash rule is the same.

 :echo "a\rb" b :echo "a\\rb" a\rb :echo "a\\\rb" b\ "This is the most strange case!!! :echo "a\\\\rb" a\\rb 

What does \r in this case? And the third subtitle is even stranger.

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

However, I cannot understand how different numbers of backslashes behave.

My question is:

  • Why do \n or \r behave differently in :substitute , echo directly and substitute() ?
  • How to explain the different number of backslash effects when using substitute() ?
  • What is the difference between \n and \r in Vim?

Addition 1:

if I :let s = "a\\\nb" and then <Cr>=s to see it in the buffer, I see

 a\ b 

How to explain this?

+8
vim
source share
1 answer
  • For :echo and substitute() , \n is the newline character, and \r is the carriage return. They should do what you expect. \n moves the cursor to the next row (column 1), and \r moves the cursor to column 1 with the same row. For \r next character that is printed will overwrite the previously printed character. (I mainly focus on substitute() and :echo :substitute below)

  • Why echo and substitute () behave differently. You need to understand how many line interpretations occur. For echo, only one happens for substitute (), two happen.

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

The second is what you would expect for an echo. Double quoted strings contain content that has been modified to match :help expr-quote . This means that both echo and substitute see the same thing if the string is interpreted only once (using single quotes)

"a\\\rb" interpreted as a\rb after one interpretation, and then a<CR>b after the second. This results in only b being printed.

  1. \r and \n change depending on where they are used. The only difference would be :substitute , where \r used as the newline in the replacement.

This is one of the cases where :substitute and substitute() behave differently,

:substitue follows

  <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> matches ^@ , which does not match the newline)

and substitute() follows

 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). ... 
+2
source share

All Articles