Friday, March 11, 2016

vi Editor: How to replace newline/carriage and replace

Re: Replacing newline/carriage returns in a file with spaces

Tim Chase-2
901 posts
> I have been trying to figure out how I can replace all the new line 
> characters in a text file with spaces - need to do this to make html 
> compatible with java and am too lazy to do it manually every time. 
> 
> Am sure there is a simple way to do it but I have looked and cant figure it 
> out :( 


Well, if you plan to do it regularly, you can use "tr" to do 
exactly what you describe: 


 tr '\012' \040' <in.txt >out.txt 

Or, if you want to do it in vim, you can do: 

        :%j 

which will join all the lines in your file, normalizing 
whitespace.  If you don't want Vim to be smart about it, you can 
tell it to simply remove the newlines: 

        :%j! 

Alternatively, you can use 

        :%s/\n/ / 

which will do the same thing as the "tr" command, only it will 
appropriately leave a \n at the end of the file. 

As a side note, I'm not sure what it meens to "make html 
compatible with java", as they're fairly disjoint languages.  One 
is for markup; the other is for execution.

No comments:

Post a Comment