Is there a way to close file after certain amount of time of user inactivity? Lets say I have a encrypted file and I want to close it after 10 min. Is it possible in Vim?
1 Answer
Yes, but it doesn't come for free in the form of a ready-to-use option; some assembly required:
If you define activity as changes done to the buffer, you can periodically sample b:changedtick
, and record (in a buffer-local variable) a timestamp (:help localtime()
) when it changes. If activity includes movements, you can do the same with the cursor position (getpos()
).
That sampling can be done with :autocmd
; the CursorHold
event can be used for triggering (as long as you allow some flexibility about the time frame for enforcement; as it only fires when Vim is idle).
For closing, this is a lot easier if you're only interested in the current buffer / tab page; you just need to sample the current buffer / all visible buffers, and can then use a simple :bdelete
/ :tabclose
. If you also want to kick out other buffers, you'll need to use getbufvar()
to sample all buffers (without moving to it).