LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-30-2008, 08:55 PM   #1
ufmale
Member
 
Registered: Feb 2007
Posts: 385

Rep: Reputation: 30
replace space with new line in vi


how to replace every space with newline?

For example, if I have "i want to go to school", i want to convert it to
=====
I
want
to
go
to
school
======

I try :%s/ /\n/g , but it does not seem to work

Also is there a way to count the number of space or number of word within vi?
 
Old 07-30-2008, 08:58 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Use Control-V and then Enter key instead of \n.

Control-V is the special character escape key.
 
1 members found this post helpful.
Old 07-30-2008, 09:05 PM   #3
ufmale
Member
 
Registered: Feb 2007
Posts: 385

Original Poster
Rep: Reputation: 30
wow! it works great..
Do you happen to know how to count number of word, or number of provided character?

For example, I want to know how many character "x" or number of "<tag>" is in the text. Is there a way to do it?
 
Old 07-30-2008, 09:13 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Vim doesn't do this natively, but there may be plug-ins that can help at vim.org.

This is the type of thing you can write a script for, and call from vim.

You can do some trickery. Perform a global substitution of your char or word, and see the status line at the bottom. It will tell you the number of replacements. Then, just undo!

:%s/x/Z/g

then see at the bottom:

4 substitutions on 3 lines
 
Old 07-30-2008, 09:29 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
another way, without undo
Code:
:%s/e/&/g
 
Old 07-30-2008, 09:29 PM   #6
ne pas
Member
 
Registered: Jul 2008
Posts: 55

Rep: Reputation: 23
Quote:
Originally Posted by ufmale View Post
Also is there a way to count the number of space or number of word within vi?
Press g followed by <Ctrl-g> or use substitute with the 'n' flag to count anything else:
Code:
:%s/\u//gn
counts uppercase characters.
 
Old 07-30-2008, 09:34 PM   #7
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
These are good suggestions. The thing that I don't like about not undo'ing is that it changes the file modified status. I've been a :set autowrite/^Z user for 25+ years, and don't want file modifications times changing unless there really are changes. No big deal if the file is changing anyway though. Good suggestions.
 
Old 09-26-2008, 12:32 PM   #8
graywh
LQ Newbie
 
Registered: Sep 2008
Posts: 1

Rep: Reputation: 0
Quote:
Originally Posted by ufmale View Post
how to replace every space with newline?

For example, if I have "i want to go to school", i want to convert it to
=====
I
want
to
go
to
school
======

I try :%s/ /\n/g , but it does not seem to work

Also is there a way to count the number of space or number of word within vi?
I know you kind of got an answer already, but I wanted to shed some more light on the issue.

The reason it doesn't work is that \n has a different meaning in the replacement part of :substitue. See :help sub-replace-special for more information. Instead, you'll want to use the command :%s/ /\r/g. Use \s instead of a space if you want to include tabs, too. And \s\+ if you want it to replace consecutive spaces and/or tabs with one newline.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed - How do you replace end of line with a space pppaaarrrkkk Programming 7 02-07-2011 12:27 PM
Using sed to replace newling with space binarybob0001 Programming 2 05-29-2008 04:32 PM
First line and replace fs11 Programming 3 04-25-2008 01:17 AM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 09:49 PM
sed: replace one line with >one line bbeers Programming 3 11-19-2002 06:27 PM


All times are GMT -5. The time now is 09:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration