Wednesday, July 11, 2012

Cleaning up the buffer list

Ping's question a few hours ago reminded me of something I keep putting
off.

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').

I took a look at buffer-oriented functions and could no see anything at
a glance that looked like it might help, so I put together the following
'prototype' :-) mostly to verify that I understand the problem:

| function! GarCol()
|
| redir @x
| exe ':silent ls'
| redir end
| let mybuffers = @x
| let mbfl = split(mybuffers, '\n')
| for bf in mbfl
| let bfl = split(bf, ' ')
| echo bfl
| if bfl[1] =~ '.a'
| echo 'skipped %a or #a buffer'
| continue
| endif
| if bfl[2] =~ 'a'
| echo 'skipped active buffer'
| continue
| endif
| if bfl[3] =~ '+'
| echo 'skipped modified buffer'
| continue
| endif
| echo bfl[0] ' will be deleted'
| let bfd = bfl[0]
| exe "bw"." ".bfd
| " exe "bun"." ".bfd
| endfor
| return 0
| endfunction

What this does is feed the output of the :ls command to a variable and
filter out buffers that are either 'active' (displayed in a window) or
'modified'. All others get buffer-wiped.

I quickly fired up a Vim session and populated it with test files, and
the above appears to work as I intended, but what bothers me is that
it's based on the output of a command meant for interactive use.

Among other things, the current format is not written in stone & more
subject to change than return values from Vim functions...

Besides, it's based on what I see when I issue the ':buffers' command,
and for all I know, there may be cases where an extra column is added in
the output, (e.g.) so that my list indexing would point to something
else than the flags I was looking for causing unpredictable results.

No big deal of course, since it's only Vim buffers I am throwing away,
not files.. but could be annoying all the same.

Has anyone looked into buffer list manipulation before and could advise
on a different approach & suggest how I might be able to come up with
something a bit less clunky..?

Thanks,

CJ

--
WE GET SIGNAL

--
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: