Search code examples
emacsdired

emacs: How to Capture stdout from dired-do-shell-command?


In Emacs, is there a way to capture the stdout of dired-do-shell-command, say to the kill-ring?

Without knowing how, I end up going to the Messages buffer and getting the stdout manually from there.


Solution

  • Help for that command says that output goes to a buffer named *Shell Command Output*, assuming command has not &. If this is the case, this piece of code will do what you want:

    (defun do-shell-and-copy-to-kill-ring (command &optional arg file-list)
      (interactive
       (let ((files (dired-get-marked-files t current-prefix-arg)))
         (list
          (dired-read-shell-command "! on %s: " current-prefix-arg files)
          current-prefix-arg
          files)))
      (dired-do-shell-command command arg file-list)
      (with-current-buffer "*Shell Command Output*"
        (copy-region-as-kill (point-min) (point-max))))
    

    For async commands, you need to wait for them and look in *Async Shell Command* buffer.