Created
July 22, 2013 16:55
-
-
Save dmitry-vsl/6055527 to your computer and use it in GitHub Desktop.
vim alt-tab-fashioned buffer switching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" -------------------- | |
" Alt tab over buffers | |
" -------------------- | |
"Stack of recently visited buffers | |
let g:alttab_buffer_stack = [] | |
"Number of consequently pressed alttab | |
let g:alttab_streak_length = 0 | |
"Constant | |
let g:alttab_history_length = 50 | |
"Is buffer change toggled by pressing tab | |
let g:alttab_tabbing = 0 | |
let g:alttab_streak_finished = 1 | |
"Put current buffer to stack first removing it if already exists | |
function! Alttab_track_change_buffer() | |
if g:alttab_tabbing | |
return | |
endif | |
call Alttab_finish_streak() | |
let g:new_buffer = bufname("%") | |
call filter(g:alttab_buffer_stack,'v:val != g:new_buffer') | |
call insert(g:alttab_buffer_stack,g:new_buffer) | |
if len(g:alttab_buffer_stack)>g:alttab_history_length | |
call remove(g:alttab_buffer_stack,-1) | |
endif | |
endfunction | |
function! Alttab() | |
if g:alttab_streak_finished | |
let g:alttab_streak_length = 0 | |
endif | |
let g:alttab_streak_finished = 0 | |
let g:alttab_streak_length = ((g:alttab_streak_length + 1) % len(g:alttab_buffer_stack)) | |
let g:alttab_tabbing = 1 | |
execute ":b ".(g:alttab_buffer_stack[g:alttab_streak_length]) | |
let g:alttab_tabbing = 0 | |
return | |
endfunction | |
"Move current buffer to the top of stack | |
function! Alttab_finish_streak() | |
if g:alttab_streak_finished | |
return | |
endif | |
let g:alttab_streak_finished = 1 | |
let l:current_buffer = remove(g:alttab_buffer_stack,g:alttab_streak_length) | |
call insert(g:alttab_buffer_stack,l:current_buffer) | |
endfunction | |
autocmd BufEnter * call Alttab_track_change_buffer() | |
autocmd CursorHold * call Alttab_finish_streak() | |
autocmd InsertEnter * call Alttab_finish_streak() | |
nnoremap <Tab> :call Alttab()<CR> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment