vimtricks.wiki Concise Vim tricks, one at a time.

How do I navigate between windows from a mapping or script using an Ex command?

Answer

:wincmd {arg}

Explanation

:wincmd {arg} executes a window command from the Ex command line — equivalent to pressing <C-w>{arg} in Normal mode. This is essential in mappings (where <C-w> may conflict), scripts, autocommands, and any context where you need to issue window commands programmatically.

How it works

:wincmd j    " same as <C-w>j (move to window below)
:wincmd v    " same as <C-w>v (vertical split)
:wincmd =    " same as <C-w>= (equalize sizes)

{arg} accepts the same characters as <C-w>: any window navigation, resizing, or management key.

Common uses

Command Action
:wincmd h/j/k/l Move to window left/down/up/right
:wincmd w Cycle to next window
:wincmd p Jump to previous window
:wincmd = Equalize all window sizes
:wincmd _ Maximize current window height
:wincmd | Maximize current window width
:wincmd o Close all other windows
:wincmd n Open a new window

In mappings

" Navigate with Alt+arrow keys (no <C-w> conflict)
nnoremap <A-h> :wincmd h<CR>
nnoremap <A-j> :wincmd j<CR>
nnoremap <A-k> :wincmd k<CR>
nnoremap <A-l> :wincmd l<CR>

In autocommands

" Auto-equalize windows when Vim is resized
autocmd VimResized * wincmd =

Tips

  • :wincmd accepts counts: :3wincmd w cycles forward 3 windows
  • The abbreviated form is :winc: :winc j works the same
  • In Insert mode, use <C-o>:wincmd j<CR> to temporarily go to Normal, execute, and return
  • :help CTRL-W lists every possible argument

Next

How do I run a search and replace only within a visually selected region?