A number generator in built in vi

2 views
Skip to first unread message

parag...@hotmail.com

unread,
Oct 10, 2008, 7:22:59 AM10/10/08
to vim_use
Does vi has some quick way to generate lets say 1-100
in 100 lines

A. S. Budden

unread,
Oct 10, 2008, 8:31:22 AM10/10/08
to vim...@googlegroups.com
2008/10/10 parag...@hotmail.com <parag...@hotmail.com>:

> Does vi has some quick way to generate lets say 1-100
> in 100 lines

I'm sure others will reply with functions etc to do this, but I tend to go for:

* Create a new file
* Enter '1' on the first line
* 'yy' (copy first line)
* '99p' (paste 99 times)
* Ctrl-V (visual block mode)
* G$ (select everything)
* :I (visual increment using Dr Chip's visincr.vim plugin [1])

Alternatively, just use perl:

perl -e 'for($i=1;$i<=100;$i++) { print $i . "\n"; }' > filename.txt

[1] http://www.vim.org/scripts/script.php?script_id=670

Tim Chase

unread,
Oct 10, 2008, 8:42:05 AM10/10/08
to vim...@googlegroups.com
> I'm sure others will reply with functions etc to do this, but I tend to go for:

Of course...this is Vim so there regularly a multitude of ways to
accomplish a goal :)


> * Create a new file
> * Enter '1' on the first line
> * 'yy' (copy first line)
> * '99p' (paste 99 times)
> * Ctrl-V (visual block mode)
> * G$ (select everything)
> * :I (visual increment using Dr Chip's visincr.vim plugin [1])

I case you don't want to bother with plugins (I like to keep my
vim installs pretty native so I don't start leaning on things
that aren't baked-in across all the umpteen machines I use), you
can use

yy (yank the initial empty/blank line)
99p (paste it 99 more times to get places to put line#s)
:%s/^/\=line('.') (to put the current line# on each line)


> Alternatively, just use perl:
>
> perl -e 'for($i=1;$i<=100;$i++) { print $i . "\n"; }' > filename.txt


Or most *nix boxes have "seq" available:

:0r! seq 100
and optionally ":$d"/"Gdd" to delete the last/blank line

:)

-tim


Andy Wokula

unread,
Oct 10, 2008, 9:19:29 AM10/10/08
to vim...@googlegroups.com
parag...@hotmail.com schrieb:

> Does vi has some quick way to generate lets say 1-100
> in 100 lines

In Vim7 you'd say for example
:put = range(1,100)

but in Vi ... are you sure you're using Vi and not Vim?
:ver

--
Andy


Erik Falor

unread,
Oct 10, 2008, 11:08:05 AM10/10/08
to vim...@googlegroups.com
On Fri, Oct 10, 2008 at 07:42:05AM -0500, Tim Chase wrote:
>
> > I'm sure others will reply with functions etc to do this, but I tend to go for:
>
> Of course...this is Vim so there regularly a multitude of ways to
> accomplish a goal :)

Indeed!

>
> > * Create a new file
> > * Enter '1' on the first line
> > * 'yy' (copy first line)
> > * '99p' (paste 99 times)
> > * Ctrl-V (visual block mode)
> > * G$ (select everything)
> > * :I (visual increment using Dr Chip's visincr.vim plugin [1])
>
> I case you don't want to bother with plugins (I like to keep my
> vim installs pretty native so I don't start leaning on things
> that aren't baked-in across all the umpteen machines I use), you
> can use
>
> yy (yank the initial empty/blank line)
> 99p (paste it 99 more times to get places to put line#s)
> :%s/^/\=line('.') (to put the current line# on each line)
>
>
> > Alternatively, just use perl:
> >
> > perl -e 'for($i=1;$i<=100;$i++) { print $i . "\n"; }' > filename.txt
>
>
> Or most *nix boxes have "seq" available:
>
> :0r! seq 100
> and optionally ":$d"/"Gdd" to delete the last/blank line
>
> :)
>
> -tim
>
>
>
> >

A pure Vimscript method that will work on boxes without Perl or seq
(I'm talking about you, WinXP):

:for i in range(1, 100) | exe "put =" i | endfor


--
Erik Falor
Registered Linux User #445632 http://counter.li.org

toomln

unread,
Oct 10, 2008, 1:46:42 PM10/10/08
to vim_use
Another approach:

:call append(0, range(1, 100))<cr>

Dave Land

unread,
Oct 10, 2008, 2:20:09 PM10/10/08
to vim...@googlegroups.com
Yet another approach:

Insert a new line that contains the number 1.
qq (start recording)
yy (yank the line)
p (put the yanked line)
^A (increment the number under the cursor)
q (stop recording)

You will now have a file with 2 lines in it: 1 and 2.

Now the fun begins:

98@q (repeat the recorded macro [yank, put, increment] 98 times)

Dave

Kevin Dillman

unread,
Oct 10, 2008, 10:16:51 AM10/10/08
to vim...@googlegroups.com
If you have macro capability:
Insert 1 on the first line.
qf       to begin recording a macro in register f.
yyp    to copy and past the same number on the line below.
C-a    to increment the second number.
q       to stop recording
98@f to run the macro in register f 98 more times for a total of 100.
 
Kevin

Reply all
Reply to author
Forward
0 new messages