Working with dired

I’ve been making a lot more use of dired recently. One use case is copying files from my remote server to my home machine. Doing this directly from dired, even with the power of tramp, is a little too time consuming and potentially locks up your session for large files. While browsing reddit r/emacs I found a reference to this post that spurred me to look at spawning rsync from dired some more.

Unfortunately the solution is currently sitting in a pull-request to what looks like an orphaned package. I also ran into some other problems with the handling of where rsync needs to be run from so rather than unpicking some unfamiliar code I decided to re-implement everything in my own package.

I’ve still got some debugging to do to get it to cleanly handle multiple sessions as well as a more detailed mode-line status. Once I’m happy I’ll tag a 0.1 and get it submitted to MELPA.

While getting more familiar with dired I also came up with this little helper:

(defun my-dired-frame (directory)
  "Open up a dired frame which closes on exit."
  (interactive)
  (switch-to-buffer (dired directory))
  (local-set-key
   (kbd "C-x C-c")
   (lambda ()
     (interactive)
     (kill-this-buffer)
     (save-buffers-kill-terminal 't))))

Which is paired with a simple alias in my shell setup:

alias dired="emacsclient -a '' -t -e '(my-dired-frame default-directory)'"

This works really nicely for popping up a dired frame in your terminal window and cleaning itself up when you exit.

2 Comments

  1. Thank you Alex for sharing! After exiting from dired the directory is the same as the initial one (that one in the shell before invoking the command)? How to adapt this helper to invoke a `cd` on the last directory visited from dired? So that after I close dired, the directory in my shell will be the last visited one. What do you think about that?

    • I’m sure it’s possible but there is a minor complication caused by the asynchronous nature of the request. While it’s perfectly possible to return values from emacsclient -e to the shell we don’t actually execute in that context when we close our final dired buffer. You would have to dump the directory to a somewhere and have an alias do something like:

      (defvar my-last-dired-directory
      nil
      “Return the directory dired was last killed in.”)

      (defun my-dired-frame (directory)
      “Open up a dired frame in `DIRECTORY’ which closes on exit.”
      (interactive)
      (switch-to-buffer (dired directory))
      (local-set-key
      (kbd “C-x C-c”)
      (lambda ()
      (interactive)
      (setq my-last-dired-directory (file-truename default-directory))
      (kill-this-buffer)
      (save-buffers-kill-terminal ‘t))))

      You would also need to ensure dired stayed in use one buffer mode.

Leave a Reply

Your email address will not be published. Required fields are marked *