Snippets

Sergio Luiz Araújo Silva Close all buffers but current one

You are viewing an old version of this snippet. View the current version.
Revised by Sergio Araújo 250f52c
" the main reference: https://salferrarello.com/vim-close-all-buffers-except-the-current-one/

" needed function preserve to avoid `"
" this function can be used for many other things like reload vimrc withou moving the cursor
" or reindent the current buffer keeping the cursor position
" preserve function

" Utility function that save last search and cursor position
" http://technotales.wordpress.com/2010/03/31/preserve-a-vim-function-that-keeps-your-state/
" video from vimcasts.org: http://vimcasts.org/episodes/tidying-whitespace
" using 'execute' command doesn't overwrite the last search pattern, so I
" don't need to store and restore it.

if !exists('*Preserve')
    function! Preserve(command)
        try
            let l:win_view = winsaveview()
            "silent! keepjumps keeppatterns execute a:command
            silent! execute 'keeppatterns keepjumps ' . a:command
        finally
            call winrestview(l:win_view)
        endtry
    endfunction
endif

command! BufOnly silent! call Preserve("exec '%bd|e#|bd#'")
cab bo BufOnly

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.