Re: Visual selection via :global

11 views
Skip to first unread message

Manas

unread,
Jul 23, 2020, 6:18:40 PM7/23/20
to v...@vim.org
Hi folks, I have a markdown file containing a couple of headings and
some pointers as shown below.

```
# Heading 1
- pointer 1
- pointer 2
- pointer 3

## Heading 2
- pointer 4
- pointer 5
```

I wanted to visual select all pointers only. So what I did was
`:g/^-/normal V` but it only selects the last pointer (i.e. pointer 5).
Shouldn't it select all pointers?

Is this normal behaviour? And what am I missing here?

Also according to `:h :normal` while writing `:normal {commands}`,

{commands} cannot start with a space. Put a count of
1 (one) before it, "1 " is one space.

I did not quite get the meaning out of it. Can someone help me?

Thanks
--
Manas
CSAM Undergraduate | 2022
IIIT-Delhi, India

Tim Chase

unread,
Jul 23, 2020, 6:58:05 PM7/23/20
to Manas, vim...@googlegroups.com, v...@vim.org
On 2020-07-24 03:48, Manas wrote:
> Hi folks, I have a markdown file containing a couple of headings and
> some pointers as shown below.
>
> ```
> # Heading 1
> - pointer 1
> - pointer 2
> - pointer 3
>
> ## Heading 2
> - pointer 4
> - pointer 5
> ```
>
> I wanted to visual select all pointers only. So what I did was
> `:g/^-/normal V` but it only selects the last pointer (i.e. pointer
> 5). Shouldn't it select all pointers?

Vim doesn't (to the best of my knowledge without hacky plugins)
support disjoint selections. That said, you don't mention what you
want to *do* with those lines once you've selected them. Change
their case? Indent them? Only do a :substitute command on
list-items and not headings? I presume you don't just want the lines
visually-selected for aesthetic reasons. ;-)

> Is this normal behaviour? And what am I missing here?

Yes, it is the normal/expected behavior, so you're not missing
anything. :-)

> Also according to `:h :normal` while writing `:normal {commands}`,
>
> {commands} cannot start with a space. Put a count of
> 1 (one) before it, "1 " is one space.
>
> I did not quite get the meaning out of it. Can someone help me?

Though mostly orthogonal to your previous text, this means that when
vim is parsing the "normal" command

:normal ~
^
and

:normal ~
^^^^^

are both the same thing, that the normal-command(s)-to-be-executed
don't start until after the leading whitespace. Thus if you really
do want the first character of your "normal" command to be a space,
you have to do

:normal 1 ~
:normal 1 ~

Alternatively, I prefer to execute the expression-register as a macro
instead:

@=' ~'

which is similar. That said, I'm not sure it has much to do with
your previous issues.

-tim






Manas

unread,
Jul 23, 2020, 7:14:01 PM7/23/20
to Tim Chase, v...@vim.org
On Thu, Jul 23, 2020 at 05:57:51PM -0500, Tim Chase wrote:
> On 2020-07-24 03:48, Manas wrote:
> > Hi folks, I have a markdown file containing a couple of headings and
> > some pointers as shown below.
> >
> > ```
> > # Heading 1
> > - pointer 1
> > - pointer 2
> > - pointer 3
> >
> > ## Heading 2
> > - pointer 4
> > - pointer 5
> > ```
> >
> > I wanted to visual select all pointers only. So what I did was
> > `:g/^-/normal V` but it only selects the last pointer (i.e. pointer
> > 5). Shouldn't it select all pointers?
>
> Vim doesn't (to the best of my knowledge without hacky plugins)
> support disjoint selections. That said, you don't mention what you
> want to *do* with those lines once you've selected them. Change
> their case? Indent them? Only do a :substitute command on
> list-items and not headings? I presume you don't just want the lines
> visually-selected for aesthetic reasons. ;-)
>
I want to yank those lines in order to paste somewhere else. In this
particular case, I wanted to send it to someone.
Really thanks for your explanations and suggestion.

Tim Chase

