shixin
shixin

Reputation: 81

How to insert with increasing numbers of line

This is a small part of the file, the original file has hundreds of lines.

Line 1 below insert the new line Numbers 1, 2, 3 lines below the new line number 2, 3, and so on, are as follows.

How to use gVim command and function to achieve the change from above to below. I'm using gVim of windows platform.

Before the modification

hsd
xlfh
1lm
2lm
3lm
4lm
5lm
6lm
7lm
8lm
9lm
slm
s1lm
......

After the modification

hsd
1
xlfh
1lm
2
3
2lm
3lm
4lm
4
5
6
5lm
6lm
7lm
8lm
7
8
9
10
9lm
slm
s1lm
......

Upvotes: 0

Views: 174

Answers (2)

cforbish
cforbish

Reputation: 8819

I do not know why you would need to do this, but if you need this often, consider a function inside .vimrc:

function! Nameme()
    1
    let mov = 1
    let start = 1
    let inc = 0
    while 1
        call append(line('.'), range(start, start+inc))
        let mov += 2
        let inc += 1
        let start += inc
        if mov + line('.') > line('$')
            break
        endif
        execute mov + line('.')
    endwhile
endfunction

Then you could just :call Nameme().

Upvotes: 1

user2987828
user2987828

Reputation: 1137

It is possible in Vim, merging another file containing the numbers to insert (created by /bin/cat -n for example), here is the beginning.

gg         go to first line
o1<esc>"ayy   a (will increase) initialized to 1
"byy        b (will be cumsum of a) initialized to 1
qz          loop this:
:.,<c-r>a   advance by a lines
"bp         put a(a-1)

... but I will only continue if you format your question so that it does not end up deleted

Upvotes: 0

Related Questions