Zam
Zam

Reputation: 377

How to exit a function, if a Search pattern found?

I have a function which contains certain search and replace pattern, like

function! SearchReplace()
%s/Search_string/\\new{Search_string}/g
%s/string/\\new{string}/g
endfunc

The o/p will \new{Search_\new{string}}. I need \new{Search_string} only. Is there any way to exit the function, if any one of the pattern found

Upvotes: 1

Views: 163

Answers (1)

Luc Hermitte
Luc Hermitte

Reputation: 32966

You could start with a call to search(), or test v:errmsg.

But, isn't your string a word?

:%s/\<string\>/\\new{string}/g

Beside, why two calls where one is enough?

%s/\v<(string|Search_string)>/\\new{\1}/g

Upvotes: 3

Related Questions