How can I delete the current file in 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 %
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.
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 alocate
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>
.