unread,
Jul 23, 2020, 7:40:27 PM7/23/20
to vim...@googlegroups.com
On 2020-07-24 04:43, Manas wrote:
> On Thu, Jul 23, 2020 at 05:57:51PM -0500, Tim Chase wrote:
>> On 2020-07-24 03:48, Manas wrote:
>>> Hi folks, I have a markdown file containing a couple of
>>> headings and some pointers as shown below.
>>>
>>> ```
>>> # Heading 1
>>> - pointer 1
>>> - pointer 2
>>> - pointer 3
>>>
>>> ## Heading 2
>>> - pointer 4
>>> - pointer 5
>>> ```
>>>
>>> I wanted to visual select all pointers only.
>>
>> Vim doesn't (to the best of my knowledge without hacky plugins)
>> support disjoint selections. That said, you don't mention what
>> you want to *do* with those lines once you've selected them.
>
> I want to yank those lines in order to paste somewhere else. In this
> particular case, I wanted to send it to someone.

Ah, the typical idiom here is to empty a register and then
yank-appending (yank into a capital-letter-named register) each of
the matching lines like

:let @a='' | g/^-/y A

This will gather all the matching lines into register "a" which you
can then either paste elsewhere, or transfer directly to your
system clipboard-register with

:let @+=@a

This lets you use any pattern you want to put matching lines on the
clipboard.

Hope this helps,

-tim

:help quote_alpha




Manas

unread,
Jul 23, 2020, 8:07:11 PM7/23/20
to vim...@googlegroups.com, tim@nitro-5
On Thu, Jul 23, 2020 at 06:40:17PM -0500, Tim Chase wrote:
> On 2020-07-24 04:43, Manas wrote:
> >
> > I want to yank those lines in order to paste somewhere else. In this
> > particular case, I wanted to send it to someone.
>
> Ah, the typical idiom here is to empty a register and then
> yank-appending (yank into a capital-letter-named register) each of
> the matching lines like
>
> :let @a='' | g/^-/y A
>
It worked like a charm. Apparently, the right command was `:g/^-/y A`
instead of `:g/^-/normal yy`. Although I am not able understand the use
of `A` after `y`. Can you please explain that?

Once again thanks a lot for your help!

Manas

unread,
Jul 23, 2020, 8:09:59 PM7/23/20
to vim...@googlegroups.com
Sorry for wrong CC'ed on previous mail!

Tim Chase

unread,
Jul 23, 2020, 9:18:33 PM7/23/20
to Manas, vim...@googlegroups.com
On 2020-07-24 05:36, Manas wrote:
> On Thu, Jul 23, 2020 at 06:40:17PM -0500, Tim Chase wrote:
> > :let @a='' | g/^-/y A
> >
> It worked like a charm. Apparently, the right command was `:g/^-/y
> A` instead of `:g/^-/normal yy`. Although I am not able understand
> the use of `A` after `y`. Can you please explain that?

The ex "y" command yanks and can take an optional register to yank
to. By default, your

:g/^-/normal yy

(using the normal-mode) would be the same as ex-mode "y" command:

:g/^-/y

which yanks, overwriting the default " register.

If you specify a register, vi/vim will yank into that register
instead. In normal mode, you'd prefix your command with the
register-name:

"ayy

to yank the current line into register "a".

The trick is that *uppercase* registers write to the same register as
their lowercase variants, but they *append* instead of *overwrite*.

:help quote_alpha

So by using

:g/^-/y A

(or as a jump to normal mode

:g/^-/normal "Ayy

would do the same thing) each line matching your pattern gets yanked
*and appended* to register "a", gathering them all up so you can then
use the "a" register either to paste or transfer the results to the
system clipboard (the "+" register) via a `:let` command

:let @+=@a

Hopefully that makes more sense of it?

-tim














Manas

unread,
Jul 24, 2020, 7:09:45 AM7/24/20
to Tim Chase, v...@vim.org
Thanks a lot for your help.
Reply all
Reply to author
Forward
0 new messages