sponsor Vim development Vim logo Vim Book Ad

basic Tip #1443: Quick switch buffers in one window

 tip karma   Rating -2/9, Viewed by 727 

created:   December 25, 2006 17:50      complexity:   basic
author:   Johnny      as of Vim:  

Some times we use tag-jump or some other things to jump from current buffer to another one, But when we want to go back we may found some problems. Yes, may be you can use Ctrl - o to solve this, to jump to a old position, but may be you would like to enhance this, like Visual-Studio's, which use the Ctrl-Tab to swap between two buffers. Here is the tip:

Copy this to your _vimrc
" --ex_GotoLastEditBuffer--
function! g:ex_GotoLastEditBuffer() " <<<
    " check if buffer existed and listed
    let bufnr = bufnr("#")
    if buflisted(bufnr) && bufloaded(bufnr) && bufexists(bufnr)
        silent exec bufnr."b!"
    else
    echohl WarningMsg
    echomsg a:msg
    echohl None
        
call g:ex_WarningMsg("Buffer: " .bufname(bufnr).  " can't be accessed.")
    endif
endfunction " >>>

 rate this tip  Life Changing Helpful Unfulfilling 

<<Let "Open in tabs" as easy as Textpad, UltraEdit, PSPad, Editplus | Shows what function the cursor is in. (for C/C++) >>

Additional Notes

Johnny, December 25, 2006 17:55
Oh, I'm so sorry that I click the "Add" button with careless, and I still not finish the tip:

Forget the upon one.
Copy the script to your _vimrc

" --ex_WarningMsg--
" Display a message using WarningMsg highlight group
function! g:ex_WarningMsg(msg) " <<<
    echohl WarningMsg
    echomsg a:msg
    echohl None
endfunction " >>>

" --ex_GotoLastEditBuffer--
function! g:ex_GotoLastEditBuffer() " <<<
    " check if buffer existed and listed
    let bufnr = bufnr("#")
    if buflisted(bufnr) && bufloaded(bufnr) && bufexists(bufnr)
        silent exec bufnr."b!"
    else
        call g:ex_WarningMsg("Buffer: " .bufname(bufnr).  " can't be accessed.")
    endif
endfunction " >>>

---------------------------------------------------------

after copied, write a map like:

map <silent> <C-Tab> :call g:ex_GotoLastEditBuffer()<CR>
map! <silent> <C-Tab> :call g:ex_GotoLastEditBuffer()<CR>

---------------------------------------------------------

now run your vim, try it when you open several buffers....

Anonymous, December 26, 2006 5:21
what's wrong with ctrl-^
[email protected], December 26, 2006 9:09
ctrl-tab in Visual Studio cycles among ALL open document windows -- it's the equivalent of :bnext in vim, and I have it mapped to that.
Anonymous, December 26, 2006 11:12
And what about :e # if you wanted to use the command line.
no jacket required., December 28, 2006 16:37
Also see

:ls!
:bufs!
:buf <space> <tab> to complete buffer names.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
    stats
Sponsored by Web Concept Group Inc. SourceForge.net Logo