在emacs dired中,如何在多个标记文件上运行命令?


5

我有几个标记为直接模式的文件,我想在其上运行一个命令(比如(delete-trailing-whitespace)每个文件。

有没有内置的方法来做到这一点,还是我需要自己编写?

基本上我想做类似的事情,(dired-do-shell-command)但我想评估emacs表达式而不是shell命令。我想在emacs中对我标记的文件执行此操作,因此我无法使用-batch

Answers:


8

我写了一些东西来做我想要的,以防其他人发现它有用:

更新:使用更通用的命令更新此解决方案。

(defun mrc-dired-do-command (command)
  "Run COMMAND on marked files. Any files not already open will be opened.
After this command has been run, any buffers it's modified will remain
open and unsaved."
  (interactive "CRun on marked files M-x ")
  (save-window-excursion
    (mapc (lambda (filename)
            (find-file filename)
            (call-interactively command))
          (dired-get-marked-files))))

现在M-x mrc-dired-do-command delete-trailing-whitespace做我想要的。

如果有人向我指出我不必这样做,我会很高兴,我忽略了一个明显的命令dired-do-command


4

AFAIK dired没有这样的工具,但ibuffer确实 - 使用dired标记你想要影响的所有文件(m),访问它们(C-u F),运行ibuffer(M-x ibuffer [RET]),标记所有缓冲区(m)和invoke ibuffer-do-evalE),插入要在每个缓冲区中计算的表达式,将它们全部保存(S)并关闭它们(D)。


1

这不是你要求的,但你可以在ibuffer中标记缓冲区并使用'E'(ibuffer-do-eval FORM)

我知道eshell允许你使用elisp进行shell脚本编写(有点),所以你可以从eshell更容易地做到这一点。


谢谢shapr,这非常有用,虽然我确实想在dired中做。
马特柯蒂斯于2010年

1

Matt的解决方案很有用,但它有点受限,因为它只允许执行单个命令而不需要任何参数。我想知道是否可以在标记文件上执行宏,并提出了以下dired命令,该命令允许为每个选定文件重放任意(保存)的宏任意次。

使用此命令,我终于能够以非常重要的方式半自动编辑许多文件!

更新:新版本允许执行任意命令(包括宏)和lisp表达式。

;; Inspired by M-x edit-kbd-macro and https://superuser.com/q/176627.
(defun my-dired-do-execute (keys &optional arg)
  "Execute a command in all marked files.
If an error occurs, execution in other files is not affected.
(Notably, this allows to run keyboard macros until there is an error.)

At the prompt, type any bound key sequence, or `\\[execute-extended-command]'
to choose a command by its name, or `\\[eval-expression]' to enter a Lisp expression.

The prefix ARG, if given, is passed on to the chosen command.
"
  (interactive
   (list (read-key-sequence (substitute-command-keys "Key sequence to execute, \
or \\[eval-expression], or \\[execute-extended-command]: "))
         current-prefix-arg))
  (when keys
    (let ((cmd (if (arrayp keys) (key-binding keys) keys))
          exp)
      (cond ((eq cmd 'execute-extended-command)
             (setq cmd (read-command "Name of command to execute: "))
             (if (string-equal cmd "")
                 (error "No command name given")))
            ((eq cmd 'eval-expression)
             (setq exp (read--expression "Eval in selected files: "))
             (setq cmd nil))
            ((null cmd)
             (error "Key sequence %s is not defined" (key-description keys))))
      (mapc (lambda (filename)
              (save-selected-window
                (find-file-other-window filename)
                (setq current-prefix-arg arg)
                (condition-case-unless-debug err
                    (if cmd
                        (call-interactively cmd)
                      (message "Result in file %s:" filename)
                      (eval-expression exp))
                  (error (message "In file %s: %S" filename err)))))
            (dired-get-marked-files)))))
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.