tags:

views:

513

answers:

5

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?

+8  A: 
:%s/ /<CTRL-V><Enter>/g

Where <CTRL-V> is Control-key plus key v, and <Enter> is Enter key.

In VIM for windows, it's Control-q instead of Control-v (as that is paste).

Ctrl-v allows entering "special" keys as characters. Also useful for e.g. Tab or Backspace.

sleske
I accept the answer because it is most system independent. samoz answer also works. It is surprising that the command for Windows works in my OS/X, not the Unix command.
Masi
:%s/ /\r/g should also work on all operating systems.
Al
AL is right, I checked it on my ubuntu box it works fine.
rangalo
@rangalo: :%s/ /\r/g works fine for me too, but not the :%s/ /\n/g.
Masi
+7  A: 

Try

%s/ /\r/g
rangalo
+4  A: 

Enter the following:

:s/ /

and now type Ctrl-V or Ctrl-Q (depends on your configuration) and hit the Enter key. You should now have:

:s/ /^M

Finish it off:

:s/ /^M/g

and you are good to go.

anon
+1  A: 

Try either

For Unix:

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

For Windows:

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

This contains an escape character for the space.

samoz
I'm fairly sure that \r works on all systems.
Al
Interesting. I'll have to give that a try.
samoz
+1  A: 

Specifically to answer your problem with trailing characters, this is the regex you specified:

:%s/ /s/\n/g

You have too many /. What happens is that you replace ' ' with s, and then you tag on this after the substitution: \n/g

I think you meant this:

:%s/ \s/\n/g

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

Nathan Fellman