Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!think.com!mintaka!ogicse!unmvax!crowley From: crowley@unmvax.cs.unm.edu (Charlie Crowley) Newsgroups: comp.editors Subject: Re: Global Empty Line Removal in vi? Message-ID: <1991Jan30.001551.7538@unmvax.cs.unm.edu> Date: 30 Jan 91 00:15:51 GMT References: <1991Jan24.163844.7521@unmvax.cs.unm.edu> <1991Jan26.233521.17721@ddsw1.MCS.COM> <1991Jan27.073848.27626@ddsw1.MCS.COM> Sender: crowley@unmvax.cs.unm.edu Organization: University of New Mexico Lines: 49 Well, the first time I read this I misunderstood and thought he wanted to get rid of all blank lines. I looked at the title and not closely enough at the text. Getting rid of all but one is harder. Solutions so far: (1) filtering through cat -s (on bsd systems) (2) using awk -- pretty easy if you know awk (3) using sed -- somewhat complex I found a way (albeit complex) to do it from within vi. (1) find a string that does not occur anywhere in the text. I will use 'qqq' below. It is easy to check by searching for it and getting a failure. /qqq (2) replace all blank lines withi this string :g/^$/s/^/qqq/ (3) Join all such lines. This must be done one step at a time where each step joins half of the remaining lines. :g/qqq$/j :g/qqq$/j :g/qqq$/j :g/qqq$/j You have to do it until it does not affect any lines. The four above will work if no run of blank lines is more than 32 blank lines. If you :set report=1 you will find out whether it affected any lines. At this point all the 'qqq 's for each run of blank lines are all on the same line. The ' ' is a side effect of the join (j). (4) Replace the first one of these with a newline. :%s/qqq /^M/ where the '^M' was produced by ^V then ^M. There are no control characters in the above two lines for this presentation but they are needed when you type the command into vi. (5) Remove all the extra 'qqq 's. :%s/qqq //g Okay, it is complex. It did it in X by having the command lines in a file and copying them into vu using the X selection. I tried combining them into a macro but I got confused in a morass of ^Vs. Maybe someone more expert in vi :map's can tell me how to do it. The two flaws are the need to find a unique string (minor) and the need to iterate the step (3) command. Charlie Crowley Univ. of New Mexico Computer Science Department