Function: evil-ex-global

evil-ex-global is an interactive and byte-compiled function defined in evil-commands.el.

Signature

(evil-ex-global BEG END &optional PATTERN COMMAND INVERT)

Documentation

The Ex global command.

[BEG,END]global[!]/PATTERN/COMMAND

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/evil-20251108.138/evil-commands.el
(evil-define-operator evil-ex-global
  (beg end pattern command &optional invert)
  "The Ex global command.
\[BEG,END]global[!]/PATTERN/COMMAND"
  :motion mark-whole-buffer
  :move-point nil
  (interactive "<r><g/><!>")
  (unless pattern
    (user-error "No pattern given"))
  (unless command
    (user-error "No command given"))
  ;; TODO: `evil-ex-make-substitute-pattern' should be executed so
  ;; :substitute can re-use :global's pattern depending on its `r'
  ;; flag. This isn't supported currently but should be simple to add
  (let* ((case-fold-search
          (eq (evil-ex-regex-case pattern evil-ex-search-case) 'insensitive))
         (pattern (evil-ex-regex-without-case pattern))
         (command-form (evil-ex-parse command))
         (ex-delete (eq 'evil-ex-delete (evil-ex-completed-binding (nth 2 command-form))))
         (transient-mark-mode transient-mark-mode)
         (deactivate-mark deactivate-mark)
         markers)
    (when (and pattern command)
      (when evil-ex-search-vim-style-regexp
        (setq pattern (evil-transform-vim-style-regexp pattern)))
      (if (and ex-delete (not (nth 3 command-form)) (not (nth 1 command-form)))
          (evil--ex-performant-global-delete beg end pattern invert)
        (setq isearch-string pattern)
        (isearch-update-ring pattern t)
        (goto-char beg)
        (evil-move-beginning-of-line)
        (while (< (point) end)
          (let ((match (re-search-forward pattern (line-end-position) t)))
            (when (if invert (not match) match)
              (push (move-marker (make-marker)
                                 (or (and match (match-beginning 0))
                                     (line-beginning-position)))
                    markers)))
          (forward-line))
        (setq markers (nreverse markers))
        (unwind-protect
            (evil-with-single-undo
              (let ((evil--ex-global-active-p t))
                (dolist (marker markers)
                  (goto-char marker)
                  (eval command-form t))))
          ;; ensure that all markers are deleted afterwards,
          ;; even in the event of failure
          (dolist (marker markers)
            (set-marker marker nil))
          (run-hooks 'evil-after-global-hook))))))