Delete non-empty directory in vim

Vim users will be familiar with accessing and viewing the current directory listing with

:o . 

In this view of the directory, we can provide additional commands, for example, d and vim will answer "Please indicate the directory name:". This, of course, allows us to create a new directory in the current directory as soon as we provide the directory name for vim.

Similarly, we can delete the empty directory, move our pointer down to the list that underlines the specific directory that we want to delete, and type D.

The problem is that vim does not allow us to delete a non-empty directory.

Is this any way to insist that we delete a non-empty directory?

+6
source share
2 answers

The kind of directory you are accessing is called netrw . You can read all of your documentation with :help netrw , but what you are looking for in this case is available :help netrw-delete :

The variable g: netrw_rmdir_cmd is used to support directory deletion. Its default value is:

g: netrw_rmdir_cmd: ssh HOSTNAME rmdir

If directory deletion fails with g: netrw_rmdir_cmd, netrw will then try to delete it again using the g: netrw_rmf_cmd variable. Its default value is:

g: netrw_rmf_cmd: ssh HOSTNAME rm -f

So, you can redefine the variable containing the command to remove such a directory:

 let g:netrw_rmf_cmd = 'ssh HOSTNAME rm -rf' 

EDIT . As stated above, this is quite risky. If you need additional confirmation, if the directory is not empty, you can write a shell script that invokes the request. A quick google raised this SO question: bash user input if .

So you can write a script that would look like this:

 #! /bin/bash hostname = $1 dirname = $2 # ... # prompt the user, save the result # ... if $yes then ssh $hostname rm -rf $dirname fi 

Then set the command to execute the script

 let g:netrw_rmf_cmd = 'safe-delete HOSTNAME' 

Of course, a lot of rigorous testing is recommended :).

+6
source

Andrew answer does not work for me. I found another way to this question. Try :help netrw_localrmdir .

Settings from my .vimrc file:

 let g:netrw_localrmdir="rm -r" 
+4
source

Source: https://habr.com/ru/post/895942/


All Articles