Question Details

No question body available.

Tags

regex file emacs command config

Answers (2)

July 14, 2025 Score: 2 Rep: 74,295 Quality: Medium Completeness: 60%

Redefine this function in your config (I recommend using :override advice).

(defun find-file-read-args (prompt mustmatch)
  (list (read-file-name prompt nil default-directory mustmatch)
    t))

Make it pass a PREDICATE function to read-file-name (see which). That function will perform your test.

(defun my-find-file-predicate (file)
  "Return nil to exclude FILE from completions."
  ...)

(define-advice find-file-read-args (:override (prompt mustmatch) my-predicate) "Make find-file' filter completions with my-find-file-predicate'.

This is advice for `find-file-read-args'. Remove it with: \(advice-remove \\='find-file-read-args \\='find-file-read-args@my-predicate)" (list (read-file-name prompt nil default-directory mustmatch nil 'my-find-file-predicate) t))

;; (advice-remove 'find-file-read-args 'find-file-read-args@my-predicate)

You could alternatively let-bind completion-regexp-list around the call to read-file-name, but that's for positive rather than negative matches, so the regexp would be a lot gnarlier.

July 16, 2025 Score: 1 Rep: 30,888 Quality: Low Completeness: 50%

Define your own command, passing a predicate to read-file-name in the interactive spec. The predicate just returns false (nil) whenever a file name matches the regexp dired-omit-files and true (non-nil) otherwise.

(defun my-find-file (file &optional wildcards)
  "Like find-file, but respect `dired-omit-files'."
  (interactive (list (read-file-name "Find file: "
                                     nil
                                     default-directory
                                     (confirm-nonexistent-file-or-buffer)
                                     nil
                                     (lambda (f) (not (string-match-p dired-omit-files f))))
                     t))
  (find-file file wildcards))

(global-set-key (kbd "M-f") #'my-find-file) ; Bind M-f (A-f) to the command.