I am not sure how to get the oldest buffers with respect to editing time, but one could, instead, try to close the oldest unedited buffers. Something like:
function CloseLast ()
python <<EOF
import vim
N = 10
listed_buffers = [b for b in vim.buffers if b.options['buflisted'] and not b.options['modified']]
for i in range (0, len (listed_buffers) - N):
vim.command (':bd' + str (listed_buffers[i].number))
EOF
endfunction
autocmd BufNew * call CloseLast()
Notes:
vim.buffers is a list of every buffer opened in the current session, so it also includes unlisted buffers. It is not the same as the list returned by :ls.
- Therefore, we must filter out the hidden or deleted buffers. This can be checked using
options['buflisted'].
- Similarly,
options['modified'] lets us check if the buffer is modified.
N is the number of unmodified, listed buffers you want open.
Thanks to Luc Hermitte's answer from which I learnt how to get the timestamps, you could use the following instead, to get the oldest inactive kicked out first:
listed_buffers = (b for b in vim.buffers if b.options['buflisted'] and not b.options['modified'])
oldest_buffers = sorted (listed_buffers, key = lambda b: eval('getftime("' + b.name + '")'))
for i in range (0, len (oldest_buffers) - N):
vim.command (':bd' + str (oldest_buffers[i].number))
b:variablesbut in as:plugin[bufid]if the plugin maintainer preferred to not pollute the publicb:"namespace". In this case, deleting the buffer won't necessarily collect all related variables/memory. – Luc Hermitte Feb 25 '15 at 16:41