How can I delete the current file in vim?

Vim

Vim Problem Overview


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 plugin.

Vim Solutions


Solution 1 - Vim

Using an external tool such as rm(1) is fine, but Vim also has its own delete() function for deleting files. This has the advantage of being portable.

:call delete(expand('%'))

An alternative way of expressing this is :call delete(@%), which uses the % (current file) register (tip by @accolade).

To completely purge the current buffer, both the file representation on disk and the Vim buffer, append :bdelete:

:call delete(expand('%')) | bdelete!

You'll probably want to map this according to your preferences.

Solution 2 - Vim

Take a look at Delete files with a Vim command. The Comments section should have what you're looking for:

> Basically, Rm will delete the current file; RM will delete the current file and quit the buffer (without saving) in one go.

Alternatively, you could just issue a shell command to remove the current file:

:!rm %

Solution 3 - Vim

Sometimes a plugin can be an attractive solution even for a simple problem. In this case we're lucky as there is eunuch.vim by the almighty Tim Pope.

In its own words eunuch.vim provides > Vim sugar for the UNIX shell commands that need it the most. Delete or rename a buffer and the underlying file at the same time. Load a find or a locate into the quickfix list. And so on.

Perfect. It has what we need, plus some additional tools if we're on a UNIX system.

The command you are looking for is

:Remove!

Again, remap it if you need it a lot, e.g. :nnoremap <Leader>rm :Remove!<CR>.

Solution 4 - Vim

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 <Leader>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 <CR> to the first line

nnoremap <Leader>d. :call DeleteFileAndCloseBuffer()<CR>

and only have to press return once.

Solution 5 - Vim

You can do it with two steps:

  1. save as a new file

    :w newfilename

  2. delete the old file

    ! rm oldfilename

Solution 6 - Vim

This may be an unpopular opinion but your file tree explorer (Netrw/NerdTree) is going to be the simplest and safest way to delete a file. Even if OP is not using NerdTree, the in-built plugin Netrw will work just as well.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfotanusView Question on Stackoverflow
Solution 1 - VimgltsView Answer on Stackoverflow
Solution 2 - VimDavid CainView Answer on Stackoverflow
Solution 3 - VimgltsView Answer on Stackoverflow
Solution 4 - VimjoelostblomView Answer on Stackoverflow
Solution 5 - VimRanger WayView Answer on Stackoverflow
Solution 6 - VimAlex MckayView Answer on Stackoverflow