Friday, June 29, 2012

Re: please explain g/\S/,/^\s*$/j

Bee, Fri 2012-06-29 @ 10:39:54-0700:
> Please explain HOW this works.
> g/\S/,/^\s*$/j
>
> I see it joins all lines by paragraph.
>
> g/\S/ find all lines that contain non-whitespace
>
> , ??? what does this do?
>
> /^\s*$/ find all blank lines (only zero or more whitespace)
>
> j ??? what does this do?

`,/^\s*$/j` is a command that will be executed on each line that matches
the /\S/ pattern (that's how the :g command works: :g/pattern/command).
So it will be executed on each line that contains a non-whitespace
character.

The actual command here is `j`, which joins lines. Like many ex
commands, it can be prefixed with a range of addresses to indicate which
line(s) it should apply to. A range consists of an address, then a
comma, then another address. In this case, the first address is omitted,
which means it defaults to the current line (in this case, the line that
matched the /\S/ pattern). The comma separates this from the second
address, /^\s*$/. These two addresses comprise the start and end lines
for the join operation.

So in a nutshell, this command finds every non-blank line in the buffer.
On each of those lines (A), it searches forward for the next blank line
(B), and joins each line in the range from A to B using the :j command.

No comments: