Brian Postow
Brian Postow

Reputation: 12207

recursively remove subdir from emacs dired

I often look at dired structures, showing the entire directory recursively, via: (dired dir "-lR"). This works great most of the time.

However, some times, there are huge sub-directory structures that Idon't want to look at. Is there a way to recursively kill a subdirectory in a dired buffer with a complete tree in it?

Upvotes: 1

Views: 645

Answers (3)

Drew
Drew

Reputation: 30708

Use Dired+, command diredp-kill-this-tree.

Just put the cursor in a subdir that you want to remove from the listing, and invoke the command. That inserted subdir and all its descendents that are also inserted (listed) are removed.

You can invoke this command also in either of these ways, after putting point within a subdir listing:

  • Click mouse-3 and choose item Remove This Inserted Subdir and Lower.
  • Choose menu-bar menu Single (called Immediate in vanilla Emacs), item Remove This Inserted Subdir and Lower.

Upvotes: 1

Brian Postow
Brian Postow

Reputation: 12207

Ok, I just wrote the following elisp:

(defun bp-kill-directory-rec()
  (interactive )
  (let (
    (i (point))
        (cur-dir (dired-current-directory)))
    (beginning-of-buffer)
    (while (search-forward cur-dir nil t)
      (dired-kill-subdir))
    (goto-char i)))

Start with the cursor in the directory that you want to kill, and it will kill all subdirs of that directory. The trick is that (dired-current-directory) gives the entire path, so searching for that should only give the directories you want to kill. If you have a directory structure, where you have something like /home/a/b and also /home/c/d/home/a/b then bad things could happen. But you'd have to be kind of crazy to do that, no?

Upvotes: 2

ataylor
ataylor

Reputation: 66109

I use find-dired for the same purpose. To exclude a subdirectory, I enter something like this at the Run find (with args): prompt: -path ./exclude_me -prune -o true, which will give me a recursive listing of the directory excluding the one directory exclude_me. This only works if you're using a Unix-like with a function version of find though.

Edit: Another way to remove the tree is to simply toggle the dired buffer to readable (C-x C-q) and edit the buffer.

Upvotes: 2

Related Questions