Skip to content

Instantly share code, notes, and snippets.

@eagletmt
Forked from thinca/.vimrc
Created May 16, 2010 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eagletmt/403042 to your computer and use it in GitHub Desktop.
Save eagletmt/403042 to your computer and use it in GitHub Desktop.
" Move window position {{{
nnoremap <silent> <Plug>swap_window_next :<C-u>call <SID>swap_window(v:count1)<CR>
nnoremap <silent> <Plug>swap_window_prev :<C-u>call <SID>swap_window(-v:count1)<CR>
nnoremap <silent> <Plug>swap_window_j :<C-u>call <SID>swap_window_dir(v:count1, 'j')<CR>
nnoremap <silent> <Plug>swap_window_k :<C-u>call <SID>swap_window_dir(v:count1, 'k')<CR>
nnoremap <silent> <Plug>swap_window_h :<C-u>call <SID>swap_window_dir(v:count1, 'h')<CR>
nnoremap <silent> <Plug>swap_window_l :<C-u>call <SID>swap_window_dir(v:count1, 'l')<CR>
nnoremap <silent> <Plug>swap_window_t :<C-u>call <SID>swap_window_dir(v:count1, 't')<CR>
nnoremap <silent> <Plug>swap_window_b :<C-u>call <SID>swap_window_dir(v:count1, 'b')<CR>
function! s:modulo(n, m) "{{{
let d = a:n * a:m < 0 ? 1 : 0
return a:n + (-(a:n + (0 < a:m ? d : -d)) / a:m + d) * a:m
endfunction "}}}
function! s:swap_window(n) "{{{
let curbuf = bufnr('%')
let target = s:modulo(winnr() + a:n - 1, winnr('$')) + 1
execute 'hide' winbufnr(target) . 'buffer'
execute target 'wincmd w'
execute curbuf 'buffer'
endfunction "}}}
function! s:swap_window_dir(n, dir) "{{{
let curbuf = bufnr('%')
execute a:n 'wincmd ' . a:dir
let target = winnr()
let targetbuf = bufnr('%')
if curbuf != targetbuf
wincmd p
execute 'hide' targetbuf . 'buffer'
execute target 'wincmd w'
execute curbuf 'buffer'
endif
endfunction "}}}
" }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment