begin quoting Ralph Shumaker as of Sun, Feb 11, 2007 at 05:07:45PM -0800: > I've been having fun, playing with regular expressions in vi. But I'd > like to do more. > > How can I do a search that regards line breaks as if they were a space?
Don't think you can in vi. In vim, use \n. To search for whitespace-or-newline, use [], e.g., "/a[ \n]space[ \n]*>" search for the string "a" followed by a space or newline followed by "space" followed by any number of spaces and/or newlines followed by ">". > If that's not easy, then how can I replace all line breaks with a space > except for line breaks or double line breaks preceding any line that > begins with "^"? This is where I'd be tempted dig out awk or perl. However. > I have a text file that has been line wrapped at about 80 characters. > Each real line begins with the "^". Each wrapped line does not. And > each line that begins with "^" is preceded by a blank line. If I'm restricting myself to vi(m) when converting the file you describe, I'd start with a macro. Possibly something like :map ^P /^[a-zA-Z]^V^M?^\^^V^MJ0 Where : -> go to command mode map -> map a keystroke to the following ^P -> control-p, the keystroke I'm mapping / -> "search" ^ -> start-of-line anchor [a-zA-Z] -> any alpha character ^V -> control-V, "escape next keystroke" ^M -> control-M, "return", which means "do this search" ? -> "search backwards" ^ -> start-of-line anchor \^ -> the caret character ^V -> control-V, "escape next keystroke" ^M -> control-M, "return", which means "do this search" J -> "join" the next line to this line 0 -> go to column 0 (to prepare for the next search) ...and then I'd hit ^P until it didn't find a match, and then undo the last join. Alternatively, the search-and-find-previous could be put after the join, and the first search could be done manually. > > Text that I want to search for (or replace) can wrap around these line > breaks, but not around the "^". Try using "[ \n]*" in your pattern. (The * is to handle trailing spaces *and* newlines, you may want "[ \n][ \n]*" if that causes problems. [snip] > If I want to search the file for "line that is" and replace it with > "line which is", I would do ":%s/line that is/line which is/cg". But > this would totally miss the only occurrence of it in my example above. > If I could get vi to regard line breaks (during a search or replace) as > if they were a space, I'd be set. Hope this helps. -- There's probably a perl one-liner out there already... Stewart Stremler -- [email protected] http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-list
