Emacs dired - 使用预定义变量

5
在emacs的dired中,我想要做一些我在Microsoft PowerShell中经常做的事情。
在PowerShell中,我有一组文件夹经常使用,我将它们的完整路径分配给我的配置脚本中的全局变量(类似于emacs中的init.el),例如:
$standardTemp = "C:\Long\Path\To\Folder"

如果我在另一个文件夹里,想要将某个东西复制到上面的文件夹中,我需要执行以下操作:
copy myFile $standardTemp

作为一项非常有用的功能,如果我在$standardTemp后面加上反斜杠,它将展开,这样如果需要进入子文件夹,我就可以轻松实现。这是一个非常棒的功能,可以节省很多时间。

使用dired复制命令时,如果我在init.el文件中使用setq定义变量,我是否也可以做类似的操作呢?


我不使用它,所以也许我真的错了,但我猜你可以用dired书签做类似的事情。然而,我不确定你想要的复制功能。所以...基本上,你可以忽略这个评论 :P - Choma
4个回答

3
这个怎么样?
;; 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 buffer,并使用'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)))

1
当我需要使用dired进入常用目录时,我会使用标准的Emacs书签功能。我手动导航到该目录,然后按下。
C-x r m

执行该命令
bookmark-set

你将被提示输入书签的名称。输入一个简短的快捷键以便于记忆。

此时,任何时候只需执行命令

bookmark-jump

即可打开该目录的dired视图。

C-x r b

输入您的目录快捷方式,Dired 将打开到该位置。

要从一个目录复制到另一个目录,请确保在您的初始化文件中设置了以下内容。

(setq dired-dwim-target t)

然后,您可以在同一窗口中为源目录和目标目录打开dired窗口,dired将自动为适当的目录分配源和目标位置。

请注意,这只是emacs书签可以为您完成的功能子集!

  • Chris

0
如果您想将环境变量的值插入到minibuffer中,可以按照以下方式操作:
C-u M-: (getenv "THE-VARIABLE")

其中THE-VARIABLE是变量名。使用C-u将求值后的S表达式的值插入到当前缓冲区(在本例中是迷你缓冲区)。

因此,您可以使用C复制Dired中标记的文件,然后使用带有现有变量的getenv S表达式的C-u,以在提示要复制到的目录时将其值插入到迷你缓冲区中。

(根据您的Emacs设置,您可能需要将enable-recursive-minibuffers设置为非nil,才能从迷你缓冲区使用M-:


0
除了使用书签之外,考虑使用目录别名(例如符号链接)或directory-abbrev-alist。请参阅Emacs手册中的文件别名节点。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接