Thursday, October 26, 2017

Re: recommendation for a function over the entire buffer

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 vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment