Vim窗口布局保存插件

672 查看

Vim 的分屏很好用,可以开多个窗口对照代码,但是分的屏多了,需要临时扩大某个窗口到最大,编辑代码,使用完成之后,又想要恢复原先已经设置好的布局,Vim本身没有提供这样的功能,但是提供了实现这种功能的基础, 下面的代码就实现了这样的功能. 将下面的代码保存为vimlayout.vim放到 Vim的plugin目录下, 设置自己喜欢的绑定键就可以了工作了. 此代码在 Vim 7.3中测试通过.

if exists("g:vimlayoutloaded")
    finish
else
    let g:vimlayoutloaded=1
endif

function! HeightToSize(height)
    let currwinno=winnr()
        if winheight(currwinno)>a:height
            while winheight(currwinno)>a:height
                execute "normal \<c-w>-"
            endwhile
        elseif winheight(currwinno)<a:height
            while winheight(currwinno)<a:height
                execute "normal \<c-w>+"
            endwhile
        endif
endfunction

function! WidthToSize(width)
       let currwinno=winnr()
        if winwidth(currwinno)>a:width
            while winwidth(currwinno)>a:width
                execute "normal \<c-w><"
            endwhile
        elseif winwidth(currwinno)<a:width
            while winwidth(currwinno)<a:width
                execute "normal \<c-w>>"
            endwhile
        endif
endfunction


function! TweakWinSize(orgisize)
    call HeightToSize(a:orgisize[0])
    call WidthToSize(a:orgisize[1])
endfunction

function! RestoreWinLayout()
    if exists("g:layout")
        let winno=1
        let orgiwinno=winnr()
        for win in g:layout
            execute "normal \<c-w>w"
            let currwinno=winnr()
            if currwinno!=1 && currwinno!=orgiwinno
                call TweakWinSize(g:layout[currwinno-1])
            endif
        endfor
        unlet g:layout
    endif
endfunction

function! SaveWinLayout()
    let wnumber=winnr("$")
    let winlist=range(wnumber)
    let winno=0
    let layout=[]
    for index in winlist
        let winno+=1
        let wininfo=[winheight(winno),winwidth(winno)]
        call add(layout,wininfo)
    endfor
    let g:layout=layout
endfunction

function! ToggleMaxWin()
    if exists("g:layout")
        if winnr("$")==len(g:layout)
            call RestoreWinLayout()
        else
            call SaveWinLayout()
            execute "normal 200\<c-w>>"
            execute "normal \<c-w>_"
            call RestoreWinLayout()
        endif
    else
        call SaveWinLayout()
        execute "normal 200\<c-w>>"
        execute "normal \<c-w>_"
    endif
endfunction