繁体   English   中英

Emacs dired - 使用预定义变量

[英]Emacs dired - using predefined variable

在emacs dired中,我想做一些我经常在Microsoft PowerShell中做的事情。

在PowerShell中,我有一组我一直使用的文件夹,并且我在我的配置文件脚本中分配了全局变量的完整路径(类似于emacs世界中的init.el ),例如:

$standardTemp = "C:\Long\Path\To\Folder"

如果我在另一个文件夹中,并且我想将某些内容复制到上面的文件夹,我会:

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中打开该目录,只需执行该命令即可

书签,跳

用钥匙

C-x r b

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

要从一个目录复制到另一个目录,请确保在init文件中设置了以下内容

(setq dired-dwim-target t)

然后,您可以打开源目录的dired窗口,以及同一帧内目标目录的另一个窗口,dired将自动将源和目标位置分配给相应的目录。

请注意,这只是emacs书签可以为您做的一部分!

  • 克里斯

除了使用书签外,还可以考虑使用目录名别名(例如符号链接)或directory-abbrev-alist 请参阅Emacs手册,节点File Aliases

如果要将环境变量的值插入到迷你缓冲区中,可以这样做:

C-u M-: (getenv "THE-VARIABLE")

其中THE-VARIABLE是变量名。 使用Cu将评估sexp的值插入当前缓冲区(在本例中为迷你缓冲区)。

因此,您可以使用C来复制Dired中标记的文件,然后使用Cugetenv sexp作为现有变量,在提示要复制到的目录时将其值插入到迷你缓冲区中。

(根据您的Emacs设置,您可能需要将enable-recursive-minibuffers设置为非nil ,以便能够使用M-:来自迷你缓冲区。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM