Where parallels cross

Interesting bits of life

Delete filename or list of filenames in region with one Emacs command

I have a piece of software at work that spits out a list of files that we may wish to delete. Often I do want to, but I postpone for a reason or another: so the list keeps growing!

I got to the point that was worth to write an Elisp function for it.

So say you have a list of files:

/tmp/fileA.txt
/tmp/fileB.txt
/tmp/fileC.txt

And you have the following code loaded (requires dash.el):

(defun delete-files-for-filepaths-in-region ()
  (interactive)
  (let ((to-delete-string (or (when (region-active-p)
                                (buffer-substring-no-properties
                                 (caar (region-bounds))
                                 (cdar (region-bounds))))
                              (thing-at-point 'filename t))))
    (--> to-delete-string
         (s-split "\n" it t)
         (-each it
           'delete-file))
    (message "Files deleted!")))

you will just need to mark the region around the list of files to remove them all. Didn't need error handling in my case, but that saved me some time. Hopefully it helps you too.

Happy deleting!

Comments