Vim Tips Wiki
Tip 1110 Printable Monobook Previous Next

created January 17, 2006 · complexity basic · author Jang Junyeong · version 6.0

You can easily switch between the maximized and original window size using the following script. Unfortunately, this seems to work only in Win32 gvim.

F11: maximize/restore height

Shift-F11: maximize/restore width

Ctrl-F11: maximize/restore height and width

function ToggleWindowSize(act)
  if a:act < 0 || a:act > 2 | return | endif
  let posX = getwinposx()
  let posY = getwinposy()
  let actTab = "XXX__X_XR__XX_X__RRRR__R"
  let idx = ((exists("g:twsWM") + exists("g:twsHM") * 2) * 3 + a:act) * 2
  let actW = strpart(actTab, idx, 1)
  let actH = strpart(actTab, idx + 1, 1)
  " note. g:tws + [Width,Height,X,Y] + [Maximized,Saved]
  if actW == "X"
    let g:twsWS = &columns | let g:twsXS = posX
    set columns=999
    let posX = getwinposx()
    let g:twsWM = &columns | let g:twsXM = posX
  elseif actW == "R"
    if g:twsWM == &columns
      let &columns = g:twsWS
      if g:twsXM == posX | let posX = g:twsXS | endif
    unlet g:twsWM g:twsWS g:twsXM g:twsXS
  if actH == "X"
    let g:twsHS = &lines | let g:twsYS = posY
    set lines=999
    let posY = getwinposy()
    let g:twsHM = &lines | let g:twsYM = posY
  elseif actH == "R"
    if g:twsHM == &lines
      let &lines = g:twsHS
      if g:twsYM == posY | let posY = g:twsYS | endif
    unlet g:twsHM g:twsHS g:twsYM g:twsYS
  execute "winpos " . posX . " " . posY
nnoremap <F11> :call ToggleWindowSize(2)<CR>
nnoremap <S-F11> :call ToggleWindowSize(1)<CR>
nnoremap <C-F11> :call ToggleWindowSize(0)<CR>
imap <F11> <C-O><F11>
imap <S-F11> <C-O><S-F11>
imap <C-F11> <C-O><C-F11>


The problem with something like lines=999 or columns=999 is that on some platforms (many Unix systems), the window is allowed to be arbitrarily large: it just won't fit on-screen and you won't be able to access it. In other words, it doesn't just assume that 999 means 'the largest size that will fit'.

On Windows, 999 makes the window as big as it can get, true, but it doesn't actually use the system maximize facility, forcing you to remember its previous size (the 'restore' size). A true maximize has the advantage that you don't need to remember the previous size: the operating system takes care of remembering the restoration point.

This points out the problems, but doesn't help outright, I realize. On Windows, the simalt mechanism seems to work for me. I use it to immediately maximize (true maximize) my GVim as soon as I start (I almost never work with an un-maximized window in most applications I use).

On RedHat Linux, I find it inadvisable to set lines/columns=999.

  • To maximize window: Alt-F10
  • To restore window: Alt-F5
  • To maximize height within window size: Ctrl-w, Ctrl-_
  • To maximize width within window size: Ctrl-w, Ctrl-|
  • Resize window height to 45 lines: z45<Enter>

For more controls, see :help window-resize

See VimTip494.

I use the following mappings to change the size of my window (I believe these may only work on the Win32 version, see the help on simalt):

" maximize window size and make all split windows the same size
map <unique> <Leader>mm <Esc>:simalt ~x<CR><C-W>=
" maximize the window size
map <unique> <Leader>mx <Esc>:simalt ~x<CR>
" minimize the window size
map <unique> <Leader>mn <Esc>:simalt ~n<CR>
" restore the window size
map <unique> <Leader>mr <Esc>:simalt ~r<CR>

On Windows, you can maximize by pressing Alt+Space then x.

However, it doesn't work when using UTF-8 as the character encoding.

Yes, on Win XP, when Vim 7.2 is in utf-8 encoding, then Alt+Space does not work. In neither Esc mode nor writing mode one can access the window control menu (Maximize, Restore, Minimize, Move, Size and Close) of gvim with Alt+Space when in utf-8 encoding. My solution was to eliminate all Alt+__ bindings in _vimrc:

se winaltkeys=yes

TaipIrNe 23:52, 10 November 2008 (UTC)

There is a small trick when in Win XP: just press and RELEASE the Alt key, THEN press Space. It works also with utf-8