Re: Visual selection via :global

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

Re: Visual selection via :global

Manas Gupta
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

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200723221818.eghokxeehxssrpsl%40nitro-5.
Reply | Threaded
Open this post in threaded view
|

Re: Visual selection via :global

Tim Chase
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






--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200723175751.6ac1b668%40bigbox.attlocal.net.
Reply | Threaded
Open this post in threaded view
|

Re: Visual selection via :global

Manas Gupta
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.

> > 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
>
Really thanks for your explanations and suggestion.

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

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200723231341.qezrydbimd7cdxqj%40nitro-5.
Reply | Threaded
Open this post in threaded view
|

Re: Visual selection via :global

Tim Chase
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




--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200723184017.7d8166aa%40bigbox.attlocal.net.
Reply | Threaded
Open this post in threaded view
|

Re: Visual selection via :global

Manas Gupta
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
CSAM Undergraduate | 2022
IIIT-Delhi, India

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200724000656.gzp3vydu2cotcebs%40nitro-5.
Reply | Threaded
Open this post in threaded view
|

Re: Visual selection via :global

Manas Gupta
In reply to this post by Tim Chase
Sorry for wrong CC'ed on previous mail!

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

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200724000944.isnblemvyxzhkata%40nitro-5.
Reply | Threaded
Open this post in threaded view
|

Re: Visual selection via :global

Tim Chase
In reply to this post by Manas Gupta
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














--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200723201823.0b889ff2%40bigbox.attlocal.net.
Reply | Threaded
Open this post in threaded view
|

Re: Visual selection via :global

Manas Gupta
On Thu, Jul 23, 2020 at 08:18:23PM -0500, Tim Chase wrote:

> 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?
>
Thanks a lot for your help.

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

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200724110921.bxoxtn3mzsdfr4kx%40nitro-5.