1 "=============================================================================
2 " File: quit_another_window.vim
3 " Author: Dmitry Frank (dimon.frank@gmail.com)
5 "=============================================================================
6 " See documentation in accompanying help file
7 " You may use this code in whatever way you see fit.
17 function! s:inputtarget()
29 function! <SID>QuitWindow(boolBang)
37 "" function to close other window
38 " @param sWincmd - window movement command(s).
39 " for example, "h" used to close left window,
40 " "j" used to close lower window,
43 " You can combine them:
44 " "jl" used to go to lower window,
45 " then go to right window,
48 function! <SID>QuitAnotherWindow(sWincmd, boolBang)
51 call <SID>QuitWindow(a:boolBang)
55 " remember current winnr, e.g. winnr of starting window
56 " (we need to go to the same winnr after close other window)
57 let l:iCurWinnr = winnr()
61 while (l:iIndex < strlen(a:sWincmd))
62 " move to window that should be closed and get its winnr
63 exec 'wincmd '.a:sWincmd[l:iIndex]
64 let l:iClosedWinnr = winnr()
69 " if its winnr is the same as starting winnr, then do nothing.
70 " (you should use just :q instead)
71 if (l:iCurWinnr == l:iClosedWinnr)
75 " calculate target winnr
76 if (l:iCurWinnr > l:iClosedWinnr)
81 call <SID>QuitWindow(a:boolBang)
83 " go to starting window
84 while (winnr() != l:iCurWinnr)
91 " define plugin's commands
92 command! -nargs=? -bang -complete=file Qh call <SID>QuitAnotherWindow('h', <bang>0)
93 command! -nargs=? -bang -complete=file Qj call <SID>QuitAnotherWindow('j', <bang>0)
94 command! -nargs=? -bang -complete=file Qk call <SID>QuitAnotherWindow('k', <bang>0)
95 command! -nargs=? -bang -complete=file Ql call <SID>QuitAnotherWindow('l', <bang>0)
97 command! -nargs=? -bang -complete=file Q call <SID>QuitAnotherWindow("<args>", <bang>0)
99 nnoremap <silent> <Plug>QAnotherWin :<C-U>call <SID>QuitAnotherWindow(<SID>inputtarget(), 0)<CR>
100 if !hasmapto("<Plug>QAnotherWin","n")
101 nmap <silent> <C-z> <Plug>QAnotherWin
104 " define lowercased aliases if possible
105 if exists("loaded_cmdalias") && exists("*CmdAlias")
106 call CmdAlias('qh', 'Qh')
107 call CmdAlias('qj', 'Qj')
108 call CmdAlias('qk', 'Qk')
109 call CmdAlias('ql', 'Ql')
111 call CmdAlias('q', 'Q')