Vim: Increment numbers in Vim

This is a discussion on Vim: Increment numbers in Vim within the Editors forums in Theory and Concepts category; Hi, I was wondering if it is possible to increment with VIM. For example I'm trying to ping an IP range that needs to be contained in a file. So I would like to create say 254 lines such as: 192.168.0.1 192.168.0.2 .... 192.168.0.254 ,without having the manually increment them yourself?...

Go Back   Application Development Forum > Theory and Concepts > Editors

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-04-2008, 11:07 AM
Andrew Woods
Guest
 
Default Vim: Increment numbers in Vim

Hi,

I was wondering if it is possible to increment with VIM. For example
I'm trying to ping an IP range that needs to be contained in a file.
So I would like to create say 254 lines such as:

192.168.0.1
192.168.0.2
....
192.168.0.254

,without having the manually increment them yourself?
Reply With Quote
  #2  
Old 08-04-2008, 11:21 AM
Stephane CHAZELAS
Guest
 
Default Re: Vim: Increment numbers in Vim

2008-08-4, 08:07(-07), Andrew Woods:
> I was wondering if it is possible to increment with VIM. For example
> I'm trying to ping an IP range that needs to be contained in a file.
> So I would like to create say 254 lines such as:
>
> 192.168.0.1
> 192.168.0.2
> ...
> 192.168.0.254
>
> ,without having the manually increment them yourself?



You can use <Ctrl-A> in command mode.

So if you have a line with

192.168.0.1

You can record a macro ("a") like:

qayyp$<Ctrl-A>q

And then:

252@a

to call the macro 252 times.

On GNU systems, you can also do:

:r! seq -f 192.168.1.\%g 254

Or if your shell is zsh:

:r! print -l 192.168.1.{1..254}

You can probably do some loops in vim language, but I don't do
that often enough for me to remember the syntax, so I'd need to
lookup the manual.

--
Stéphane
Reply With Quote
  #3  
Old 08-04-2008, 11:34 AM
Andrew Woods
Guest
 
Default Re: Vim: Increment numbers in Vim

On Aug 4, 5:21*pm, Stephane CHAZELAS <stephane_chaze...@yahoo.fr>
wrote:
> 2008-08-4, 08:07(-07), Andrew Woods:
>
> > I was wondering if it is possible to increment with VIM. For example
> > I'm trying to ping an IP range that needs to be contained in a file.
> > So I would like to create say 254 lines such as:

>
> > 192.168.0.1
> > 192.168.0.2
> > ...
> > 192.168.0.254

>
> > ,without having the manually increment them yourself?

>
> You can use <Ctrl-A> in command mode.
>
> So if you have a line with
>
> 192.168.0.1
>
> You can record a macro ("a") like:
>
> qayyp$<Ctrl-A>q
>
> And then:
>
> 252@a
>
> to call the macro 252 times.
>
> On GNU systems, you can also do:
>
> :r! seq -f 192.168.1.\%g 254
>
> Or if your shell is zsh:
>
> :r! print -l 192.168.1.{1..254}
>
> You can probably do some loops in vim language, but I don't do
> that often enough for me to remember the syntax, so I'd need to
> lookup the manual.
>
> --
> Stéphane


Hi Stéphane,

Great thanks, your macro idea was very useful, I must start trying to
use them more often.

Drew
Reply With Quote
  #4  
Old 08-04-2008, 11:35 AM
Teemu Likonen
Guest
 
Default Re: Vim: Increment numbers in Vim

Stephane CHAZELAS wrote:

> 2008-08-4, 08:07(-07), Andrew Woods:
>> I was wondering if it is possible to increment with VIM. For example
>> I'm trying to ping an IP range that needs to be contained in a file.
>> So I would like to create say 254 lines such as:
>>
>> 192.168.0.1
>> 192.168.0.2
>> ...
>> 192.168.0.254


> You can probably do some loops in vim language, but I don't do
> that often enough for me to remember the syntax, so I'd need to
> lookup the manual.


