Emacs dired-使用预定义变量
在emacs dired中,我想做一些我在Microsoft PowerShell中经常做的事情 在PowerShell中,我有一组始终使用的文件夹,我在配置文件脚本中将它们的完整路径分配给全局变量(类似于emacs世界中的Emacs dired-使用预定义变量,emacs,dired,Emacs,Dired,在emacs dired中,我想做一些我在Microsoft PowerShell中经常做的事情 在PowerShell中,我有一组始终使用的文件夹,我在配置文件脚本中将它们的完整路径分配给全局变量(类似于emacs世界中的init.el),例如: 如果我在另一个文件夹中,并且我想将某些内容复制到上述文件夹中,我会: copy myFile $standardTemp 更有用的功能是,如果我在$standardTemp后面加上反斜杠,它会将其展开,以便在需要时可以进入子文件夹。这是一个非常棒的
init.el
),例如:
如果我在另一个文件夹中,并且我想将某些内容复制到上述文件夹中,我会:
copy myFile $standardTemp
更有用的功能是,如果我在$standardTemp
后面加上反斜杠,它会将其展开,以便在需要时可以进入子文件夹。这是一个非常棒的功能,为我节省了很多时间
使用dired copy命令,如果我在
init.el
文件中使用例如setq
定义变量,我可以做类似的事情吗?类似的事情怎么样
;; 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)))
它并没有经过详尽的测试,因为我只是在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)))
当我需要使用dired访问常用目录时,我使用标准的emacs书签功能 我手动导航到该目录,然后按
C-x r m
执行命令
bookmark-set
系统将提示您输入书签的名称。输入您可以记住的快捷方式
此时,只要执行以下命令,就可以随时在dired中打开该目录
bookmark-set
书签跳转
用钥匙
C-x r b
输入目录的快捷方式,dired将打开到该位置
要从一个目录复制到另一个目录,请确保在init文件中设置了以下内容
(setq dired-dwim-target t)
然后,您可以在同一帧中为源目录打开一个dired窗口,为中的目标目录打开另一个窗口,dired将自动将源和目标位置分配给相应的目录
注意,这只是emacs书签可以为您做的事情的一个子集
- 克里斯
除了使用书签之外,还可以考虑使用目录名别名(例如Syrink)或<强> >代码>目录缩写符< /Cord><强>。请参阅Emacs手册,节点。
如果要将环境变量的值插入微型缓冲区,可以通过以下方式执行:C-u M-: (getenv "THE-VARIABLE")
其中,变量
是变量名。使用C-u
将评估sexp的值插入当前缓冲区(在本例中为迷你缓冲区)
因此,您可以使用C
复制Dired中标记的文件,然后对现有变量使用C-u
和getenv
sexp,在提示复制到的目录时将其值插入迷你缓冲区
(根据您的Emacs设置,您可能需要将
enable recursive minibuffer
设置为non-nil
,以便能够从minibuffer中使用M-:
)我不使用它,所以可能我真的错了,但我想您可以用它做类似的事情。但我不确定你想要的复制功能。所以基本上,你可以忽略这个评论:P