最近折騰 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配置