On Fri, 16 Apr 2010, Tony Mechelynck wrote:

> On 16/04/10 01:00, Benjamin R. Haskell wrote:
> > I encountered an oddity when trying to use the substitute() function 
> > to prepend '# ' to the start of every line returned from a command.
> > 
> > A very simplified example:
> > 
> > I might expect
> > :echo substitute("a\na",'^','b','g')
> > to echo "ba\na"
> > 
> > But, why does
> > :echo substitute("a\na",'\_^','b','g')
> > also echo "ba\na" -- instead of "ba\nba"?
> > 
> > In the end, my ugly workaround was to use:
> > join(map(split(system(...),"\n"),'substitute(v:val,x,y,z)'),"\n")
> > where I wanted just:
> > substitute(system(...),x,y,z)
> > 
> 
> \_^ matches start-of-line. However, unlike the range of the 
> :s[ubstitute] command, the first argument of the substitute() function 
> is regarded by Vim as one line. It has therefore only one 
> start-of-line -- at the very beginning of the string.
> 
> What you can do is use '^\|\n\zs' as second argument of substitute(), 
> to match the start of the first argument or (zero-length) whatever 
> follows an embedded linefeed.

Clever.  I like it.

In my case, I realized after I'd sent it that I didn't really need the 
power of substitute() after I used the join-map-split version:

join(map(split(system(...),"\n"),'"# ".v:val'),"\n")

But, I prefer your workaround:

substitute(system(...),'^\|\n\zs','# ','g')

-- 
Thanks,
Ben

-- 
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

Subscription settings: http://groups.google.com/group/vim_use/subscribe?hl=en

Reply via email to