Using substitute command or global cmd to analyse a whole file

9 views
Skip to first unread message

epanda

unread,
Dec 13, 2009, 3:22:09 PM12/13/09
to vim_use
Hi,


I have done a func that I call on that way :

:%s/myPattern/\=MY_FUNC(submatch(4),submatch(5),submatch(3),submatch
(2))/



I would like to not substitute the pattern, just analyse each line.
Thanks



NB : I don't want to use while to read the file.

Gary Johnson

unread,
Dec 13, 2009, 5:00:27 PM12/13/09
to vim_use
On 2009-12-13, epanda wrote:
> Hi,
>
>
> I have done a func that I call on that way :
>
> :%s/myPattern/\=MY_FUNC(submatch(4),submatch(5),submatch(3),submatch
> (2))/
>
>
>
> I would like to not substitute the pattern, just analyse each line.
> Thanks

How about this?

:%call MY_FUNC(getline('.'))

HTH,
Gary


John Beckett

unread,
Dec 13, 2009, 7:24:28 PM12/13/09
to vim...@googlegroups.com
epanda wrote:
> I have done a func that I call on that way :
>
> :%s/myPattern/\=MY_FUNC(submatch(4),submatch(5),submatch(3),submatch
> (2))/
>
> I would like to not substitute the pattern, just analyse each line.

Add the n flag: :%s/.../.../n

John

Tim Chase

unread,
Dec 13, 2009, 7:34:26 PM12/13/09
to vim...@googlegroups.com
That was my first thought too, but it looks like the replacement
portion isn't evaluated if you use the /n flag. I tried the
following on some sample text:

:let x=[] | %s/\w\+/\=add(x, submatch(0))/gn

and x was still empty at the end of it (it was populated when the
/n flag wasn't used). This suggests that Epanda's MY_FUNC()
won't get called with the /n flag. To get evaluation, I had to
do something like

:%s/myPattern/\=MyFunc(...)/g | u

to let it do the replacements and then undo them. This does
require an appropriate (non-zero) 'undolevels' setting.

-tim





John Beckett

unread,
Dec 14, 2009, 1:29:57 AM12/14/09
to vim...@googlegroups.com
Tim Chase wrote:
>> Add the n flag: :%s/.../.../n
>
> That was my first thought too, but it looks like the
> replacement portion isn't evaluated if you use the /n flag.

Ahh ... now that you point it out, it's a bit obvious that
evaluating the replacement should be pointless when using 'n'.

John

Jürgen Krämer

unread,
Dec 14, 2009, 1:52:59 AM12/14/09
to vim...@googlegroups.com

Hi,

epanda schrieb:
> Hi,
>
>
> I have done a func that I call on that way :
>
> :%s/myPattern/\=MY_FUNC(submatch(4),submatch(5),submatch(3),submatch
> (2))/
>
>
>
> I would like to not substitute the pattern, just analyse each line.
> Thanks

add \zs at the end of the search pattern and let your function return
an empty string:

function! MY_FUNC(a, b, c, d)
" ...
return ''
endfunction

:%s/myPattern\zs/\=MY_FUNC(submatch(4),submatch(5),submatch(3),submatch(2))/

Regards,
J�rgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)

epanda

unread,
Dec 14, 2009, 3:15:44 AM12/14/09
to vim_use
> Hi,
>
> epanda schrieb:
>
> > Hi,
>
> > I have done a func that I call on that way :
>
> > :%s/myPattern/\=MY_FUNC(submatch(4),submatch(5),submatch(3),submatch
> > (2))/
>
> > I would like to not substitute the pattern, just analyse each line.
> > Thanks
>
> add \zs at the end of the search pattern and let your function return
> an empty string:
>
>   function! MY_FUNC(a, b, c, d)
>       " ...
>       return ''
>   endfunction
>
>   :%s/myPattern\zs/\=MY_FUNC(submatch(4),submatch(5),submatch(3),submatch(2)) /
>

Thanks, I didn't remind zs but effectively it is the solution I
wanted.

I was using u (undo) temporary.

Thank to all of you

Christian Brabandt

unread,
Dec 14, 2009, 3:55:35 AM12/14/09
to vim_use
Hi epanda!

On So, 13 Dez 2009, epanda wrote:

> I have done a func that I call on that way :
>
> :%s/myPattern/\=MY_FUNC(submatch(4),submatch(5),submatch(3),submatch
> (2))/
>
>
>
> I would like to not substitute the pattern, just analyse each line.

I think, this should work:
Define your function such that you pass submatch(0). Inside your
function you can then access all different submatches. At the end of
your function return submatch(0).

regards,
Christian
--
hundred-and-one symptoms of being an internet addict:
12. You turn off your modem and get this awful empty feeling, like you just
pulled the plug on a loved one.
Reply all
Reply to author
Forward
0 new messages