Educating yourself does not mean that you were stupid in the first place; it means that you are intelligent enough to know that there is plenty left to 'learn'. -Melanie Joy

Monday, 4 June 2012

Creating A List Using vim

June 04, 2012 Posted by Dinesh No comments
Some time you may need to insert a list of ascending numbers in a file. We can do this using vim very easily.

Method 1 :

:put =range(11,15)


This will create a series of increasing numbers after the current line including 11 and 15.
output will be-     

11
12
13
14
15
 
Method 2 :
Some times if you want to add a constant predefined string before the number series, then you you can go using loop to create a list.
       
for i in range(1,6) | put ='10.168.0.'.i | endfor

Executing the command will insert the following lines after the current line

10.168.0.1
10.168.0.2
10.168.0.3
10.168.0.4
10.168.0.5
10.168.0.6
   
Method 3 : 
Another simple way is to use Ctrl+A in a macro.
for example, some times you may need to initialize a array of size 100.
array[0] = 0
array[1] = 0
array[2] = 0
... so on

type array[0] = 0; then start recording macro.
Type the following commands without pressing enter.

qa          [ record to buffer 'a' ]
Y           [ copy the current line ] 
p           [ paste the line ]
Ctrl-A      [ Ctrl+A increment the number or use Ctrl+X to decrement]
q           [ stop recording macro ] 

now type 100@a to perform the macro 100 times.

array[0] = 0;
array[1] = 0;
array[2] = 0;
array[3] = 0;
array[4] = 0;
... and so on

if you want to increase the index and assigned value also then type Ctrl+A by moving the courser to the value.

0 comments:

Post a Comment