最近折腾 vim, 除了配置巨麻烦外, 另外一个很蛋疼的就是窗口位置问题了, 折腾了半天没法启动时自动居中, 找遍各地也只有保存上次位置, 下次启动时恢复的方法 废话很少说, 直接上代码, 丢到 vimrc 中便可:vim
function WindowCenterInScreen() set lines=9999 columns=9999 let g:windowsSizeFixX = 58 let g:windowsSizeFixY = 118 let g:windowsScaleX = 7.75 let g:windowsScaleY = 17.0 let g:windowsPosOldX = getwinposx() let g:windowsPosOldY = getwinposy() let g:windowsScreenWidth = float2nr(winwidth(0) * g:windowsScaleX) + g:windowsPosOldX + g:windowsSizeFixX let g:windowsScreenHeight = float2nr(winheight(0) * g:windowsScaleY) + g:windowsPosOldY + g:windowsSizeFixY set lines=30 columns=108 let g:windowsSizeWidth = float2nr(winwidth(0) * g:windowsScaleX) + g:windowsSizeFixX let g:windowsSizeHeight = float2nr(winheight(0) * g:windowsScaleY) + g:windowsSizeFixY let g:windowsPosX = ((g:windowsScreenWidth - g:windowsSizeWidth) / 2) let g:windowsPosY = ((g:windowsScreenHeight - g:windowsSizeHeight) / 2) exec ':winpos ' . g:windowsPosX . ' ' . g:windowsPosY endfunc au GUIEnter * call WindowCenterInScreen()
几个 magic number 的解释:windows
windowsSizeFixX/Y
bash
系统窗口边框的大小, 像素为单位code
windowsScaleX/Y
blog
单个字符的平均宽高, 像素为单位get
能够把窗口最大化, 计算一下总像素和总字符数, 除法一下就行了io
在相同 DPI 的屏幕下, 不管什么分辨率都能自动居中function
不一样 DPI 的屏幕下, 需从新计算那几个 magic numberclass
来源:http://zsaber.com/blog/p/14配置