Skip to content

Instantly share code, notes, and snippets.

@hovsater
Last active October 21, 2021 08:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hovsater/5068370 to your computer and use it in GitHub Desktop.
Save hovsater/5068370 to your computer and use it in GitHub Desktop.
Removing non-empty local directories with netrw

A few months ago I read Drew Neil's article about split windows and the project drawer. It's a great article and I strongly recommend you to go ahead and read it.

The article got me spending some time with netrw and sure enough, it replaced NERDTree. One thing that annoyed me though, was its default for deleting directories. Quoting from :help netrw-delete.

Deleting/removing files and directories involves moving the cursor to the file/directory to be deleted and pressing "D". Directories must be empty first before they can be successfully removed.

This is not a default I'm happy with, but as with everything else in Vim, netrw is customisable. The default command for deleting local directories is rmdir which requires the directory to be empty. By setting g:netrw_localrmdir to something else, such as rm -r I get the behavior I want.

Excerpt from my .vimrc:

" Allow netrw to remove non-empty local directories
"
let g:netrw_localrmdir='rm -r'

Happy Hacking!

GitHub: KevinSjoberg | Twitter: KevinSjoberg

@ming-codes
Copy link

Thanks for the tip!

With this, you're free to change the rm command to anything you want. Right now I disabled the rm command on my machine with alias and switched to use trash instead to move everything to trash.

@xhoskin
Copy link

xhoskin commented Oct 21, 2015

Great! Thnx!

Is it possible somehow to copy a non empty dir in netrw? Tried "let g:netrw_localcopycmd = 'cp -r'", but doesn't work (

@nomasprime
Copy link

Thanks, this has been bugging me for ages.

@kiryph
Copy link

kiryph commented Dec 11, 2016

I was trying to achieve the same on Microsoft Windows 10 with following line:

let g:netrw_localrmdir='C:\Windows/system32/cmd.exe /c rmdir /s /q'

The switch \s is similar to -r.

However, there were issues in the code of netrw-156 in vim8 which require to switch to the more recent netrw-162g version available on http://www.drchip.org/astronaut/vim/index.html#NETRW.

With this new version the variable becomes obsolete:

  *g:netrw_localrmdir*		="rmdir"        Linux/Unix/MacOS/Cygwin  
 				=expand("$COMSPEC")             Windows
				Remove directory command (rmdir)
				This variable is only used if your vim is
				earlier than 7.4 or if your vim doesn't
				have patch#1107.  Otherwise, |delete()|
				is used with the "d" option.

This means your tip does not work anymore.
That's too bad. I was hoping that netrw would offer more convenient default behavior than the command-line. However, there is also the risk that a simple press of D deletes your complete homedirectory. So there is a good reason not to offer such a feature.

The following might be a better reason to re-introduce this variable:
When people want to use programs like rmtrash (OSX) or trash-cli (https://github.com/andreafrancia/trash-cli/), they cannot do this anymore.

@jordan-acosta
Copy link

If netrw_localrmdir doesn't work for you, remember that, unless you change it, it's just an alias for netrw_rmdir_cmd. Setting that will have the same effect, while maintaining consistent local and remote behavior.

Accidentally deleting directories shouldn't be an issue, either, since netrw requires confirmation before deleting.

See the netrw doc for more info: http://vimdoc.sourceforge.net/htmldoc/usr_22.html

@tatethurston
Copy link

If your vim has 7.4 with patch#1107, then |g:netrw_localrmdir| no longer
is used to remove directories; instead, vim's |delete()| is used with
the "d" option.  Please note that only empty directories may be deleted
with the "D" mapping.  Regular files are deleted with |delete()|, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment