Skip to content

Instantly share code, notes, and snippets.

@kshenoy
Last active December 17, 2015 19:39
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 kshenoy/5661878 to your computer and use it in GitHub Desktop.
Save kshenoy/5661878 to your computer and use it in GitHub Desktop.
Function to toggle/enable/disable scrollbind for a set of open window splits
function! ScrollBind(...)
" Description: Toggle scrollbind amongst window splits
" Arguments: 'mode' ( optional ) If not given, toggle scrollbind
" = 0 - Disable scrollbind
" 1 - Enable scrollbind
let l:curr_bufnr = bufnr('%')
let g:scb_status = ( a:0 > 0 ? a:1 : !exists('g:scb_status') || !g:scb_status )
if !exists('g:scb_pos') | let g:scb_pos = {} | endif
let l:loop_cont = 1
while l:loop_cont
setl noscb
if !g:scb_status && has_key( g:scb_pos, bufnr('%') )
call setpos( '.', g:scb_pos[ bufnr('%') ] )
endif
execute "wincmd w"
let l:loop_cont = !( l:curr_bufnr == bufnr('%') )
endwhile
if g:scb_status
let l:loop_cont = 1
while l:loop_cont
let g:scb_pos[ bufnr('%') ] = getpos( '.' )
normal! gg
setl scb
execute "wincmd w"
let l:loop_cont = !( l:curr_bufnr == bufnr('%') )
endwhile
else
let g:scb_pos = {}
endif
"if g:scb_status
" echom "Enabling scrollbind"
"else
" echom "Disabling scrollbind"
"endif
endfunction
nmap <silent> <leader>sb :call ScrollBind()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment