Unable to replace space with newline in Vim

I know the flow.

I ran

:%s/ /s/\n/g 

I get

 E488: Trailing characters 

Second example

I ran

 :%s/ /\n/g 

I get

 text^@text 

I run the same codes also with the following separetaly settings

 set fileformat=unix 

and

 set fileformat=dos 

How can you replace a new line in Vim?

+48
vim
Jun 26 '09 at 11:34
source share
5 answers

:%s/ / Ctrl v Return /g

Where Ctrl v is the control key plus the v key and Return is the return key (the one on the main keyboard, not the enter key on the numeric keypad). Other characters are printed as usual.

If entered correctly, the sequence Ctrl v Return will be displayed as ^M characters, usually of a different color, to indicate that they are special. Note that actually ^M input will not work.

Also note that in Vim for Windows it is Control-q instead of Control-v (how to insert this).




Ctrl-v also allows you to enter other "special" keyboard keys. It is also useful for e.g. Tab or Backspace.

+85
Jun 26 '09 at 11:36
source share

Try

 %s/ /\r/g 
+39
Jun 26 '09 at 11:37
source share

Enter the following:

 :s/ / 

and now type Ctrl-V or Ctrl-Q (depending on your configuration) and press Enter. Now you should:

 :s/ /^M 

Complete it:

 :s/ /^M/g 

and you are good to go.

+7
Jun 26 '09 at 11:38
source share

Try either

For Unix:

 :1,$s/\ /\n/g 

For Windows:

 :1,$s/\ /\r/g 

It contains an escape character for a space.

+2
Jun 26 '09 at 12:04
source share

In particular, to answer your problem with trailing characters, this is the regular expression you specified:

 :%s/ /s/\n/g 

You Have Too Much. It happens that you replace '' with s , and then you mark this after replacing: \n/g

I think you meant this:

 :%s/ \s/\n/g 

Note that your /s been changed to \s . Now the substitution will replace one space, followed by a space of any kind (space or tab) with \n . I doubt this will solve the problem or replace the space with a new line, but it should explain the error message.

+2
Jun 26 '09 at 14:11
source share



All Articles