Monday, January 4, 2010

Re: How to use an expression as the pattern

Karl Cai wrote:
> I sent the email too soon. I think this question was asked and replied
> at
>
> http://groups.google.com/group/vim_use/browse_thread/thread/ab9c297a30bffcf1/75db1eff8dacffaf?hl=en&lnk=gst&q=use+a+variable+as+search+pattern#75db1eff8dacffaf

A slightly more robust way involves escaping the variable with
escape(), something like

:exec ':%s/'.escape(myvar, '/.*^$[~\\').'/replacement/g'

You can also do this something like

:let @/=myvar
:%s//replacement/g

which sets the "previous search" register to your contents (it
will accept things like regexps, without needing to escape the
"/" character), and then searches for it, because an empty search
reuses the last search pattern.

Additionally, you can alo use control+R followed by equals to
enter an arbitrary expression to be evaluated, such as


:%s/<c-r>=42*2<cr>/replacement/g

to make your command

:%s/84/replacement/g

Just a few suggestions to try.

-tim


--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment