1 "=============================================================================
2 " File: close_another_window.vim
3 " Author: Samir Benmendil <me [at] rmz [dot] io>
4 " Contributor: Dmitry Frank (dimon.frank@gmail.com)
6 "=============================================================================
7 " See documentation in accompanying help file
8 " You may use this code in whatever way you see fit.
18 function! s:inputtarget()
30 function! <SID>CloseWindow(boolBang)
38 "" function to close other window
39 " @param sWincmd - window movement command(s).
40 " for example, "h" used to close left window,
41 " "j" used to close lower window,
44 " You can combine them:
45 " "jl" used to go to lower window,
46 " then go to right window,
49 function! <SID>CloseAnotherWindow(sWincmd, boolBang)
52 call <SID>CloseWindow(a:boolBang)
56 " remember current winnr, e.g. winnr of starting window
57 " (we need to go to the same winnr after close other window)
58 let l:iCurWinnr = winnr()
62 while (l:iIndex < strlen(a:sWincmd))
63 " move to window that should be closed and get its winnr
64 exec 'wincmd '.a:sWincmd[l:iIndex]
65 let l:iClosedWinnr = winnr()
70 " if its winnr is the same as starting winnr, then do nothing.
71 " (you should use just :q instead)
72 if (l:iCurWinnr == l:iClosedWinnr)
76 " calculate target winnr
77 if (l:iCurWinnr > l:iClosedWinnr)
82 call <SID>CloseWindow(a:boolBang)
84 " go to starting window
85 while (winnr() != l:iCurWinnr)
90 " define plugin's commands
91 command! -nargs=? -bang -complete=file CloseLeftWindow call <SID>CloseAnotherWindow('h', <bang>0)
92 command! -nargs=? -bang -complete=file CloseBelowWindow call <SID>CloseAnotherWindow('j', <bang>0)
93 command! -nargs=? -bang -complete=file CloseAboveWindow call <SID>CloseAnotherWindow('k', <bang>0)
94 command! -nargs=? -bang -complete=file CloseRightWindow call <SID>CloseAnotherWindow('l', <bang>0)
96 command! -nargs=? -bang -complete=file CloseAnotherWindow call <SID>CloseAnotherWindow("<args>", <bang>0)
98 nnoremap <silent> <Plug>CloseAnotherWindow :<C-U>call <SID>CloseAnotherWindow(<SID>inputtarget(), 0)<CR>