在emacs dired中,我想做一些我经常在Microsoft PowerShell中做的事情.
在PowerShell中,我有一组我一直使用的文件夹,并且我在我的配置文件脚本中分配了全局变量的完整路径(类似于init.elemacs世界),例如:
$standardTemp = "C:\Long\Path\To\Folder"
Run Code Online (Sandbox Code Playgroud)
如果我在另一个文件夹中,并且我想将某些内容复制到上面的文件夹,我会:
copy myFile $standardTemp
Run Code Online (Sandbox Code Playgroud)
作为一个功能更有用的是,如果我之后添加反斜杠$standardTemp,它会将其展开,所以如果需要,我可以进入子文件夹.这是一个非常棒的功能,节省了我很多时间.
使用dired copy命令可以做类似的事情,如果我在例如setq我的init.el文件中定义变量?
像这样的事情怎么样?
;; Use ido
(require 'ido)
(ido-mode t)
;; Make a hash table to hold the paths
(setq my-target-dirs (make-hash-table :test 'equal))
;; Put some paths in the hash (sorry for Unix pathnames)
(puthash "home" "/home/jhrr/" my-target-dirs)
(puthash "target" "/home/jhrr/target/" my-target-dirs)
;; A function to return all the keys from a hash.
(defun get-keys-from-hash (hash)
(let ((keys ()))
(maphash (lambda (k v) (push k keys)) hash)
keys))
;; And the function to prompt for a directory by keyword that is looked
;; up in the hash-table and used to build the target path from the
;; value of the lookup.
(defun my-dired-expand-copy ()
(interactive)
(let* ((my-hash my-target-dirs)
(files (dired-get-marked-files))
(keys (get-keys-from-hash my-hash)))
(mapc (lambda (file)
(copy-file file
(concat
(gethash
(ido-completing-read
(concat "copy " file " to: ") keys) my-hash)
(file-name-nondirectory file))))
files)))
Run Code Online (Sandbox Code Playgroud)
它没有经过详尽的测试,因为我只是在 10 分钟内完成了它,但它可以完成工作并且可以处理多个文件。
您需要打开文件所在目录中的 dired 缓冲区,并用“m”标记要复制的每个文件,然后调用my-dired-expand-copy,它将提示您输入目标目的地(以哈希中的关键字的形式)表(我们为该文件设置),最后将文件复制到映射到目标关键字的目录。
它并没有完全涵盖您提到的子目录用例,但考虑到更多的黑客攻击,到达那里应该不会太难。
更新:
现在应该提示您能够从原始目标下降到子目录;总体而言,也许不是最令人心碎的美妙用户体验,但是,它确实有效:
(defun my-dired-expand-copy-2 ()
(interactive)
(let* ((my-hash my-target-dirs)
(files (dired-get-marked-files))
(keys (get-keys-from-hash my-hash)))
(mapc (lambda (file)
(let ((target (gethash
(ido-completing-read
(concat "copy " file " to: ") keys) my-hash)))
(if (y-or-n-p "Descend?")
;; Descend into subdirectories relative to target dir
(let ((new-target (ido-read-directory-name "new dir: " target)))
(copy-file file (concat new-target
(file-name-nondirectory file)))
(message (concat "File: " file " was copied to " new-target)))
;; Else copy to root of originally selected directory
(copy-file file (concat target (file-name-nondirectory file)))
(message (concat "File: " file " was copied to " target)))))
files)))
Run Code Online (Sandbox Code Playgroud)