Tuesday, January 5, 2010

Re: How to use an expression as the pattern

Karl Cai wrote:
> Hi Vim-users,
>
> Can somebody tell me how to use an expression (e.g., the output of a
> command) as the search pattern? I am using vim 7.0.94
>
> For example:
>
> :let x = system("hostname") # say x is "mycomputer" now. This should work
> :%s/$x/yourcomputer/g # trying to change all "mycomputer" to
> "yourcomputer". This doesn't work
This doesn't work because vim doesn't expand $x. That's a shell
construct (bash, posix, korn, etc).
Instead, read up on what vim does support. Try

:help exe
> Or
> :%s/\=x/yourcomputer/g # same purpose, but doesn't work either.

Again, whatever this construct is, it is clearly not vim. Vim is not
Perl. Vim is not a Posix shell interpreter.
>
> In section 9 (9. Compare with Perl patterns) of doc patten.txt it says:
>
> Finally, these constructs are unique to Perl:
> - execution of arbitrary code in the regex: (?{perl code})
>
> Does this mean that vim can't use an arbitrary expression as the
> search pattern? If so, any known workaround?
An arbitrary expression can include "magic" characters. Do you want
them to be literal? Read :help /magic ; you may want to use \V (even
with that, the $ retains magic). Read :help escape() on how to escape
characters.

Regards,
Chip Campbell

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

No comments:

Post a Comment