Skip to content

Instantly share code, notes, and snippets.

@grota
Created October 10, 2011 20:31
Show Gist options
  • Save grota/1276427 to your computer and use it in GitHub Desktop.
Save grota/1276427 to your computer and use it in GitHub Desktop.
Easier buffer navigation, to review
" Easy buffer navigation, the following:
" http://vim.wikia.com/wiki/Avoid_scrolling_when_switch_buffers
" does not work because the events BufLeave,BufEnter are called
" before the <C-w>|<C-w>_ commands
function Savewinpos()
let b:winview = winsaveview()
endfunc
function RestoreWinPos()
if(exists('b:winview')) | call winrestview(b:winview) | endif
endfunc
nnoremap <silent> <C-h> :call Savewinpos()<CR><C-w>h<C-w>\|<C-w>_:call RestoreWinPos()<CR>
nnoremap <silent> <C-j> :call Savewinpos()<CR><C-w>j<C-w>\|<C-w>_:call RestoreWinPos()<CR>
nnoremap <silent> <C-k> :call Savewinpos()<CR><C-w>k<C-w>\|<C-w>_:call RestoreWinPos()<CR>
nnoremap <silent> <C-l> :call Savewinpos()<CR><C-w>l<C-w>\|<C-w>_:call RestoreWinPos()<CR>
@sjl
Copy link

sjl commented Oct 10, 2011

It seems like there's a chance Vim will end up maximizing the window but not restoring the state. Does that happen in practice?

@grota
Copy link
Author

grota commented Oct 10, 2011

didn't happen to me yet, but I've been using this since only today.

What do you think about this implementation in terms of:

  • polluting the "global namespace" with the 2 functions
  • performance
  • possible alternative implementations with

@sjl
Copy link

sjl commented Oct 10, 2011

If you move the | and _ wincmd's into the Restore function you wouldn't have to worry about inadvertently maximizing any windows.

You could prefix the function names with s: to make them local to the script.

Performance will probably not even be noticeably affected, but if it is you'll be able to tell pretty quickly.

I wouldn't worry about expr -- this is clear enough and it works.

@grota
Copy link
Author

grota commented Oct 10, 2011

gotcha, thanks!
I'm getting an E81 while using the s: prefix, but I'll work it out.
a 🎩 to you! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment