Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete non-empty directory in vim

Tags:

vim

Vim users would be familiar with getting into and viewing the current directory listing by using

:o .

In this directory view, we are able to give additional commands like d and vim will respond with "Please give directory name:". This of course allows us to create a new directory in the current directory once we provide a directory name to vim.

Similarly, we can delete an empty directory by first moving our cursor down to the listing that underlines the specific directory we want to remove and typing D.

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

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

like image 358
Calvin Cheng Avatar asked Aug 26 '11 06:08

Calvin Cheng


2 Answers

The directory view you're referring to is called netrw. You could read up on its entire documentation with :help netrw, but what you're looking for in this case is accessible by :help netrw-delete:

The g:netrw_rmdir_cmd variable is used to support the removal of directories. Its default value is:

g:netrw_rmdir_cmd: ssh HOSTNAME rmdir

If removing a directory fails with g:netrw_rmdir_cmd, netrw then will attempt to remove it again using the g:netrw_rmf_cmd variable. Its default value is:

g:netrw_rmf_cmd: ssh HOSTNAME rm -f

So, you could override the variable that contains the command to remove a directory like so:

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

EDIT: Like sehe pointed out, this is fairly risky. If you need additional confirmation in case the directory is not empty, you could write a shell script that does the prompting. A quick google turned up this SO question: bash user input if.

So, you could write a script that goes 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 your script

let g:netrw_rmf_cmd = 'safe-delete HOSTNAME'

A lot of careful testing is recommended, of course :).

like image 119
Andrew Radev Avatar answered Sep 21 '22 12:09

Andrew Radev


Andrew's answer doesn't work for me. I have found another way to this question. Try :help netrw_localrmdir.

Settings from my .vimrc file:

let g:netrw_localrmdir="rm -r"
like image 33
diabloneo Avatar answered Sep 19 '22 12:09

diabloneo