How do I control windows using Ex commands in scripts and mappings instead of Ctrl-W shortcuts?
Answer
:wincmd {cmd}
Explanation
:wincmd {key} is the Ex command equivalent of every <C-w>{key} window shortcut. It lets you perform any window operation — split, move focus, resize, rotate — from a place where keybindings cannot be sent directly: Vimscript functions, autocmds, and command-line mappings.
How it works
:wincmd takes exactly one argument: the key you would press after <C-w>. Any <C-w>{key} combination maps 1:1 to :wincmd {key}:
:wincmd h " move focus to the left window (<C-w>h)
:wincmd v " open a vertical split (<C-w>v)
:wincmd = " equalize all window sizes (<C-w>=)
:wincmd K " move window to top (<C-w>K)
:wincmd z " close the preview window (<C-w>z)
Example
In a Vimscript function, you can't easily send <C-w> keycodes. Use :wincmd instead:
function! FocusQuickfix()
copen
wincmd J " push the quickfix window to the bottom
wincmd p " jump back to the previous window
endfunction
In an autocmd — equalize splits whenever a window is resized:
autocmd VimResized * wincmd =
In a mapping — avoids the <C-w> prefix in the RHS:
nnoremap <silent> <leader>= :wincmd =<CR>
Tips
:h wincmdlists all accepted keysexecute 'wincmd ' . key_variablelets you build the command dynamically{count}wincmd {key}passes a count, e.g.:5wincmd >widens the window by 5 columns