views:

176

answers:

1

I can such code in my .vimrc file:

imap <D-1> <ESC>1gt
vmap <D-1> 1gt
nmap <D-1> 1gt

imap <D-2> <ESC>2gt
vmap <D-2> 2gt
nmap <D-2> 2gt

imap <D-3> <ESC>3gt
vmap <D-3> 3gt
nmap <D-3> 3gt

imap <D-4> <ESC>4gt
vmap <D-4> 4gt
nmap <D-4> 4gt

imap <D-5> <ESC>5gt
vmap <D-5> 5gt
nmap <D-5> 5gt

imap <D-6> <ESC>6gt
vmap <D-6> 6gt
nmap <D-6> 6gt

imap <D-7> <ESC>7gt
vmap <D-7> 7gt
nmap <D-7> 7gt

imap <D-8> <ESC>8gt
vmap <D-8> 8gt
nmap <D-8> 8gt

imap <D-9> <ESC>9gt
vmap <D-9> 9gt
nmap <D-9> 9gt

How I can refactor this code?

+2  A: 
let i=1
while i<=9
    execute "nmap <D-".i."> ".i."gt"
    execute "vmap <D-".i."> ".i."gt"
    execute "imap <D-".i."> <ESC>".i."gt"
    let i+=1
endwhile
ZyX
better to use `for item in range(1, 9) | ... | endfor`
Nikolay Frantsev
If I am not mistaking, range generates list and it consumes memory while you do not need any lists. I never use range if I do not need a list as it is a bad habit.
ZyX