Created
May 8, 2025 04:35
-
-
Save DinoChiesa/5929ab4152c6c591ebca37551e6d02d9 to your computer and use it in GitHub Desktop.
Copy or move files or directories from one dired buffer to another
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun mode-for-buffer (&optional buffer-or-string) | |
"Returns the major mode associated with a buffer." | |
(with-current-buffer (or buffer-or-string (current-buffer)) | |
major-mode)) | |
(defun my-dired-copy-or-move-other-window (operation) | |
"Copy or move the marked files to another directory. | |
OPERATION is a symbol, either `COPY' or `MOVE' . | |
This works with files or directories." | |
(unless (eq major-mode 'dired-mode) | |
(error "works only when current-buffer is in dired-mode")) | |
(let ((other-visible-dired-buffers | |
(delq nil (mapcar #'(lambda (w) | |
(let* ((b (window-buffer w)) | |
(m (mode-for-buffer b))) | |
(and (eq m 'dired-mode) | |
(not (eq b (current-buffer))) | |
b))) | |
(window-list))))) | |
(unless (= (length other-visible-dired-buffers) 1) | |
(error "Can copy only if exactly 2 dired windows are visible")) | |
(let ((fns (cond | |
((eq operation 'COPY) | |
'(copy-file copy-directory)) | |
((eq operation 'MOVE) | |
'(rename-file rename-file)) | |
(t | |
(error "unsupported operation") | |
))) | |
(dst-dir (expand-file-name (with-current-buffer (car other-visible-dired-buffers) | |
default-directory)))) | |
(mapc #'(lambda (f) | |
(let ((fn | |
(cond | |
((file-directory-p f) | |
(cadr fns)) | |
(t | |
(car fns))))) | |
(funcall fn f dst-dir 1))) | |
(dired-get-marked-files nil)) | |
(with-current-buffer (car other-visible-dired-buffers) | |
(revert-buffer)) | |
(revert-buffer)))) | |
(defun my-dired-move-file-to-dir-in-other-window (&optional arg) | |
"If there are two or more windows, and the current one is in | |
dired-mode, and one of the others is also dired-mode, then move | |
the file under cursor or the marked files to the directory shown | |
in the other dired window. If the current buffer is not in | |
dired-mode, or if not exactly 2 windows show dired, then message | |
and quit. | |
" | |
(interactive "P") | |
(my-dired-copy-or-move-other-window 'MOVE)) | |
(defun my-dired-copy-file-to-dir-in-other-window (&optional arg) | |
"If there are two or more windows, and the current one is in | |
dired-mode, and one of the others is also dired-mode, then copy | |
the file under cursor or the marked files to the directory shown | |
in the other dired window. If the current buffer is not in | |
dired-mode, or if not exactly 2 windows show dired, then message | |
and quit. | |
" | |
(interactive "P") | |
(my-dired-copy-or-move-other-window 'COPY)) | |
(defun my-dired-mode-hook-fn () | |
(keymap-local-set "C-c C-c" #'my-dired-copy-file-to-dir-in-other-window) | |
(keymap-local-set "C-c C-m" #'my-dired-move-file-to-dir-in-other-window)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing this code! I'm curious how this differs from Emacs's built-in
(setq dired-dwim-target t)
?