Using vim to create a numbered list, e.g. list of IP addresses

Print Friendly, PDF & Email

If you need to create a list of IP addresses in VIM (for a /24 mask), you can use the following keystrokes. You could obviously use excel or something else, but this is useful when working on a text based environment, like a console, and you need some lists to play with, perhaps for a script.

Syntax:

1.   Create the first list entry, make sure it starts with a number.
2.   qa        - start recording into register 'a'
3.   Y         - yank the entry (capital ‘Y’)
4.   p         - put a copy of the entry below the first one
5.   CTRL-A     - increment the number
6.   q         - stop recording
7.   <count>@a - repeat the yank, put and increment <count> times

Example:

1.   Create the first list entry, make sure it starts with a number.
2.   qa        - start recording into register 'a'
3.   Y         - yank the entry (capital ‘Y’)
4.   p         - put a copy of the entry below the first one
5.   CTRL-A     - increment the number
6.   q         - stop recording
7.   253@a      - repeat the yank 253 times – remember, 1 and 2 should already be there
8.   :1,255s/^/10.1.1./g – This will insert “10.1.1.” at the start of all lines from line 1 to line 255, globally

Note: This only works in ‘vim’, not ‘vi’. ‘vi’ does not have support for macros/recording.

It’s ‘hacky’…but it works.

Enjoy.

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.