Skip to content

Instantly share code, notes, and snippets.

@madx
Last active January 3, 2016 11:49
Show Gist options
  • Save madx/8458952 to your computer and use it in GitHub Desktop.
Save madx/8458952 to your computer and use it in GitHub Desktop.
Simple window management for Vim

About

This little plugin eases working with windows in Vim. Put it in your plugins dir.

It provides 4 functions:

  • WMFocusMasterWindow: focuses the topmost-leftmost window
  • WMMarkWindowSwap: marks the current window for a later swap
  • WMDoWindowSwap: exchanges current window with marked window
  • WMSwapWithMasterWindow: exchanges current window with master window
  • WMReStack: rearranges all the windows in a tiled layout with the current one as master

You can map them as indicated in the comments at the end of the file.

Example use

Arrows indicate current window

+---------+---------+
|         |         |
|         |    B    |
|         |         |
|  > A <  +---------+
|         |         |
|         |    C    |
|         |         |
+---------+---------+

:WMMarkWindowSwap
<Ctrl-w> l

+---------+---------+
|         |         |
|         |  > B <  |
|         |         |
|    A    +---------+
|         |         |
|         |    C    |
|         |         |
+---------+---------+

:WMDoWindowSwap

+---------+---------+
|         |         |
|         |  > A <  |
|         |         |
|    B    +---------+
|         |         |
|         |    C    |
|         |         |
+---------+---------+

<Ctrl-w> j

+---------+---------+
|         |         |
|         |    A    |
|         |         |
|    B    +---------+
|         |         |
|         |  > C <  |
|         |         |
+---------+---------+

:WMSwapWithMasterWindow

+---------+---------+
|         |         |
|         |    A    |
|         |         |
|  > C <  +---------+
|         |         |
|         |    B    |
|         |         |
+---------+---------+

An example of WMReStack:

+---------+---------+
|         |    C    |
|    A    |---------|
|         |  > D <  |
|---------+---------+
|         |         |
|    B    |    E    |
|         |         |
+---------+---------+

:WMReStack

+---------+---------+
|         |    A    |
|         |---------|
|         |    B    |
|  > D <  +---------+
|         |    C    |
|         |---------|
|         |    E    |
+---------+---------+


if exists("wm_loaded")
finish
endif
let wm_loaded = 1
function! WMFocusMasterWindow()
exe "1wincmd w"
endfunction
function! WMSwapWithMasterWindow()
call WMMarkWindowSwap()
call WMFocusMasterWindow()
call WMDoWindowSwap()
endfunction
function! WMMarkWindowSwap()
let g:markedWinNum = winnr()
endfunction
function! WMDoWindowSwap()
let curNum = winnr()
let curBuf = bufnr( "%" )
exe g:markedWinNum . "wincmd w"
let markedBuf = bufnr( "%" )
exe 'hide buf' curBuf
exe curNum . "wincmd w"
exe 'hide buf' markedBuf
endfunction
function! WMReStack()
let curBuf = bufnr( "%" )
exe "windo wincmd K"
let bufWinNum = bufwinnr( curBuf )
exe bufWinNum . "wincmd w"
exe "wincmd H"
endfunction
" Example use
" nmap <silent> <leader>w<Space> :call WMFocusMasterWindow()<CR>
" nmap <silent> <leader>w<CR> :call WMSwapWithMasterWindow()<CR>
" nmap <silent> <leader>wm :call WMMarkWindowSwap()<CR>
" nmap <silent> <leader>wx :call WMDoWindowSwap()<CR>
" nmap <silent> <leader>wt :call WMReStack()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment