How can I delete from the disk the current open file from within vim? Would be nice to also close the buffer.
I see you can use NERDTree for that, but I don\'t use this
I like being able to delete files from within vim, but I am also paranoid about accidentally deleting important work that, for one reason or another, is not yet under version control. I find it useful to combine the previous information from @glts and @accolade with this answer on how to use the confirm
command to prompt before quitting vim.
Putting these together, I added a function to my ~/.vimrc
, which prompts before deleting the file and closing the buffer, and mapped it to a key combination:
nnoremap d. :call DeleteFileAndCloseBuffer()
fun! DeleteFileAndCloseBuffer()
let choice = confirm("Delete file and close buffer?", "&Do it!\n&Nonono", 1)
if choice == 1 | call delete(expand('%:p')) | q! | endif
endfun
If you are one keystroke less paranoid than I am, you can append
to the first line
nnoremap d. :call DeleteFileAndCloseBuffer()
and only have to press return once.