jhoran
jhoran

Reputation: 340

Vim substitute() function flags order changes its behavior?

I have the following line made of tab separated strings ; I have sometimes multiple sequential <Tab>:

zer<Tab><Tab>abc<Tab>def<Tab><Tab>iop<Tab><Tab>

I want to insert the 'null' string between 2 consecutive <Tab> ; I run the following command:

:s/\t\(\t\)\@=/\tnull/eg

which give me as I expected:

zer<Tab>null<Tab>abc<Tab>def<Tab>null<Tab>iop<Tab>null<Tab>

The equivalent substitute function to the above command is (I echoed its result):

:echo substitute(getline('.'),'\t\(\t\)\@=','\tnull','eg')

which insert a <Tab> only between the first two <Tab>s:

zer<Tab>null<Tab>abc<Tab>def<Tab><Tab>iop<Tab><Tab>

whereas if I change the order of the substitute flags in the substitute function call ('eg' replaced by 'ge'):

    :echo substitute(getline('.'),'\t\(\t\)\@=','\tnull','ge')

Then I get the expected result:

zer<Tab>null<Tab>abc<Tab>def<Tab>null<Tab>iop<Tab>null<Tab>

It seems that the order of the flags in the substitute() function change its behavior while it has no effect for the substitute command. Does anyone have an idea why that ?

Upvotes: 0

Views: 96

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172698

The :substitute command can take many flags, but the substitute() function only supports the g flag. Flags like c (for interactivity) or e (for error suppression) do not apply to the low-level function.

Upvotes: 0

romainl
romainl

Reputation: 196777

From my limited understanding of C, it looks like Vim only cares about the {flags} argument if its first character is g:

do_all = (flags[0] == 'g');
[…]
if (!do_all)
    break;

This may explain the fact that :help substitute() only mentions g when explaining {flags}:

When {flags} is "g", all matches of {pat} in {expr} are
replaced.  Otherwise {flags} should be "".

Upvotes: 1

Related Questions