Passing Emacs variables to minibuffer shell commands

前端 未结 6 2092
误落风尘
误落风尘 2020-12-19 06:13

I can run a shell command quickly by hitting M-!. One thing I\'d like to do is perform shell quick operations on the current file. An example would be checking th

6条回答
  •  遥遥无期
    2020-12-19 06:50

    Everyone seems to be rolling their own version, so here's mine -- it will substitue the current filename or marked dired-files or current dired file wherever a % is in the shell command. It follows the same conventions as M-! so I bind it to that.

    (defun my-shell-command (command &optional output-buffer error-buffer)
      "Run a shell command with the current file (or marked dired files).
    In the shell command, the file(s) will be substituted wherever a '%' is."
      (interactive (list (read-from-minibuffer "Shell command: "
                                               nil nil nil 'shell-command-history)
                         current-prefix-arg
                         shell-command-default-error-buffer))
      (cond ((buffer-file-name)
             (setq command (replace-regexp-in-string "%" (buffer-file-name) command nil t)))
            ((and (equal major-mode 'dired-mode) (save-excursion (dired-move-to-filename)))
             (setq command (replace-regexp-in-string "%" (mapconcat 'identity (dired-get-marked-files) " ") command nil t))))
      (shell-command command output-buffer error-buffer))
    

提交回复
热议问题