aboutsummaryrefslogtreecommitdiff
path: root/entries/joeyshi12/vimscript/fib.vim
blob: b385049a3efaaf4a637ed055eb63bc7e21f6f41d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
" Add me to your .vimrc :]

function! Fib(n)
    if a:n == 0
        return "0"
    elseif a:n == 1
        return "0,1"
    endif
    let sequence = "0,1"
    let prev = 0
    let curr = 1
    let iter = 2
    while iter <= a:n
        let curr = prev + curr
        let prev = curr - prev
        let sequence .= "," . curr
        let iter += 1
    endwhile
    return sequence
endfunction

function! AppendFib(n)
    execute "normal! i" . Fib(a:n) . "\<Esc>"
endfunction

" Map AppendFib to \fib
:command! -nargs=1 AppendFib :call AppendFib(<q-args>)
nnoremap <Leader>fib :AppendFib<Space>