the perl x operator ( feature request ? )

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

the perl x operator ( feature request ? )

marc chantreux-2
hi all,

is there a way to multiply a string by a number as the perl x operator
do ? i haven't found it on the vim doc ( using vim7).

in perl :

$x = 'ab' x 4
$x == 'abababab'

it could be usefull to write something like :

:s/.*/\='-'len(submatch(0))/

or :

let indent = ' ' x &sw


in the case that there is no way to do it in vim, this is a feature
request ! :)

regards

mc

Reply | Threaded
Open this post in threaded view
|

Re: the perl x operator ( feature request ? )

Bram Moolenaar

Marc Chantreux wrote:

> is there a way to multiply a string by a number as the perl x operator
> do ? i haven't found it on the vim doc ( using vim7).
>
> in perl :
>
> $x = 'ab' x 4
> $x == 'abababab'
>
> it could be usefull to write something like :
>
> :s/.*/\='-'len(submatch(0))/
>
> or :
>
> let indent = ' ' x &sw
>
>
> in the case that there is no way to do it in vim, this is a feature
> request ! :)

You can use the repeat() function.  Only in Vim 7 though!

        :let x = repeat('ab', 4)
        :let seperator = repeat('-', 80)

--
hundred-and-one symptoms of being an internet addict:
131. You challenge authority and society by portnuking people

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        Sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\              Project leader for A-A-P -- http://www.A-A-P.org        ///
 \\\     Buy LOTR 3 and help AIDS victims -- http://ICCF.nl/lotr.html   ///
Reply | Threaded
Open this post in threaded view
|

Re: the perl x operator ( feature request ? )

Tim Chase-2
>>:s/.*/\='-'len(submatch(0))/
>>
>>or :
>>
>>let indent = ' ' x &sw
>>
>>in the case that there is no way to do it in vim, this is a feature
>>request ! :)
>
> You can use the repeat() function.  Only in Vim 7 though!
>
> :let x = repeat('ab', 4)
> :let seperator = repeat('-', 80)

In case you haven't migrated to Vim7 yet, this function should do
the trick for you:

     fun! Rep(s, count)
         let l:result = ""
         let l:count = a:count
         while (l:count > 0)
             let l:result = l:result . a:s
             let l:count = l:count - 1
         endwhile
         return l:result
     endfun

which you can use just like the Vim7 version of things, or per
your example:

        :%s/.*/\=Rep('-', strlen(submatch(0)))

as a nice little redactor :)  (though NB, it's "strlen", not
"len")

Sure, this could be optimized a tad if large numbers were to be
passed in as the a:count value, but this should be sufficient.

Hope this helps,

-tim





Reply | Threaded
Open this post in threaded view
|

Re: the perl x operator ( feature request ? )

marc chantreux-2
Tim Chase a ?crit :

> In case you haven't migrated to Vim7 yet, this function should do
> the trick for you:

thanks for help, Tim. I'm still under the vim7 charm so y can't use vim6
anymore :)

regards
mc
Reply | Threaded
Open this post in threaded view
|

Re: the perl x operator ( feature request ? )

Wes Potts
In reply to this post by marc chantreux-2
It appears you are more interested in a script solution but, I
sometimes prefix the normal mode insert command 'i' with a count.  Say
I wish to insert a line with 60 '#' characters into a shell script, I
simply use:

60i#<ESC>

Hope this helps.

Wes

On 8/30/05, Marc Chantreux <[hidden email]> wrote:

> hi all,
>
> is there a way to multiply a string by a number as the perl x operator
> do ? i haven't found it on the vim doc ( using vim7).
>
> in perl :
>
> $x = 'ab' x 4
> $x == 'abababab'
>
> it could be usefull to write something like :
>
> :s/.*/\='-'len(submatch(0))/
>
> or :
>
> let indent = ' ' x &sw
>
>
> in the case that there is no way to do it in vim, this is a feature
> request ! :)
>
> regards
>
> mc
>
>
Reply | Threaded
Open this post in threaded view
|

[ my VST fttplugin ] Re: the perl x operator ( feature request ? )

marc chantreux-2
Wes Potts a ?crit :
> 60i#<ESC>

thanks for the tips but it doesn't help.

in fact, i've searched this solution to automaticaly underline my vst heads.

I gave it away for something better : autoindent while underline.

my rst.vim, finally :

if exists('b:did_ftplugin')
     finish
endif
let b:did_ftplugin=1

runtime macros/vst.vim

set sw=2

let mapleader='('
inoremap <buffer> <leader>1 <esc>0d^Yp:s/./=/g<cr>
inoremap <buffer> <leader>2 <esc>0d^Yp:s/./-/g<cr>k>j
inoremap <buffer> <leader>3 <esc>0d^Yp:s/./~/g<cr>k2>j

regards