Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to replace a space with a new line in Vim

Tags:

vim

I know the thread.

I run

:%s/ /s/\n/g 

I get

E488: Trailing characters 

2nd example

I run

:%s/ /\n/g 

I get

text^@text 

I run the same codes also with the following settings separetaly

set fileformat=unix 

and

set fileformat=dos 

How can you replace with a new line in Vim?

like image 562
Léo Léopold Hertz 준영 Avatar asked Jun 26 '09 11:06

Léo Léopold Hertz 준영


People also ask

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

:%s/ /Ctrl vReturn/g

Where Ctrl v is Control-key plus key v and Return is the return key (the one on the main keyboard, not the enter key on the numpad). The other characters are typed as usual.

If this is entered correctly, the sequence Ctrl vReturn will display as the characters ^M, typically in a different color, to indicate that they are special. Note that actually typing ^M will not work.

Also note that in Vim for windows, it's Control-q instead of Control-v (as that is paste).


Ctrl-v also allows entering other "special" keys via the keyboard. It is also useful for e.g. Tab or Backspace.

like image 83
sleske Avatar answered Sep 20 '22 14:09

sleske


Try

%s/ /\r/g 
like image 39
rangalo Avatar answered Sep 21 '22 14:09

rangalo