7

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?

Calvin Cheng
  • 35,640
  • 39
  • 116
  • 167

2 Answers2

7

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 :).

Community
  • 1
  • 1
Andrew Radev
  • 3,982
  • 22
  • 35
  • giving you the +1 because it _is_ a way. I'm not sure I'd recommend it, though. I like to have the nuclear weapons disarmed 99% of time :) – sehe Aug 26 '11 at 07:18
4

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"
diabloneo
  • 2,607
  • 2
  • 18
  • 17
  • Alternately, you might feel safer moving the directory to your trash, if your OS has one. Example: `let g:netrw_localrmdir="mv PATH_TO_YOUR/Trash"`. Make sure you check what trash path makes sense for your OS, or use a tool to simplify trashing from the command line like https://github.com/andreafrancia/trash-cli. – thisgeek Jan 07 '19 at 16:33