Create a gist now

Instantly share code, notes, and snippets.

Move a file from somewhere else to here.
;; move file here ;;
(require 'dash)
(require 'swiper)
;; start directory
(defvar bjm/move-file-here-start-dir (expand-file-name "~/downloads"))
(defun bjm/move-file-here ()
"Move file from somewhere else to here.
The file is taken from a start directory set by `bjm/move-file-here-start-dir' and moved to the current directory if invoked in dired, or else the directory containing current buffer. The user is presented with a list of files in the start directory, from which to select the file to move, sorted by most recent first."
(let (file-list target-dir file-list-sorted start-file start-file-full)
;; clean directories from list but keep times
(setq file-list
(-remove (lambda (x) (nth 1 x))
(directory-files-and-attributes bjm/move-file-here-start-dir)))
;; get target directory
(setq target-dir
(if (equal major-mode 'dired-mode)
(expand-file-name default-directory)
(if (null (buffer-file-name))
(user-error "ERROR: current buffer is not associated with a file.")
(file-name-directory (buffer-file-name)))))
;; sort list by most recent
(setq file-list-sorted
(mapcar #'car
(sort file-list
#'(lambda (x y) (time-less-p (nth 6 y) (nth 6 x))))))
;; use ivy to select start-file
(setq start-file (ivy-read
(concat "Move selected file to " target-dir ":")
:re-builder #'ivy--regex
:sort nil
:initial-input nil))
;; add full path to start file and end-file
(setq start-file-full
(expand-file-name start-file bjm/move-file-here-start-dir))
(setq end-file
(expand-file-name (file-name-nondirectory start-file) target-dir))
(rename-file start-file-full end-file)
(message "moved %s to %s" start-file-full end-file)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment