LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-16-2004, 02:58 PM   #1
phreak7t7
LQ Newbie
 
Registered: Apr 2004
Posts: 1

Rep: Reputation: 0
vim search and replace - split line


I need to know how to turn something like line 'A' below into line 'B' using search and replace in vim. Can anyone help?

Line A:
There are 10 types of people in this world. Those who understand binary and those who don't.

Line B:
There are 10 types of people in this world.
Those who understand binary and those who don't.

As you can see, I just need to take one line and split it into three.

I know that I need to use something like

:%s/./.[newline character]/g

to do this throughout my file but don't know what to put to represent adding a newline in a search and replace command.

Any help would be appreciated.

Thanks,

Paul.

P.S. If anyone knows of a place I can find an easy-to-read guide to regular expressions I'd appreciate that too.

P.P.S. Are there any major differences between regexp format in vim and sed?
 
Old 08-16-2004, 03:55 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
:%s/\. */.[ctrl-V][Enter]/g

seems to work. The \ before the . is important because . normally matches any character. As you can see, ctrl-V is used for escaping various things, like enter. That's not in sed but for the most part they are the same regexes as in sed.
 
Old 08-16-2004, 05:02 PM   #3
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
For fun you could do this with a macro too.

Code:
q1f.a<Enter><Esc>q
100@1
Decoded:
q - begin recording macro...
1 - number 1 (bound to key 1, really, doesn't need to be a number, any alphanumeric char would do)
f - find the next ...
. - period
a - append a ...
<Enter> - carriage return/line feed
<Esc> - go back into command mode
q - ...and finish recording the macro. Now, ...
100 - 100 times
@ - execute macro ...
1 - number 1


Granted, that's imprecise because you have to know how many periods you have in the file (or over-guess and clean up afterwards), but it's just a neat little exercise.
 
  


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
vim 'n' lynx: command line option to search for string Samsara Linux - Software 6 01-02-2013 06:36 PM
Search and Replace with multiple-line strings ChristianNerds.com Programming 4 08-21-2005 02:32 PM
Quick VIM question (unhighlighting search terms after search) lrt2003 Linux - Newbie 5 05-08-2004 05:21 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration