Ali
Ali

Reputation: 456

vim substitute function with s/

I'm trying to execute the following

%s/foo/\=substitute(getline('.'),'bar','','g')

to this example

this is a foo cool bar

what I would like it to return is

this is a bar cool bar

but it's returning

this is a this is a foo cool  cool bar

meaning the whole line is returned not just the matched regexp in the substitute() function

am I missing something?

I'm aware of the split() function and sed implementation, but I want it in substitute()

Upvotes: 0

Views: 1768

Answers (1)

Kent
Kent

Reputation: 195129

why substitute()?

why not simply :s/foo/bar/ ?

anyway, both ugly commands should work ... :(

:%s/foo/\=substitute(submatch(0),".*","bar","g")/

:%s/.*/\=substitute(submatch(0),"foo","bar","g")/

if you prefer to do it only with functions, this works too:

:%call setline(line('.'),substitute(getline('.'),'foo','bar','g'))

Upvotes: 1

Related Questions