Search for the contents of a variable

3 views
Skip to first unread message

ACR

unread,
Mar 14, 2008, 3:21:18 AM3/14/08
to vim_use
Hi all,

Can someone tell me how to search for a pattern stored in a local
variable.

Say, I have
:let str = <some string>
(i.e like, :let str = "hello")

Now, How can I search for the string stored in str?

Also, If I have two variables holding two strings, how can I search
for the string stored in the first variable and replace it with the
string stored in the second variable?

i.e
let str1 = "pattern1"
let str2 = "pattern2".

How can I replace all occurrences of "pattern1" in the current file
by "pattern2", using str1 and str2 in the search command?

Thanks,
Anith.

Tony Mechelynck

unread,
Mar 14, 2008, 3:29:25 AM3/14/08
to vim...@googlegroups.com

To replace all occurrence of the _pattern_ in str1 by the _string_ in
str2 (which may contain backrefs but no wildcards), use

:exe '1,$s/' . str1 . '/' . str2 . '/g'

see
:help :execute
:help :substitute

Best regards,
Tony.
--
"Now the Lord God planted a garden East of Whittier in a place called
Yorba Linda, and out of the ground he made to grow orange trees that
were good for food and the fruits thereof he labeled SUNKIST ..."
-- "The Begatting of a President"

ACR

unread,
Mar 14, 2008, 4:40:42 AM3/14/08
to vim_use

Thanks a lot Tony! :)

With :exec, both are working great!!!
Reply all
Reply to author
Forward
0 new messages