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.
Right-click the file, then click Delete on the shortcut menu. Tip: You can also select more than one file to be deleted at the same time. Press and hold the CTRL key as you select multiple files to delete.
Put the following in your vimrc (all on one line) and you can use the "Remove" command with the normal Vim command-line arguments (such as "%" to remove the current file). It also has filename completion. :call delete(expand('%')) | bdelete!
To delete a word, position the cursor at the beginning of the word and type dw . The word and the space it occupied are removed. To delete part of a word, position the cursor on the word to the right of the part to be saved. Type dw to delete the rest of the word.
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.
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 %
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With