Function: evil-shell-command

evil-shell-command is an interactive and byte-compiled function defined in evil-commands.el.

Signature

(evil-shell-command BEG END &optional TYPE COMMAND PREVIOUS)

Documentation

Execute a shell command.

If BEG, END and TYPE is specified, COMMAND is executed on the region, which is replaced with the command's output. Otherwise, the output is displayed in its own buffer. If PREVIOUS is non-nil, the previous shell command is executed instead.

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/evil-20251108.138/evil-commands.el
(evil-define-operator evil-shell-command
  (beg end _type command &optional previous)
  "Execute a shell command.
If BEG, END and TYPE is specified, COMMAND is executed on the region,
which is replaced with the command's output. Otherwise, the
output is displayed in its own buffer. If PREVIOUS is non-nil,
the previous shell command is executed instead."
  (interactive "<R><sh><!>")
  (if (not evil-called-from-ex-p)
      (let ((current-prefix-arg
             (if (or current-prefix-arg (evil-visual-state-p))
                 current-prefix-arg
               (goto-char (min beg end))
               (count-lines beg end)))
            (evil-ex-initial-input "!"))
        (call-interactively #'evil-ex))
    (if (zerop (length command))
        (when previous (setq command evil-previous-shell-command))
      (setq command (evil-ex-replace-special-filenames command)
            evil-previous-shell-command command))
    (cond
     ((zerop (length command))
      (user-error "No%s shell command" (if previous " previous" "")))
     (evil-ex-range
      (if (not evil-display-shell-error-in-message)
          (shell-command-on-region beg end command nil t)
        (let ((output-buffer (generate-new-buffer " *temp*"))
              (error-buffer (generate-new-buffer " *temp*")))
          (unwind-protect
              (if (zerop (shell-command-on-region
                          beg end command
                          output-buffer nil error-buffer))
                  (progn
                    (delete-region beg end)
                    (insert-buffer-substring output-buffer)
                    (goto-char beg)
                    (evil-first-non-blank))
                (display-message-or-buffer error-buffer))
            (kill-buffer output-buffer)
            (kill-buffer error-buffer)))))
     (t (shell-command command)))))