Saturday, June 30, 2012

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

On 29/06/12 20:06, Bee wrote:
>
>
> On Jun 29, 10:56 am, Taylor Hedberg <tmhedb...@gmail.com> wrote:
>> 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.
>>
>> signature.asc
>> < 1KViewDownload
>
> Thank you.
>
> My confusion, I was thinking the 'j' was the 'normal' version for line
> down and was expecting the 'normal J' for join. Now I understand it is
> ':j' for join lines.
>

Yes, the command to be executed on each match of the :g command is
always an ex-command (the kind you would use after typing a colon, or on
any line of a script such as your vimrc). If you don't specify an
ex-command after the pattern, then :p is used by default (print, which
defaults to displaying the current line if no range is used). That's
where the Unix grep command got its name: g/re/p where /re/ stands for
"regular expression".


Best regards,
Tony.
--
"I'd love to go out with you, but I have to stay home and see if I
snore."

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: