Tuesday, May 10, 2016

Re: why %s/b//gn doesn't work?

On 2016-05-09 22:02, 李哲 wrote:
> 在 2016年5月7日星期六 UTC+8上午10:53:10,Tim Chase写道:
> > I get the same behavior in both cases (because of the /n flag,
> > neither actually performs the replacement. I recall there was a
> > patch that performed evaluations of the replacement in the case it
> > was a "\=" (sub-replace-special), but didn't actually replace the
> > text.
>
> tnx a lot for you reply but I get a little confused about the word
> `evaluations`, what is the meaning here about it?

Vim lets you execute code on each match, for example

:%s/\d\+/\=submatch(0)+1/g

will find all the numbers in your file (/\d\+/) and replace them with
the result of adding one to them (\=submatch(0)+1)

Before 7.3.627, that expression wouldn't get evaluated if you used
the /n flag, so you couldn't do things like

:let x=[] | %s/\w\+/\=add(x, submatch(0))/gn

to build a list of every matching result because the /n flag
prevented the add() call from actually executing. It could be worked
around by creating a wrapper function that did the add() and then
returned the match (performing a null-op replacing with the same
thing), or omit the /n flag, let it change the file, then undo it.

It came up in this thread:

http://vim.1045645.n5.nabble.com/Using-substitute-command-or-global-cmd-to-analyse-a-whole-file-td1191293.html

-tim





--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: