recommendation for a function over the entire buffer

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

recommendation for a function over the entire buffer

Jose Caballero
Hello,

if I want to write a function that does something after every line
matching a given pattern (for example, for every python method
definition), what does the community prefer to handle it?
Just iteratively?
Or with a recursive function where the range changes from call to call?

I am just wondering if there is a favorite way for this type of things...

Cheers,
Jose

--
--
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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: recommendation for a function over the entire buffer

Tim Chase
On 2017-10-26 19:13, Jose Caballero wrote:
> if I want to write a function that does something after every line
> matching a given pattern (for example, for every python method
> definition), what does the community prefer to handle it?
> Just iteratively?

The pattern you describe matches nigh-exactly the functionality of
the :g command

  :g/^\s*def\>/ACTION

You don't mention *what* you want to do for each of these lines, but
the ex-mode :g command gives you lots of power. From each of those
matching lines, you can define a range relative to that line and
perform any command you want over that range.  It could be as simple
as "append a comment after every function definition":

  :g/^\s*def\>/s/$/ # my comment here

or adding a Python docstring underneath it:

  :g/^\s*def\>/put='"Docstring here"'

or indent the following line through the next blank line:

  :g/^\s*def\>/sil! +,/^\s*$/->

That's the implicit way of writing the clearer

  :g/^\s*def\>/sil! .+1,/^\s*$/-1>

It (and its counterpart ":v") is one of the most powerful vim
commands and I use it nigh-daily.

-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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: recommendation for a function over the entire buffer

Jose Caballero
2017-10-26 20:24 GMT-04:00 Tim Chase <[hidden email]>:

> On 2017-10-26 19:13, Jose Caballero wrote:
>> if I want to write a function that does something after every line
>> matching a given pattern (for example, for every python method
>> definition), what does the community prefer to handle it?
>> Just iteratively?
>
> The pattern you describe matches nigh-exactly the functionality of
> the :g command
>
>   :g/^\s*def\>/ACTION
>
> You don't mention *what* you want to do for each of these lines, but
> the ex-mode :g command gives you lots of power. From each of those
> matching lines, you can define a range relative to that line and
> perform any command you want over that range.  It could be as simple
> as "append a comment after every function definition":
>
>   :g/^\s*def\>/s/$/ # my comment here
>
> or adding a Python docstring underneath it:
>
>   :g/^\s*def\>/put='"Docstring here"'
>
> or indent the following line through the next blank line:
>
>   :g/^\s*def\>/sil! +,/^\s*$/->
>
> That's the implicit way of writing the clearer
>
>   :g/^\s*def\>/sil! .+1,/^\s*$/-1>
>
> It (and its counterpart ":v") is one of the most powerful vim
> commands and I use it nigh-daily.
>
> -tim
>
>
>

Hi Tim,

thanks for the pointer.
Quick question: when using the :g command, can I call a plugin function? Like

:g/<pattern>/:call AFunction()

Well, I think I can investigate that myself :)

Thanks a lot.
Jose

--
--
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].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: recommendation for a function over the entire buffer

Tim Chase
On 2017-10-26 21:12, Jose Caballero wrote:
> Quick question: when using the :g command, can I call a plugin
> function? Like
>
> :g/<pattern>/:call AFunction()

Yes (that 2nd colon is optional).  It would require the function to
context-aware with regards to the current line, but otherwise, it
Should Work™.  Other than a second :g or :v command, I believe any Ex
command (or chain of "|"-separated commands) can follow
the :g/pattern/ so :call certainly qualifies.

-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].
For more options, visit https://groups.google.com/d/optout.