2012/7/12 <geoffrey.wood@thomsonreuters.com>
On Thursday, 12 July 2012 00:38:04 UTC+1, Chris Jones wrote:> [I wrote a function to] feed the output of the :ls command to a variable and
> I usually have a single Vim instance that stays up for weeks if not
> months at a time, such that I eventually have a large number of buffers
> as listed by the ':ls/:buffers' command.. well over 100 at this point in
> time. Even though I usually take good care to exit files once I know
> I will no longer need them (via a ':q/:wq').
> filter out buffers that are either 'active' (displayed in a window) or> 'modified'. All others get buffer-wiped. [The function
> has potential problems.]
> on a different approach & suggest how I might be able to come up with
> Has anyone looked into buffer list manipulation before and could advise
> something a bit less clunky..?
I use the following bufonly plugin written by CJ Robinson, which works perfectly for me.
Originally coming from using IDEs such as Eclipse,
the provided :BOnly command works as the "close others" on the Tabs of Eclipse.
I use it quite frequently. Also you do not have to worry about changed buffers getting deleted,
the plugin warns you and does not close them, unless you provide a Bang like this :BOnly!
Actually, you can check it out here:
or, to install it, put the following vimscript as bonly.vim in your ~/.vim/plugin directory:
" BufOnly.vim - Delete all the buffers except the current/named buffer.
"
" Copyright November 2003 by Christian J. Robinson <heptite@gmail.com>
"
" Distributed under the terms of the Vim license. See ":help license".
"
" Usage:
"
" :Bonly / :BOnly / :Bufonly / :BufOnly [buffer]
"
" Without any arguments the current buffer is kept. With an argument the
" buffer name/number supplied is kept.
command! -nargs=? -complete=buffer -bang Bonly
\ :call BufOnly('<args>', '<bang>')
command! -nargs=? -complete=buffer -bang BOnly
\ :call BufOnly('<args>', '<bang>')
command! -nargs=? -complete=buffer -bang Bufonly
\ :call BufOnly('<args>', '<bang>')
command! -nargs=? -complete=buffer -bang BufOnly
\ :call BufOnly('<args>', '<bang>')
function! BufOnly(buffer, bang)
if a:buffer == ''
" No buffer provided, use the current buffer.
let buffer = bufnr('%')
elseif (a:buffer + 0) > 0
" A buffer number was provided.
let buffer = bufnr(a:buffer + 0)
else
" A buffer name was provided.
let buffer = bufnr(a:buffer)
endif
if buffer == -1
echohl ErrorMsg
echomsg "No matching buffer for" a:buffer
echohl None
return
endif
let last_buffer = bufnr('$')
let delete_count = 0
let n = 1
while n <= last_buffer
if n != buffer && buflisted(n)
if a:bang == '' && getbufvar(n, '&modified')
echohl ErrorMsg
echomsg 'No write since last change for buffer'
\ n '(add ! to override)'
echohl None
else
silent exe 'bdel' . a:bang . ' ' . n
if ! buflisted(n)
let delete_count = delete_count+1
endif
endif
endif
let n = n+1
endwhile
if delete_count == 1
echomsg delete_count "buffer deleted"
elseif delete_count > 1
echomsg delete_count "buffers deleted"
endif
endfunction
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
No comments:
Post a Comment