"============================================================================= " File: close_another_window.vim " Author: Samir Benmendil " Contributor: Dmitry Frank (dimon.frank@gmail.com) " Version: 1.04 "============================================================================= " See documentation in accompanying help file " You may use this code in whatever way you see fit. function! s:getchar() let c = getchar() if c =~ '^\d\+$' let c = nr2char(c) endif return c endfunction function! s:inputtarget() let a = s:getchar() let c = '' while a != ' ' if a =~ '^[hjkl]$' let c = c . a endif let a = s:getchar() endwhile return c endfunction function! CloseWindow(boolBang) if (!a:boolBang) :close else :close! endif endfunction "" function to close other window " @param sWincmd - window movement command(s). " for example, "h" used to close left window, " "j" used to close lower window, " etc. " " You can combine them: " "jl" used to go to lower window, " then go to right window, " then close it. " etc. function! CloseAnotherWindow(sWincmd, boolBang) if empty(a:sWincmd) call CloseWindow(a:boolBang) return endif " remember current winnr, e.g. winnr of starting window " (we need to go to the same winnr after close other window) let l:iCurWinnr = winnr() let l:iIndex = 0 while (l:iIndex < strlen(a:sWincmd)) " move to window that should be closed and get its winnr exec 'wincmd '.a:sWincmd[l:iIndex] let l:iClosedWinnr = winnr() let l:iIndex += 1 endwhile " if its winnr is the same as starting winnr, then do nothing. " (you should use just :q instead) if (l:iCurWinnr == l:iClosedWinnr) return endif " calculate target winnr if (l:iCurWinnr > l:iClosedWinnr) let l:iCurWinnr -= 1 endif " close needed window call CloseWindow(a:boolBang) " go to starting window while (winnr() != l:iCurWinnr) :wincmd w endwhile endfunction " define plugin's commands command! -nargs=? -bang -complete=file CloseLeftWindow call CloseAnotherWindow('h', 0) command! -nargs=? -bang -complete=file CloseBelowWindow call CloseAnotherWindow('j', 0) command! -nargs=? -bang -complete=file CloseAboveWindow call CloseAnotherWindow('k', 0) command! -nargs=? -bang -complete=file CloseRightWindow call CloseAnotherWindow('l', 0) command! -nargs=? -bang -complete=file CloseAnotherWindow call CloseAnotherWindow("", 0) nnoremap CloseAnotherWindow :call CloseAnotherWindow(inputtarget(), 0)