Skip to content

Instantly share code, notes, and snippets.

@tyru
Created October 30, 2010 00:08
Show Gist options
  • Save tyru/654701 to your computer and use it in GitHub Desktop.
Save tyru/654701 to your computer and use it in GitHub Desktop.
" Swap window without moving cursor {{{
nmap <Space>j <SID>(swap-window-down-no-cursor-move)
nmap <Space>k <SID>(swap-window-up-no-cursor-move)
nmap <Space>h <SID>(swap-window-left-no-cursor-move)
nmap <Space>l <SID>(swap-window-right-no-cursor-move)
nnoremap <SID>(swap-window-down-no-cursor-move) :<C-u>call <SID>swap_with_wincmd(v:count1, 'j')<CR>
nnoremap <SID>(swap-window-up-no-cursor-move) :<C-u>call <SID>swap_with_wincmd(v:count1, 'k')<CR>
nnoremap <SID>(swap-window-left-no-cursor-move) :<C-u>call <SID>swap_with_wincmd(v:count1, 'h')<CR>
nnoremap <SID>(swap-window-right-no-cursor-move) :<C-u>call <SID>swap_with_wincmd(v:count1, 'l')<CR>
function! s:swap_with_wincmd(n, dir) "{{{
let curwin = winnr()
execute a:n 'wincmd' a:dir
let targetwin = winnr()
wincmd p
call s:swap_window(curwin, targetwin)
endfunction "}}}
function! s:swap_window(curwin, targetwin) "{{{
let curbuf = winbufnr(a:curwin)
let targetbuf = winbufnr(a:targetwin)
if curbuf == targetbuf
" TODO: Swap also same buffer!
else
execute 'hide' targetbuf . 'buffer'
execute a:targetwin 'wincmd w'
execute curbuf 'buffer'
wincmd p " Behave like <C-w>x ?
endif
endfunction "}}}
" }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment