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