有时我想创建多个文件的副本(例如,配置文件),这些文件最初应该与初始文件具有相同的内容。因此,我希望能够将一些文件标记为 dired 并“复制”它们,这个复制过程可以像大多数文件管理器使用的复制过程一样工作,当粘贴到原始目录时:复制的文件名得到“ (复制)”附加(就在文件扩展名之前)。
我似乎找不到这样做的内置函数,也许有人可以帮助/已经创建了这样的函数?
非常感谢您的帮助!
有一个功能可以满足您的需求:dired-do-copy-regexp
使用示例:
M-x dired-do-copy-regexp
\(.*\)\.\(.*\)
\1 (copy).\2
对于名为 foo.txt 的文件,您将创建另一个名为 foo (copy).txt
请注意,我的第一个正则表达式有两个组,第二个正则表达式引用它们。如果需要,您可以做更复杂的事情。
也许你会想要重命名函数(我没有想出更好的名字),也许一些更复杂的格式,如果你愿意的话......
(defcustom dired-keep-marker-version ?V
"Controls marking of versioned files.
If t, versioned files are marked if and as the corresponding original files were.
If a character, copied files are unconditionally marked with that character."
:type '(choice (const :tag "Keep" t)
(character :tag "Mark"))
:group 'dired-mark)
(defun dired-version-file (from to ok-flag)
(dired-handle-overwrite to)
(dired-copy-file-recursive from to ok-flag dired-copy-preserve-time t
dired-recursive-copies))
(defun dired-do-version (&optional arg)
"Search for numeric pattern in file name and create a version of that file
with that number incremented by one, or, in case such file already exists,
will search for a file with the similar name, incrementing the counter each
time by one.
Additionally, if called with prefix argument, will prompt for number format.
The formatting is the same as is used with `format' function."
(interactive "P")
(let ((fn-list (dired-get-marked-files nil nil)))
(dired-create-files
(function dired-version-file) "Version" fn-list
(function
(lambda (from)
(let (new-name (i 0) (fmt (if arg (read-string "Version format: " "%d") "%d")))
(while (or (null new-name) (file-exists-p new-name))
(setq new-name
(if (string-match "^\\([^0-9]*\\)\\([0-9]+\\)\\(.*\\)$" from)
(concat (match-string 1 from)
(format fmt
(+ (string-to-number (match-string 2 from)) (1+ i)))
(match-string 3 from))
(concat from (format (concat "." fmt) i)))
i (1+ i))) new-name)))
dired-keep-marker-version)))
(define-key dired-mode-map (kbd "c") 'dired-do-version)
另外,我最初习惯于v绑定这个函数,因为我不使用dired-view
,但是你需要在dired
s 钩子中绑定它。c只是第一个未定义的键,所以我使用了它。
在 Dired 模式下,将光标放在要复制的文件上或标记该文件,然后按“C”。系统将提示您输入新名称。
您也可以使用此功能在 Dired 缓冲区之间复制文件。为了使它可以放入您的初始化文件中:
(setq dired-dwim-target t)