Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete the current file in vim?

Tags:

vim

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.

like image 526
fotanus Avatar asked May 21 '13 20:05

fotanus


People also ask

How do I delete a current file?

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.

How do I delete a Vim file in Unix?

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!

What is the command to delete the current in vi editor?

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.


Video Answer


2 Answers

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.

like image 99
glts Avatar answered Sep 21 '22 17:09

glts


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 % 
like image 43
David Cain Avatar answered Sep 23 '22 17:09

David Cain