Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is \r a newline for Vim?

Tags:

vim

From question How to replace a character for a newline in Vim?. You have to use \r when replacing text for a newline, like this

:%s/%/\r/g 

But when replacing end of lines and newlines for a character, you can do it like:

:%s/\n/%/g  

What section of the manual documents these behaviors, and what's the reasoning behind them?

like image 815
Vinko Vrsalovic Avatar asked Sep 16 '08 11:09

Vinko Vrsalovic


People also ask

Why does vim add a newline?

A sequence of zero or more non- <newline> characters plus a terminating <newline> character. And, therefore, they all need to end with a newline character. That's why Vim always adds a newline by default (because, according to POSIX, it should always be there). It is not the only editor doing that.

Does vim add newline at end of file?

Vim doesn't show latest newline in the buffer but actually vim always place EOL at the end of the file when you write it, because it standard for text files in Unix systems. You can find more information about this here. In short you don't have to worry about the absence a new lines at the end of the file in vim.

What is carriage return in vim?

Ctrl - V tells vi that the next character typed should be inserted literally and ctrl - m is the keystroke for a carriage return.

How do I add a new line in vim?

Starting in normal mode, you can press O to insert a blank line before the current line, or o to insert one after. O and o ("open") also switch to insert mode so you can start typing. To add 10 empty lines below the cursor in normal mode, type 10o<Esc> or to add them above the cursor type 10O<Esc> .


2 Answers

From http://vim.wikia.com/wiki/Search_and_replace :

When Searching

...

\n is newline, \r is CR (carriage return = Ctrl-M = ^M)

When Replacing

...

\r is newline, \n is a null byte (0x00).

like image 179
lmat - Reinstate Monica Avatar answered Oct 21 '22 14:10

lmat - Reinstate Monica


From vim docs on patterns:

\r matches <CR>

\n matches an end-of-line - When matching in a string instead of buffer text a literal newline character is matched.

like image 35
pjz Avatar answered Oct 21 '22 13:10

pjz