using :s

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

using :s

Stephen Morton
I'm fairly new to vim, so I apologize if this is something glaringly
obvious I'm overlooking, but a decent amount of google searches and
looking through online documentation has failed to help me.

I want to do this

:%s/.\n./{string}/g

where {string} is identical to the expression in the first part,
except the newline replaced with a space. I've tried everything I can
think of to try and accomplish this, and read the documentation, but
I'm stuck. What I'm doing is taking a text document from project
Gutenberg, which has newlines built in to make a uniform page width,
and remove these so I can put it on my palm pilot, where the page
width is smaller, and it will automatically wrap to work with this.
Doing it this way preserves the breaks in between paragraphs.

Thanks in advance for any help here.
Reply | Threaded
Open this post in threaded view
|

Re: using :s

Aaron Griffin
On 8/15/05, Stephen Morton <[hidden email]> wrote:
> I want to do this
>
> :%s/.\n./{string}/g

I'll give you two answers...
when you want to "reuse" a matched string in a regexp, you need to
wrap the match with \( and \) to "group" the match.  Using \1 will
then provide the matched string in the "replace" portion (this works
for \2 \3 \4 ...\N for subsequent groups as well).

However, what you want to do is already accounted for in vim.  the "J"
command (that's shift+j) will join two lines (intelligently too, if
there is a period it will join them with 2 spaces).  You should be
able to do [count]J where [count] is the number of lines to join.

So with a document of 120 lines, move to the top (gg) and hit 120J.
Reply | Threaded
Open this post in threaded view
|

Re: using :s

Alan G Isaac
In reply to this post by Stephen Morton
On Mon, 15 Aug 2005, Stephen Morton apparently wrote:
> :%s/.\n./{string}/g

I understand that you want to join paragraphs to a single
line but retain an end-of-line between paragraphs?  Maybe
g/./.,/^\s*$/j

Does that do it?

Cheers,
Alan Isaac



Reply | Threaded
Open this post in threaded view
|

Re: using :s

Charles E Campbell Jr
In reply to this post by Stephen Morton
Quoting Stephen Morton <[hidden email]>:
>...
> I want to do this
>
> :%s/.\n./{string}/g
>
> where {string} is identical to the expression in the first part,
> except the newline replaced with a space...

:%s/\(.\)\n\(.\)/\1 \2/g

Explanation:

\(.\)  - record character in substitution expression #1
\n     - newline
\(.\)  - recored character in substitution expression #2

The \1 and \2, of course, refer to those substitution expressions.

Regards,
Chip Campbell

Reply | Threaded
Open this post in threaded view
|

Re: using :s

Giorgos Gaganis
In reply to this post by Stephen Morton
Stephen Morton wrote:

>I'm fairly new to vim, so I apologize if this is something glaringly
>obvious I'm overlooking, but a decent amount of google searches and
>looking through online documentation has failed to help me.
>
>I want to do this
>
>:%s/.\n./{string}/g
>  
>
Try replacing \n with \r

>where {string} is identical to the expression in the first part,
>except the newline replaced with a space. I've tried everything I can
>think of to try and accomplish this, and read the documentation, but
>I'm stuck. What I'm doing is taking a text document from project
>Gutenberg, which has newlines built in to make a uniform page width,
>and remove these so I can put it on my palm pilot, where the page
>width is smaller, and it will automatically wrap to work with this.
>Doing it this way preserves the breaks in between paragraphs.
>
>Thanks in advance for any help here.
>
>
>  
>

Reply | Threaded
Open this post in threaded view
|

Re[2]: using :g

Alan G Isaac
In reply to this post by Alan G Isaac
On Thu, 18 Aug 2005, someone wrote:
> Can you pls explain how the command works
> g/./.,/^\s*$/j

Go to
:help :g
and you will find (roughly)

:g/{pattern}/[cmd]
                        Execute the Ex command [cmd] on the
                        lines where {pattern} matches.

So you identified the pattern already:
:g/./
is going to operate on any line containing any character.
So your question is really about the command:
.,/^\s*$/j

Going to
:help :j
you find (rougly)
:[range]j
                Join [range] lines.

So now you know this joins lines in the range  .,/^\s*$/
But what is this range?

Going to
:h range
you find (roughly)
Line numbers may be specified with: *:range*
        . the current line  
        /{pattern}[/] the next line where {pattern} matches  *:/*

So the range is from the current line to the pattern
^\s*$
^  - start of line
\s - space character
*  - zero or more ties
$  - end of line
I.e., a line that is blank or has only white space.

So whenever Vim finds a line that is not empty,
it joins it to all the following lines until it hits
a blank line (or whitespace only line).

Note that you'll want a blank line at the end of
the file, or your last paragraph won't be joined.

hth,
Alan Isaac