With Vim's script language one can do

:for i in range(254) | execute 'normal o192.168.0.'.(i+1) | endfor

for example.
Reply With Quote
  #5  
Old 08-04-2008, 12:32 PM
Philippe ROUDOT,TEMICS
Guest
 
Default Re: Vim: Increment numbers in Vim

hi ,
it's a little a bit dirty but it works :
you need two marvellous features of Vim.

- you can record sequences of instructions with the 'q' command
(for exemple 'qa' will record the following sequence in the register 'a',
type 'q' again to close the sequence)
- C-a increment numbers !

so you type your first number
i 125.200.666.0
then you register the following sequence
qa yy p $ C-a q

Hit @a to execute your sequence and @@ to reexecute the last sequence.
50@a will do it 50 times.

I'm sure there is a clean way to put it into your vimrc or something
but I'm too lasy for searching .

Reply With Quote
  #6  
Old 08-04-2008, 12:35 PM
Philippe ROUDOT,TEMICS
Guest
 
Default Re: Vim: Increment numbers in Vim

I guess i've been too slow ^^.
I find Teemu's solution much better than mine.
Reply With Quote
  #7  
Old 08-04-2008, 04:40 PM
J O'Connell
Guest
 
Default Re: Vim: Increment numbers in Vim

Teemu Likonen wrote:
>
> With Vim's script language one can do
>
> :for i in range(254) | execute 'normal o192.168.0.'.(i+1) | endfor
>
> for example.


To The List:

How may we nominate this as an example of brevity, simplicity, and
elegance?

could it be placed in the official Vim FAQ or one of the Vim
'Tips-n-Tricks' sites?

-Fionn
Reply With Quote
  #8  
Old 08-04-2008, 05:42 PM
Teemu Likonen
Guest
 
Default Re: Vim: Increment numbers in Vim

J O'Connell wrote:

> Teemu Likonen wrote:
>>
>> With Vim's script language one can do
>>
>> :for i in range(254) | execute 'normal o192.168.0.'.(i+1) | endfor
>>
>> for example.


> How may we nominate this as an example of brevity, simplicity, and
> elegance?


Hmm, let's make it even more elegant and simple:

:for i in range(1,254) | execute 'normal o192.168.0.'.i | endfor
Reply With Quote
  #9  
Old 08-05-2008, 09:26 PM
canoe
Guest
 
Default Re: Vim: Increment numbers in Vim

Andrew Woods wrote:
> Hi,
>
> I was wondering if it is possible to increment with VIM. For example
> I'm trying to ping an IP range that needs to be contained in a file.
> So I would like to create say 254 lines such as:
>
> 192.168.0.1
> 192.168.0.2
> ...
> 192.168.0.254
>
> ,without having the manually increment them yourself?


The visincr plugin makes this pretty simple:
192.168.0.1
ma"ay'a (get a copy of the line into register a)
253"ap (make 254 total copies)

Now, use visual-mode (go to the rightmost 1, ctrl-v, G).
Using visincr: I

That'll make the visually-selected region an incremented list.

You can get visincr from:

http://mysite.verizon.net/astronaut/...x.html#VISINCR (cutting edge)
http://vim.sourceforge.net/scripts/s...?script_id=670 (stable)

Visincr also supports hexadecimal, octal, roman, day, and various date incrementing.

Regards,
Chip Campbell
Reply With Quote
  #10  
Old 08-05-2008, 10:17 PM
Chris Burkhardt
Guest
 
Default Re: Vim: Increment numbers in Vim

J O'Connell wrote:
> To The List:
>
> How may we nominate this as an example of brevity, simplicity, and
> elegance?
>
> could it be placed in the official Vim FAQ or one of the Vim
> 'Tips-n-Tricks' sites?


Anybody may add tips to the Vim tips wiki: http://vim.wikia.com/wiki/Main_Page

- Chris
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 07:12 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.