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?
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.
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.
Ctrl - V tells vi that the next character typed should be inserted literally and ctrl - m is the keystroke for a carriage return.
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> .
From http://vim.wikia.com/wiki/Search_and_replace :
When Searching
...
\n
is newline,\r
isCR
(carriage return =Ctrl-M
=^M
)When Replacing
...
\r
is newline,\n
is a null byte (0x00
).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With