VIM分屏布局保存插件

vim的分屏很好用,可以开多个窗口对照代码,但是分的屏多了,需要临时扩大某个窗口到最大,编辑代码,使用完成之后,又想要恢复原先已经设置好的布 局,vim本身没有提供这样的功能,但是提供了实现这种功能的基础,一时在网上没有找到合适的,所以即兴编写了这个vim的分屏布局保存插件

在vim中配置快捷键 : map <silent> <A-Z> :call ToggleMaxWin()<cr>

"
" save current window layout
" can restore layout when press some key
" author:alex8224@gmail.com
" 2012-04-27
"

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

转载于:https://my.oschina.net/u/157777/blog/64416

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值