How can I delete the current file in vim?

后端 未结 6 1909
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 01:35

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

6条回答
  •  悲哀的现实
    2021-01-30 02:06

    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.

提交回复
热议问题