Function: dired-mark-files-containing-regexp

dired-mark-files-containing-regexp is an interactive and byte-compiled function defined in dired.el.gz.

Signature

(dired-mark-files-containing-regexp REGEXP &optional MARKER-CHAR)

Documentation

Mark all files with contents containing REGEXP for use in later commands.

A prefix argument means to unmark them instead.
. and .. are never marked.

If the region is active in Transient Mark mode, mark files only in the active region if dired-mark-region is non-nil.

Note that if a file is visited in an Emacs buffer, and dired-always-read-filesystem is nil, this command will look in the buffer without revisiting the file, so the results might be inconsistent with the file on disk if its contents has changed since it was last visited.

Probably introduced at or before Emacs version 26.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/dired.el.gz
(defun dired-mark-files-containing-regexp (regexp &optional marker-char)
  "Mark all files with contents containing REGEXP for use in later commands.
A prefix argument means to unmark them instead.
`.' and `..' are never marked.

If the region is active in Transient Mark mode, mark files
only in the active region if `dired-mark-region' is non-nil.

Note that if a file is visited in an Emacs buffer, and
`dired-always-read-filesystem' is nil, this command will
look in the buffer without revisiting the file, so the results might
be inconsistent with the file on disk if its contents has changed
since it was last visited."
  (interactive
   (list (read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
                              " files containing (regexp): ")
                      nil 'dired-regexp-history)
	 (if current-prefix-arg ?\s)))
  (let ((dired-marker-char (or marker-char dired-marker-char)))
    (dired-mark-if
     (and (not (looking-at-p dired-re-dot))
	  (not (eolp))			; empty line
	  (let ((fn (dired-get-filename nil t)))
	    (when (and fn (file-readable-p fn)
		       (not (file-directory-p fn)))
	      (let ((prebuf (get-file-buffer fn)))
		(message "Checking %s" fn)
		;; For now we do it inside emacs
		;; Grep might be better if there are a lot of files
		(if (and prebuf (not dired-always-read-filesystem))
		    (with-current-buffer prebuf
		      (save-excursion
			(goto-char (point-min))
			(re-search-forward regexp nil t)))
		  (with-temp-buffer
		    (insert-file-contents fn)
		    (goto-char (point-min))
		    (re-search-forward regexp nil t)))))))
     "matching file")))