You can use the CursorHold and CursorHoldI events of autocmd
to implement this easily.
This autocmd
is sufficient to enable this for the current buffer:
:autocmd CursorHold,CursorHoldI <buffer> %d
This will run the %d
command (delete whole buffer) once no key is pressed for the duration of 'updatetime'
, which defaults to 4 seconds.
If you want to update that to a shorter or longer interval, just set that option (it takes a number of milliseconds.) To erase after 10s of inactivity, for instance:
:setlocal updatetime=10000
Using both CursorHoldI
and CursorHold
means this works whether you're in insert mode or normal mode. There are some limitations though, it doesn't trigger in Visual mode, when waiting for a command to be typed or in operator-pending mode.
You can also undo the deletion using u
, or just paste back from the default buffer... So it's not foolproof (it all depends on what you're trying to accomplish, I guess.) It's probably a good start though, it's pretty simple.
(If you're looking for something more robust and you don't mind depending on Vim 8 or nvim features, you can probably build something better using timer_start()
.)